说明:
最近经常根据需求修改界面的UI,于是索性改为外部读取照片并转换为Sprite显示,简单记录一下。
实现:
public class GetUIfile : MonoBehaviour
{
private string UIPath = Application.streamingAssetsPath + "/UI/1.png";
public Image image;
private void Awake()
{
//调用
TextureToSprite(ReturnImgByte(UIPath));
}
private void TextureToSprite(byte[] ImgByte)
{
Texture2D texture2D = new Texture2D(1080, 1920);
texture2D.LoadImage(ImgByte);
Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));
image.sprite = sprite;
}
//返回字节数组
private byte[] ReturnImgByte(string UIPath)
{
FileStream fs = new FileStream(UIPath, FileMode.Open);
byte[] imgByte = new byte[fs.Length];
fs.Read(imgByte,0,imgByte.Length);
fs.Close();
return imgByte;
}
}