纳金网
标题:
Unity3D中2D元素的处理
[打印本页]
作者:
会飞的鱼
时间:
2012-2-21 14:49
标题:
Unity3D中2D元素的处理
先前在绘制2D元素的时候用的是GUI的DrawTexture()。用法很简单,类似这样
var tex : Texture;
DrawTexture( Rect( left, top, width, height ), tex );
在屏幕的Rect位置绘制tex。但是官方文档在Graphics的DrawTexture()函数介绍中又这么一句话
It's probably better to use GUI.DrawTexture for GUI code.
这么说来Graphics更适合于绘制2D元素,而且,更适合于动画,一张纹理上多帧元素。这个函数不是很容易用,参数较多而且有点怪异,但是用了之后会觉得十分的合理。这个方法有4个重载,只讲最复杂的一个。
static function DrawTexture (screenRect : Rect, texture : Texture, sourceRect : Rect, leftBorder : int, rightBorder : int, topBorder : int, bottomBorder : int, color : Color, mat : Material = null) : void
screenRect:这个参数就是在屏幕的以像素为单位。left,top为起始点,创建一个大小width,height的矩形。Rect( left, top, width, height );
sourceRect:就是纹理对象。
sourceRect:在纹理上取一个矩形,缩放后放到screenRect内,所以通常这两个大小一定要一样,如果要缩放仅缩放screenRect就行了。但是有2个重要的是,第一、这个矩形的单位是相对大小,跟uv一样,是百分比的,大小在 0-1 之内。第二、起始点为left,button。也就是说以左下角为起始点。
border:后面4个border以像素为单位,缩放值,暂且不管,全给0,也就是原始大小。
color:颜色,类型为 Color,RGBA格式,0-1(0/255.0 - 255/255.0),默认为 Color( 0.5, 0.5, 0.5, 0.5 );
mat : 材质,暂时不管。
此代码不能放在任意位置,也就说必须有约束条件。
you should only do that from EventType.Repaint events
就是说触发了Repaint消息,此函数才能执行,而这个消息必须在OnGUI() 内执行。像这样
var tex : Texture;
function OnGUI()
{
if( Event.current.type.Equals(EventType.Repaint) )
Graphics.DrawTexture(......);
}
转自:http://blog.163.com/long_wtf/blog/static/1855532702011102322816678/
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5