DrawTextureWithTexCoords()的使用

一、新建一个脚本挂在主像机上


  1. using UnityEngine;
  2. using System.Collections;
  3. public class DrawTextureWithTexCoordsTest : MonoBehaviour {
  4. public Texture tex;
  5. void OnGUI () {
  6. //指定要显示在的屏幕区域
  7. Rect destRect = new Rect(50, 50, tex.width, tex.height);
  8. //指定要显示的图片区域
  9. Rect sourceRect = new Rect(0, 0, tex.width, tex.height);
  10. DrawTextureWithTexCoords(destRect, sourceRect, tex);
  11. }
  12. void DrawTextureWithTexCoords(Rect destRect, Rect sourceRect, Texture tex)
  13. {
  14. int tw, th;
  15. tw = tex.width;
  16. th = tex.height;
  17. //------------调整放缩比例------------------
  18. sourceRect.x = sourceRect.x / tw;
  19. //屏幕坐标系原点: 左上角
  20. //图片坐标系原点: 左下角
  21. //图片的Y轴与屏幕的Y转方向相反,这里需要颠倒一下(都以左上角为坐标原点)
  22. sourceRect.y = 1.0f - (sourceRect.y + sourceRect.height) / th;
  23. sourceRect.width = sourceRect.width / tw;
  24. sourceRect.height = sourceRect.height / th;
  25. //------------------------------------------
  26. GUI.DrawTextureWithTexCoords(destRect, tex, sourceRect, true);
  27. }
  28. }

DrawTextureWithTexCoords(Rect position, Texture image, Rect texCoords, bool alphaBlend)
【DrawTextureWithTexCoords()的使用】参数说明:
position: 屏幕坐标的具体位置
image: 显示用的纹理图片
texCoords: 当图片长宽比不适合给定范围长宽比的时候如何伸缩图像 (这点要注意)
alphaBlend: 是否启用默认的alpha渲染管线



如果是使用NGUI来做的话,那么我们就需要通过将Scale和UV Rectangle结合起来使用,具体使用方法参照赛车项目的档位加速条的制作


    推荐阅读