Unity学习笔记(十八)GUI控件(二)
2026/6/27 3:13:21 网站建设 项目流程

输入框

普通输入 inputStr 为默认的显示内容, 10为最大输入长度 可自定义。返回值为String

inputStr = GUI.TextField(new Rect(0, 20, 100, 30), inputStr, 10);

密码输入 用*号替换密码密码

inputPwd = GUI.PasswordField(new Rect(0, 50, 100, 30), inputPwd, '*');

拖动条

nowValue的值从0到1变化 不传值不变化 要nowValue=

水平

nowValue = GUI.HorizontalSlider(new Rect(0, 100, 100, 50), nowValue, 0, 1); Debug.Log(nowValue);

垂直

nowValue = GUI.VerticalSlider(new Rect(100, 200, 100, 50), nowValue, 0, 1); Debug.Log(nowValue);

图片绘制

public Rect texPos; public Rect textPos; public Texture Texture; public ScaleMode mode=ScaleMode.StretchToFill;

ScaleAndCrop:也会通过宽高比来计算图片,但是 会进行裁剪
ScaleToFit 会自动根据宽高比进行计算,不会拉变形,会一直保持图片完全显示的状态
StretchToFill 始终填充满你填入的Rect范围

透明不透明

public bool alpha=true;

imageAspect自定义宽高比

public float wh = 0;

绘制方法

GUI.DrawTexture(texPos, Texture, mode, alpha, wh);

框绘制

GUI.Box(textPos,"");

工具栏

帮助我们根据不同的返回所 来处理不同的逻辑

private int toolbarIndex = 0; private string[] toolbarInfos = new string[] { "1", "2", "3" };
toolbarIndex=GUI.Toolbar(new Rect(0,0,100,30),toolbarIndex,toolbarInfos); switch (toolbarIndex) { case 0: break; case 1: break; case 2: break; }

网格选择器

private int selGridIndex = 0; private int xCount = 3;

xCount代表 水平方向最多显示的按钮数量

selGridIndex=GUI.SelectionGrid(new Rect(0, 50, 200, 30),selGridIndex,toolbarInfos,xCount);

分组

用于批量控制控件位置
可以理解为包裹着的控件加了一个父对象
可以通过控制分组来包裹控件的位置

public Rect groupPos; GUI.BeginGroup(groupPos); GUI.Button(new Rect(0, 0, 100, 50), "TEST"); GUI.Label(new Rect(0, 110, 100, 50), "Test"); GUI.EndGroup();

滚动列表

public Rect scrollPos; public Rect showPos; private Vector2 nowPows; public string[] strs; nowPows=GUI.BeginScrollView(scrollPos,nowPows,showPos); for(int i = 0; i < strs.Length; i++) { GUI.Label(new Rect(0, i * 30, 100, 30), strs[i]); } GUI.EndScrollView();

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询