码迷,mamicode.com
首页 > 编程语言 > 详细

unity3d-绘制贴图

时间:2015-01-07 23:22:48      阅读:261      评论:0      收藏: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 }

 

Demo演示

技术分享

unity3d-绘制贴图

标签:

原文地址:http://www.cnblogs.com/niboy/p/4209610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!