DrawTextureWithTexCoords()的使用

来源:互联网 发布:温室效应 知乎 编辑:程序博客网 时间:2024/05/15 09:03

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

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

 

DrawTextureWithTexCoords(Rect position, Texture image, Rect texCoords, bool alphaBlend)

参数说明:

position: 屏幕坐标的具体位置

image: 显示用的纹理图片

texCoords: 当图片长宽比不适合给定范围长宽比的时候如何伸缩图像 (这点要注意)

alphaBlend: 是否启用默认的alpha渲染管线

 

 

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

 

0 0
原创粉丝点击