DrawPreviewTexture, DrawRect, DrawTextureAlpha

示例程序
说明绘制一个由预览贴图/普通颜色/含透明通道贴图填充的矩形区域

用法

public static void DrawPreviewTexture(Rect position, Texture image, Material mat = null, ScaleMode scaleMode = ScaleMode.StretchToFill, float imageAspect = 0, float mipLevel = -1);
public static void DrawRect(Rect rect, Color color);
public static void DrawTextureAlpha(Rect position, Texture image, ScaleMode scaleMode = ScaleMode.StretchToFill, float imageAspect = 0, float mipLevel = -1);

参数

position/rect

矩形区域的位置和尺寸。

image

要绘制的贴图。

mat

绘制贴图时使用的材质。

scaleMode

贴图不符合要绘图比例时缩放图像的模式。

imageAspect

用于源图像的宽高比。 取 0 则使用原始比例。

mipLevel

要采样的 mip 级别。 取负数时为正常采样纹理。

color

绘制矩形区域使用的颜色。

示例

void OnGUI () {
		EditorGUI.PrefixLabel (new Rect (5, 5, 100, 17), 0, new GUIContent ("Choose a color: "));
		_color = EditorGUI.ColorField (new Rect (200, 5, 200, 17), _color);
	
		EditorGUI.PrefixLabel (new Rect (5, 27, 100, 17), 1, new GUIContent ("Rect drawn: "));
		EditorGUI.DrawRect (new Rect (200, 27, 60, 60), _color);
	
		EditorGUI.PrefixLabel (new Rect (5, 150, 100, 17), 2, new GUIContent ("Add a texture: "));
		_texture = (Texture)EditorGUI.ObjectField (new Rect (200, 150, 60, 60), _texture, typeof(Texture), true);
	
		EditorGUI.PrefixLabel (new Rect (5, 215, 100, 17), 3, new GUIContent ("Add a material for preview: "));
		_material = (Material)EditorGUI.ObjectField (new Rect (200, 215, 200, 17), _material, typeof (Material), true);
	
		EditorGUI.PrefixLabel (new Rect (5, 237, 100, 17), 3, new GUIContent ("Texture with alpha: "));
		if (!_texture) {
				EditorGUI.DrawRect (new Rect (200, 237, 60, 60), Color.black);
		} else {
				EditorGUI.DrawTextureAlpha (new Rect (200, 237, 60, 60), _texture);
		}
	
		EditorGUI.PrefixLabel (new Rect (5, 302, 100, 17), 3, new GUIContent ("Texture preview: "));
		if (!_texture) {
				EditorGUI.DrawRect (new Rect (200, 302, 60, 60), Color.black);
		} else {
				EditorGUI.DrawPreviewTexture (new Rect (200, 302, 60, 60), _texture, _material);
		}
}