[英]How can I make a sprite from a byte array?
I have a Unity game where the player gets one's inventory from the database after starting the game.我有一个 Unity 游戏,玩家在开始游戏后从数据库中获取自己的库存。 I got the pictures of items stored in Firebase Storage, and I can download them.
我得到了 Firebase Storage 中存储的项目的图片,我可以下载它们。 However, I don't know how to make sprites from the byte[] (fileContents) I get by downloading.
但是,我不知道如何从下载得到的 byte[] (fileContents) 制作精灵。 (I'd load these sprites in the player's inventory later)
(稍后我会将这些精灵加载到玩家的库存中)
I got the following C# script:我得到了以下 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!");
}
});
}
}
}
});
}
I believe that you're looking for ImageConversion.LoadImage我相信你正在寻找ImageConversion.LoadImage
Your full code will look something like:您的完整代码如下所示:
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));
You can also pack textures at runtime if that's your eventual goal.如果这是您的最终目标,您还可以在运行时打包纹理。 Once you get through the
tex.LoadImage
chunk, you should be able to work out Texture2D.PackTextures
.一旦你通过
tex.LoadImage
块,你应该能够计算出Texture2D.PackTextures
。
--Patrick ——帕特里克
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.