using UnityEngine;
using System.Collections;
public class DrawTextureTest : MonoBehaviour {
public Texture aTexture;
void OnGUI()
{
if (null == aTexture)
{
Debug.LogError("Assign a Texture in the inspector.");
return;
}
//参数: 绘制矩形, 绘制纹理, 缩放模式, 是否启用Alpha混合, 自定义宽高比(宽/高)
//缩放模式
//StretchToFill : 拉伸纹理,来填充整个矩形。
//ScaleAndCrop : 缩放纹理,保持高宽比,使它完全覆盖整个矩形。如果纹理被绘制到一个和原始纹理不同宽高比的矩形,超出矩形部分的图像将被裁切。
//ScaleToFit : 缩放纹理,保持高宽比,以便它完全适配在矩形内的位置。
//如果不指定自定义宽高比,则默认采用实际的纹理宽高比。
GUI.DrawTexture(new Rect(10, 10, 220, 150), aTexture, ScaleMode.ScaleToFit, true, 2.0F);
}
}
运行效果