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) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律