unity3d-绘制贴图
2015-01-07 22:51 糯米粥 阅读(1645) 评论(0) 编辑 收藏 举报准备贴图
在屏幕在绘制一张静态贴图,需要用到GUI.DrawTexture()方法, 该方法可以设定图片的显示位置、缩放比例和渲染混合等
/* Rect position:表示图片的绘制区域 * Texture image:表示绘制图片的对象 * ScaleMode scaleMode:表示图片的缩放模式 * bool alphaBlend:表示十分开启图片混合模式 * float imageAspect:表示图片的缩放宽高比例 */ public static void DrawTexture(Rect position, Texture image, ScaleMode scaleMode, bool alphaBlend, float imageAspect);
在Project视图中将需要加载的图片保存在根目录 "Resources" 中,记住 一定要保存在Resources中,这是unity3d规定的,否则无法加载!!

加载并绘制贴图
加载贴图使用Resources.Load()和Resources.LoadAll()方法,
1 using UnityEngine; 2 using System.Collections; 3 4 public class loadText2d : MonoBehaviour 5 { 6 7 private Texture2D txt; 8 Texture2D[] array; 9 // Use this for initialization 10 void Start() 11 { 12 13 } 14 15 // Update is called once per frame 16 void Update() 17 { 18 19 } 20 void OnGUI() 21 { 22 //加载贴图 23 if (GUI.Button(new Rect(10, 50, 90, 90), "加载一张图片")) 24 { 25 txt = Resources.Load<Texture2D>("one/0"); 26 } 27 if (GUI.Button(new Rect(10, 150, 90, 90), "加载所有图片")) 28 { 29 array = Resources.LoadAll<Texture2D>("more"); 30 } 31 //绘制贴图 32 if (txt != null) 33 { 34 GUI.DrawTexture(new Rect(150, 50, 90, 90), txt, ScaleMode.StretchToFill, true, 0); 35 } 36 if (array != null) 37 { 38 for (int i = 0; i < array.Length; i++) 39 { 40 GUI.DrawTexture(new Rect(150 + i * 95, 150, 90, 90), array[i], ScaleMode.ScaleToFit, true, 0); 41 } 42 43 44 } 45 } 46 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?