從流數(shù)據(jù)轉(zhuǎn)成Sprite,需要使用Texture2D作為中轉(zhuǎn)
1.從sprite 轉(zhuǎn)換到bytes[]
public byte[] GetByte(Sprite sp)
{
//轉(zhuǎn)換成Texture
Texture2D temp = sp.texture;
//在轉(zhuǎn)換成bytes
byte[] photoByte = temp.EncodeToPNG();
return photoByte;
}
2. 從bytes[] 轉(zhuǎn)換到Sprite
public Sprite GetSprite(Byte[] bytes)
{
//先創(chuàng)建一個(gè)Texture2D對(duì)象,用于把流數(shù)據(jù)轉(zhuǎn)成Texture2D
Texture2D texture = new Texture2D(10, 10);
texture.LoadImage(bytes);//流數(shù)據(jù)轉(zhuǎn)換成Texture2D
//創(chuàng)建一個(gè)Sprite,以Texture2D對(duì)象為基礎(chǔ)
Sprite sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
return sp;
}