GUI之绘制2D贴图
来源:互联网 发布:linux切换oracle实例 编辑:程序博客网 时间:2024/05/29 02:32
绘制贴图
在C#编写的脚本中,在屏幕中绘制一张静态贴图
,需要使用GUI.DrawTexture()方法,重载方法很多,输入参数可以在写代码时候多看下定义;参数最全的方法的原型为:
public static void DrawTexture(Rect position, Texture image, ScaleMode
scaleMode, bool alphaBlend);
//
// 摘要:
// Draw a texture within a rectangle.
//
// 参数:
// position:
// Rectangle on the screen to draw the texture within.
//
// image:
// Texture to display.
//
// scaleMode:
// How to scale the image when the aspect ratio of it doesn't fit the aspect
// ratio to be drawn within.
//
// alphaBlend:
// Whether to apply alpha blending when drawing the image (enabled by default).
//
// imageAspect:
// Aspect ratio to use for the source image. If 0 (the default), the aspect
// ratio from the image is used. Pass in w/h for the desired aspect ratio. This
// allows the aspect ratio of the source image to be adjusted without changing
// the pixel width and height.
比如:下面的表达式即可绘制贴图,注意C#中使用new Rect(…),而JavaScript中直接使用Rect(…),只是因为C#中的原型要求Rect position = new Rect(…)这样的表达式。
GUI.DrawTexture(new Rect(100, 20, 50, 50), texSingle, ScaleMode.StretchToFill, true, 0);
注意:C#中:className obj = new className();// obj是引用,本质是地址,不使用指针
。C++中: className *obj = new className();// 这里使用的是指针
加载资源
1.资源必须放在Resources
文件夹中;
2.加载资源为Resources.Load()
和Resources.LoadAll()
方法,参数均为资源文件夹的完整路径,前者返回读取的资源对象,后者返回的是资源对象的数组。
具体用法参看下面的代码清单:
using UnityEngine;using System.Collections;public class TextureTest : MonoBehaviour { public float startX, startY;// 设置为公有,可以在U3D的Inspector中进行设置数值 public Texture2D texSingle;// 一张贴图 public Object[] texAll; // 一组贴图对象 // Update is called once per frame void OnGUI () { /* ------------------ 加载贴图 ---------------------------- */ if (GUI.Button(new Rect(startX, startY, 100, 20), "加载一张帖图")){ if (texSingle == null){ // 加载单个贴图 texSingle = (Texture2D)Resources.Load("single/0"); } } if (GUI.Button(new Rect(startX, startY + 60, 100, 20), "加载一组贴图")){ // 加载一组贴图,textures文件夹中的所有贴图 texAll = Resources.LoadAll("textures");// public static Object[] Load(string path); } /* ------------------ 设置布局、绘制贴图 ------------------ */ // 默认垂直布局 // 第一行的水平线性布局 GUILayout.BeginHorizontal(); if (texSingle != null){ // 绘制贴图 GUI.DrawTexture(new Rect(startX + 100, startY, 50, 50), texSingle, ScaleMode.StretchToFill, true, 0); } GUILayout.EndHorizontal(); // 第二行水平线性布局 GUILayout.BeginHorizontal(); if (texAll != null) { for (int i = 0; i < texAll.Length; ++i) { Texture2D tmp = (Texture2D)texAll[i]; GUI.DrawTexture(new Rect(startX + 50 * i + 100, startY + 60, 50, 50), tmp, ScaleMode.StretchToFill, true, 0); } } GUILayout.EndHorizontal(); }}
- GUI之绘制2D贴图
- μc/GUI学习笔记之四2D图形绘制
- OpenGL ES绘制3D纹理贴图
- unity 2d贴图
- Quartz 2D之基本图形绘制
- 2D制图实战之绘制时钟
- 2D制图实战之绘制星球
- QT5之2D绘图-绘制路径
- Android GUI之View绘制流程
- Directx 2D的alpha 通道贴图
- 通过代码创建2d贴图
- Android OpenGL 2D贴图切换
- 记录一下GLFW的2D贴图
- Android图形绘制之——贴图的艺术
- Qt绘制2d
- Qt绘制2d
- [Unity3D]Unity3D游戏开发之2D贴图实现血条组件
- html5之canvas标签应用之 2d图形绘制以及图片绘制
- uboot必须解决哪些问题
- An Introduction to Machine Learning with Python
- Linux下配置用msmtp和mutt发邮件
- iOS开发总结之仿qq侧滑功能
- js代码实现查找关键字高亮显示
- GUI之绘制2D贴图
- poj 1741 树的分治。。。。
- 欢迎使用CSDN-markdown编辑器
- GPU 加速NLP任务(Theano+CUDA)
- 简单的leetcode题(四)
- 3.1.4 Memcached的简单使用过程
- LeetCode 110. Balanced Binary Tree
- 软件设计模式——你真的会写单例模式吗?
- 深度测试