Unity IO 加载图片
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.UI; public class LoaderPic:MonoBehaviour { /// <summary> /// 以IO方式进行加载 /// </summary> public static void LoadByIo(string url,GameObject ImageOBJ) {//创建文件读取流 FileStream fileStream = new FileStream(url,FileMode.Open,FileAccess.Read); //创建文件长度缓冲区 byte[] bytes = new byte[fileStream.Length]; //读取文件 fileStream.Read(bytes,0,(int)fileStream.Length); //释放文件读取流 fileStream.Close(); //释放本机屏幕资源 fileStream.Dispose(); fileStream = null; //创建Texture int width = 300; int height = 372; Texture2D texture = new Texture2D(width,height); texture.LoadImage(bytes); //创建Sprite Sprite sprite = Sprite.Create(texture,new Rect(0,0,texture.width,texture.height),new Vector2(0.5f,0.5f)); ImageOBJ.GetComponent<Image>().sprite = sprite; } }
posted on 2022-10-19 10:48 zqiang0803 阅读(62) 评论(0) 编辑 收藏 举报