一、新建一个脚本挂在主像机上
- using UnityEngine;
- using System.Collections;
- public class DrawTextureWithTexCoordsTest : MonoBehaviour {
- public Texture tex;
- void OnGUI () {
- //指定要显示在的屏幕区域
- Rect destRect = new Rect(50, 50, tex.width, tex.height);
- //指定要显示的图片区域
- Rect sourceRect = new Rect(0, 0, tex.width, tex.height);
- DrawTextureWithTexCoords(destRect, sourceRect, tex);
- }
- void DrawTextureWithTexCoords(Rect destRect, Rect sourceRect, Texture tex)
- {
- int tw, th;
- tw = tex.width;
- th = tex.height;
- //------------调整放缩比例------------------
- sourceRect.x = sourceRect.x / tw;
- //屏幕坐标系原点: 左上角
- //图片坐标系原点: 左下角
- //图片的Y轴与屏幕的Y转方向相反,这里需要颠倒一下(都以左上角为坐标原点)
- sourceRect.y = 1.0f - (sourceRect.y + sourceRect.height) / th;
- sourceRect.width = sourceRect.width / tw;
- sourceRect.height = sourceRect.height / th;
- //------------------------------------------
- GUI.DrawTextureWithTexCoords(destRect, tex, sourceRect, true);
- }
- }
DrawTextureWithTexCoords(Rect position, Texture image, Rect texCoords, bool alphaBlend)
【DrawTextureWithTexCoords()的使用】参数说明:
position: 屏幕坐标的具体位置
image: 显示用的纹理图片
texCoords: 当图片长宽比不适合给定范围长宽比的时候如何伸缩图像 (这点要注意)
alphaBlend: 是否启用默认的alpha渲染管线
如果是使用NGUI来做的话,那么我们就需要通过将Scale和UV Rectangle结合起来使用,具体使用方法参照赛车项目的档位加速条的制作
推荐阅读
- 游戏|元宇宙密室逃脱游戏攻略来啦!
- Unity|unity 一些控制物体移动 小技巧
- Unity|Unity 学习 SpaceShooter 第二次记录
- Unity|Unity 游戏开始UI设计
- Unity|用C#写题--如何读入一行按空格分开的整数
- Visual Studio Professional 2017 点击项目->属性 提示(尝试加载该页时出错)
- Unity|【Unity Shader】Blend混合
- unity|AR Shadow Shader 实时阴影+ DepthMask透明遮罩
- Unity|unity3d知识点之一
- Unity|绿幕背景抠图,去除掉物体周围一圈的绿边 OpenCVForUnity