Unity学习笔记(十七)GUI控件(一)
2026/5/29 4:44:13 网站建设 项目流程

文本控件

public Texture pic; public Rect rect1; public GUIContent gUIContent; public GUIStyle style;

style 自定义文字样式

GUI.Label(new Rect(0,0,100,20),"Welcome",style);

Rect的宽高会改变图片显示的大小,而且是按图片等比例放大的

GUI.Label(new Rect(0, 30, 100, 100), pic);

综合使用 同时显示 文字和图片

GUI.Label(rect, gUIContent);

获取当前鼠标或者键盘选中的GUI控件 对应的tooltip信息

Debug.Log(GUI.tooltip);

按钮组件

public Rect buttonRect; public GUIContent buttonContent; public GUIStyle buttonStyle;

基本使用 鼠标在按钮范围内按下并抬起才算一次点击

if(GUI.Button(buttonRect, buttonContent, buttonStyle)) { Debug.Log("被点击"); }

长按 按钮 按下就会响应点击 抬起才停止

if (GUI.RepeatButton(buttonRect, buttonContent)) { Debug.Log("长按点击"); }

多选框

private bool isSel; private bool isSel2; public GUIStyle style;

Toggle的固定写法 ,这样可以根据返回值切换自己的状态

isSel=GUI.Toggle(new Rect(0, 0, 100, 30), isSel, "效果开关");

自定义样式 显示问题

修改固定宽高 fixedWidth和fixedHeight 对背景图片进行宽高的设置 同时不影响点击区域

修改从GUIStyle边缘到内容起始处的空间 padding

isSel2=GUI.Toggle(new Rect(0,40,100,30),isSel2,"Music",style);

单选框

private int nowSelIndex = 1;

单选框基于多选框去实现 关键 通过一个int标识来决定是否选中

if(GUI.Toggle(new Rect(0, 60, 100, 30), nowSelIndex==1, "Selection1")) { nowSelIndex = 1; } if(GUI.Toggle(new Rect(0,100,100,30), nowSelIndex == 2, "Selection2")) { nowSelIndex=2; } if(GUI.Toggle(new Rect(0,140,100,30), nowSelIndex == 3, "Selection3")) { nowSelIndex=3; }

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

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

立即咨询