简体   繁体   English

如何从字节数组制作精灵?

[英]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.

相关问题 如何从 NEF 或其他 RAW 格式制作字节数组并进行编辑? - How can i make byte array from NEF or another RAW Format and edit it? 如何从byte []数组中获取数组的一部分 - How can I get part of the array from a byte[] array 如何从字节数组转换为通用数组? - How can I convert from a Byte Array to Generic Array? 我如何从会话字节的数组中提取? 在会话内部也写入了字节的数组 - How can I take from Session byte's array? Inside the session was written byte's array too 如何从注册表到字节数组读取二进制数据 - How can I read binary data from registry to byte array 如何从SHA-1字节数组生成Guid? - How can I generate a Guid from a SHA-1 byte array? 如何制作可空字节[] - How can i make nulleable Byte[] 如何制作一个精灵以在单人游戏中从 position 到鼠标单击进行可见移动? - How can I make a sprite to do a visible moving from a position to a mouse click in monogame? 如何通过将垂直轴和水平轴的输入添加到变换来使 2D 精灵移动 - How can I make a 2D sprite move by adding the input from Vertical and Horizontal axis to transform 如何将子画面从子画面表更改为另一个子画面? - How to make a sprite change to another sprite from the sprite sheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM