Created
July 23, 2017 05:04
-
-
Save TakaakiIchijo/55415025597dc3e953711692e59ec8d9 to your computer and use it in GitHub Desktop.
Unityでbyte配列からテクスチャ作ってスプライト化するまでやる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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