Skip to content

Instantly share code, notes, and snippets.

@TakaakiIchijo
Created July 23, 2017 05:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TakaakiIchijo/55415025597dc3e953711692e59ec8d9 to your computer and use it in GitHub Desktop.
Save TakaakiIchijo/55415025597dc3e953711692e59ec8d9 to your computer and use it in GitHub Desktop.
Unityでbyte配列からテクスチャ作ってスプライト化するまでやる
using UnityEngine;
public static class Util
{
public static Sprite CreateSpriteFromBytes(byte[] bytes)
{
//横サイズの判定
int pos = 16;
int width = 0;
for (int i = 0; i < 4; i++)
{
width = width * 256 + bytes[pos++];
}
//縦サイズの判定
int height = 0;
for (int i = 0; i < 4; i++)
{
height = height * 256 + bytes[pos++];
}
//byteからTexture2D作成
Texture2D texture = new Texture2D(width, height);
texture.LoadImage(bytes);
return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment