gpt4 book ai didi

c# - 如何从字节数组制作 Sprite ?

转载 作者:行者123 更新时间:2023-12-05 06:20:18 25 4
gpt4 key购买 nike

我有一个 Unity 游戏,玩家在开始游戏后从数据库中获取一个人的元素栏。我得到了存储在 Firebase Storage 中的项目的图片,我可以下载它们。但是,我不知道如何从我通过下载获得的 byte[] (fileContents) 制作 Sprite 。(稍后我会将这些 Sprite 加载到玩家的元素栏中)

我得到了以下 C# 脚本:

private void getPlayersInventory()
{
FirebaseDatabase.DefaultInstance.GetReference("users").Child(auth.CurrentUser.UserId)
.Child("inventory").GetValueAsync().ContinueWith(task =>
{
if (task.IsFaulted)
{
print("unable to get snapshot for the inventory");
}
else if (task.IsCompleted)
{
snapshot = task.Result;
if(snapshot.HasChildren)
{
foreach(var current in snapshot.Children)
{
string currentName = current.Value.ToString();
print(currentName.ToLower() + ".png");
const long maxAllowedSize = 10 * 1024 * 1024;
storageReference.Child(currentName.ToLower() + ".png")
.GetBytesAsync(maxAllowedSize)
.ContinueWith((Task<byte[]> task_) =>
{
if (task_.IsFaulted || task_.IsCanceled)
{
Debug.Log(task_.Exception.ToString());
}
else
{
byte[] fileContents = task_.Result;
Debug.Log("Finished downloading!");
}
});
}
}
}
});
}

最佳答案

我相信您正在寻找 ImageConversion.LoadImage

您的完整代码如下所示:

var tex = new Texture(1,1); // note that the size is overridden
tex.LoadImage(task_.Result);
var sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(tex.width/2, tex.height/2));

您也可以pack textures如果这是您的最终目标,则在运行时。一旦你完成了 tex.LoadImage block ,你应该能够计算出 Texture2D.PackTextures

--帕特里克

关于c# - 如何从字节数组制作 Sprite ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60578314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com