12.总结
12.1 知识点
学习的主要内容
优点
缺点
主要用处
12.2 核心要点速览
文本控件(Label)
作用:生成文本控件。
重载形式:
public static void Label(Rect position, string text);
public static void Label(Rect position, Texture image);
public static void Label(Rect position, GUIContent guiContent);
public static void Label(Rect position, string text, GUIStyle style);
public static void Label(Rect position, Texture image, GUIStyle style);
public static void Label(Rect position, GUIContent guiContent, GUIStyle style);
使用示例:
GUI.Label(new Rect(100, 20, 100, 20), "666666欢迎你"); GUI.Label(labelRect1, image); GUI.Label(labelRect2, guiContent); GUI.Label(new Rect(500, 200, 500, 500), "林文韬欢迎你", labelGuiStyle);
按钮控件
普通按钮(Button)
作用:创建单击按钮,点击时执行操作,返回布尔值。
重载形式:与 Label 类似,有多种参数组合。
使用示例:
if (GUI.Button(btnRect, btnGUIContent1, btnGuiStyle)) { Debug.Log("按钮被点击"); }
长按按钮(RepeatButton)
作用:用户按住时一直处于激活状态,返回布尔值。
使用示例:
if (GUI.RepeatButton(new Rect(300, 60, 100, 20), btnGUIContent2)) { Debug.Log("长按按钮被点击"); }
多选框(Toggle)
作用:创建打开/关闭的开关按钮,需传入布尔值表示选中状态,返回改变后的选中状态。
重载形式:与 Label 类似,有多种参数组合。
使用示例:
isSel = GUI.Toggle(new Rect(0, 0, 100, 30), isSel, "效果开关"); isSel2 = GUI.Toggle(new Rect(0, 40, 100, 30), isSel2, "音效开关", style);
单选框(基于 Toggle 实现)
关键:通过一个 int 标识决定是否选中,改变 int 值。
使用示例:
if (GUI.Toggle(new Rect(0, 100, 100, 30), nowSelIndex == 1, "选项一")) { nowSelIndex = 1; } if (GUI.Toggle(new Rect(0, 140, 100, 30), nowSelIndex == 2, "选项二")) { nowSelIndex = 2; } if (GUI.Toggle(new Rect(0, 180, 100, 30), nowSelIndex == 3, "选项三")) { nowSelIndex = 3; }
输入框
普通输入框(TextField)
作用:创建可供用户编辑字符串的单行文本字段。
使用示例:
inputStr = GUI.TextField(new Rect(0, 0, 100, 30), inputStr, 5);
密码输入框(PasswordField)
作用:创建可让用户输入密码的文本字段。
使用示例:
inputPW = GUI.PasswordField(new Rect(0, 50, 100, 30), inputPW, '★');
拖动条
水平拖动条(HorizontalSlider)
作用:用户可拖动的水平滑动条,用于在最小值和最大值之间更改某值。
使用示例:
nowValue = GUI.HorizontalSlider(new Rect(0, 100, 100, 50), nowValue, 0, 1);
竖直拖动条(VerticalSlider)
作用:用户可拖动的垂直滑动条,用于在最小值和最大值之间更改某值。
使用示例:
nowValue = GUI.VerticalSlider(new Rect(0, 150, 50, 100), nowValue, 0, 1);
图片绘制(DrawTexture)
作用:在一个矩形内绘制纹理。
重载形式:
public static void DrawTexture(Rect position, Texture image);
public static void DrawTexture(Rect position, Texture image, ScaleMode scaleMode);
public static void DrawTexture(Rect position, Texture image, ScaleMode scaleMode, bool alphaBlend);
public static void DrawTexture(Rect position, Texture image, ScaleMode scaleMode, bool alphaBlend, float imageAspect);
参数说明:
- ScaleMode:图像宽高比不适合绘制宽高比时的缩放方式,包括 ScaleAndCrop(计算后裁剪)、ScaleToFit(保持完整显示)、StretchToFill(填充满整个区域)。
- alphaBlend:绘制图像时是否应用 Alpha 混合,控制透明通道。
- imageAspect:源图像宽高比,为 0 时使用图像默认宽高比。
使用示例:
GUI.DrawTexture(texPos, tex, mode, alpha, wh);
框绘制(Box)
作用:在 GUI 层上创建一个框。
使用示例:
GUI.Box(texPos, "123");
工具栏(Toolbar)
作用:创建一个工具栏,需要一个 int 用于记录当前索引和一个 string 数组用于显示内容。返回新的选中索引。
示例:
int toolbarIndex = 0; string[] toolbarInfos = { "选项1", "选项2", "选项3" }; toolbarIndex = GUI.Toolbar(new Rect(0, 0, 200, 30), toolbarIndex, toolbarInfos); switch (toolbarIndex) { case 0: // 逻辑处理 break; case 1: // 逻辑处理 break; case 2: // 逻辑处理 break; }
选择网格(SelectionGrid)
作用:创建一个按钮网格,需要一个 int 索引、string 数组内容和 xCount(每行按钮数)。
示例:
int selGridIndex = 0; string[] gridInfos = { "选项1", "选项2", "选项3" }; selGridIndex = GUI.SelectionGrid(new Rect(0, 50, 200, 60), selGridIndex, gridInfos, 2);
分组(BeginGroup 和 EndGroup)
作用:批量控制控件位置,类似父对象容器。
示例:
GUI.BeginGroup(groupRect); GUI.Button(new Rect(0, 0, 100, 50), "测试按钮"); GUI.Label(new Rect(0, 60, 100, 20), "Label信息"); GUI.EndGroup();
滚动列表(BeginScrollView 和 EndScrollView)
作用:创建可滚动视图。
参数说明:
- position:滚动区域位置和大小。
- scrollPosition:当前滚动偏移,需要赋值。
- viewRect:内容区域边界。
示例:
nowPos = GUI.BeginScrollView(scRect, nowPos, showRect); // 内容 GUI.EndScrollView();
窗口
普通窗口(Window)
作用:创建弹出窗口。
参数:
- id:窗口唯一标识。
- position:位置和大小。
- function:绘制内容的回调。
示例:
private void OnGUI() { GUI.Window(1, new Rect(100, 100, 200, 150), DrawWindow, "测试窗口"); GUI.Window(2, new Rect(100, 250, 200, 150), DrawWindow, "测试窗口2"); } private void DrawWindow(int id) { switch (id) { case 1: GUI.Button(new Rect(0, 30, 30, 20), "1"); break; case 2: GUI.Button(new Rect(0, 30, 30, 20), "2"); break; case 3: GUI.Button(new Rect(0, 30, 30, 20), "3"); break; } }
模态窗口(ModalWindow)
作用:显示模态窗口,阻止其他控件交互,常用于警告等。
示例:
GUI.ModalWindow(3, new Rect(300, 100, 200, 150), DrawWindow, "模态窗口");
拖动窗口
作用:实现窗口可拖动。
示例:
dragWinPos = GUI.Window(4, dragWinPos, DrawWindow, "拖动窗口"); private void DrawWindow(int id) { switch (id) { case 4: GUI.DragWindow(new Rect(0, 0, 1000, 20)); break; } }
全局颜色和皮肤样式
全局颜色
GUI.color:全局着色。
GUI.contentColor:文本着色,与 GUI.color 相乘。
GUI.backgroundColor:背景着色,与 GUI.color 相乘。
示例:
GUI.color = Color.red; GUI.Label(new Rect(0, 50, 150, 30), "全局着色颜色标签红"); GUI.contentColor = Color.yellow; GUI.Button(new Rect(0, 200, 150, 30), "文本着色颜色"); GUI.backgroundColor = Color.green; GUI.Button(new Rect(0, 250, 150, 30), "背景元素着色颜色", style);
整体皮肤样式
创建:在 Project 窗口右键创建 GUI 皮肤文件。
使用:通过
GUI.skin
应用全局皮肤,但传入 GUIStyle 参数时优先使用该样式。示例:
GUI.color = Color.white; GUI.contentColor = Color.white; GUI.backgroundColor = Color.white; GUI.skin = mySkin; GUI.Button(new Rect(200, 0, 100, 30), "测试按钮1"); GUI.Button(new Rect(200, 50, 100, 30), "测试按钮2", style); GUI.skin = null; GUI.Button(new Rect(200, 100, 100, 30), "测试按钮3");
GUILayout 自动布局
特点:类似 GUI 类,但自动计算布局,常用于编辑器扩展。
示例:
GUI.BeginGroup(new Rect(100, 100, 500, 300)); GUILayout.BeginArea(new Rect(10, 10, 400, 300)); GUILayout.BeginVertical(); GUILayout.Button("竖直123", GUILayout.Width(200)); GUILayout.Button("竖直245666656565"); GUILayout.Button("竖直235", GUILayout.ExpandWidth(false)); GUILayout.EndVertical(); GUILayout.BeginHorizontal(); GUILayout.Button("水平123", GUILayout.Height(300)); GUILayout.Button("水平245666656565"); GUILayout.Button("水平235", GUILayout.ExpandWidth(false)); GUILayout.EndHorizontal(); GUILayout.EndArea(); GUI.EndGroup();
GUILayoutOption 布局选项
- GUILayout.Width, GUILayout.Height:设置固定宽高。
- GUILayout.MinWidth, GUILayout.MinHeight:设置最小宽高。
- GUILayout.MaxWidth, GUILayout.MaxHeight:设置最大宽高。
- GUILayout.ExpandWidth, GUILayout.ExpandHeight:允许或禁止横向、纵向扩展。
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com