当前位置:网站首页>【Unity3D】GUI控件
【Unity3D】GUI控件
2022-07-06 02:41:00 【little_fat_sheep】
1 前言
Unity 3D 提供了 GUI、NGUI、UGUI 等图形系统,以增强玩家与游戏的交互性。GUI 代码需要在 OnGUI 函数中调用才能绘制,布局分为手动布局(GUI)和自动布局(GUILayout)。
- 手动布局:需要传人Rect 参数来指定屏幕绘制区域,通过 GUI 调用控件
- 自动布局:不需要传入 Rect 参数,自动在屏幕中布局,通过 GUILayout 调用控件
注意:屏幕坐标系以左上角为原点。
GUI 中主要包含以下控件:
- Label:绘制文本和图片
- Box:绘制一个图形框
- Button:绘制按钮,响应单击事件
- RepeatButton:绘制一个处理持续按下事件的按钮
- TextField:绘制一个单行文本输入框
- PasswordField:绘制一个秘密输入框
- TextArea:绘制一个多行文本输入框
- Toggle:绘制一个开关
- Toolbar:绘制工具条
- SelectionGrid:绘制一组网格按钮
- HorizontalSlider:绘制一个水平方向的滑动条
- VerticalSlider:绘制一个垂直方向的滑动条
- HorizontalScrollbar:绘制一个水平方向的滚动条
- VerticalScrollbar:绘制一个垂直方向的滚动条
- Window:绘制一个窗口,可以用于放置控件
2 GUI 控件
1)Label:绘制文本和图片
// 绘制文本
public static void Label(Rect position, string text, GUIStyle style)
// 绘制图片
public static void Label(Rect position, Texture image, GUIStyle style)
// 应用
GUI.Label(new Rect (10, 10, 100, 20), "Hello World!");
GUI.Label(new Rect (100, 100, texture.width, texture.height), texture);![]()
2)Box:绘制一个图形框
// 绘制带边框的文本
public static void Box(Rect position, string text, GUIStyle style)
// 绘制带边框图片
public static void Box(Rect position, Texture image, GUIStyle style)![]()
3)Button:绘制按钮,响应单击事件
// 绘制带文本的按钮,单击抬起时返回true
public static bool Button(Rect position, string text, GUIStyle style)
// 绘制带图片的按钮,单击抬起时返回true
public static bool Button(Rect position, Texture image, GUIStyle style)![]()
4)RepeatButton:绘制一个处理持续按下事件的按钮
// 绘制带文本的按钮,按住时持续返回true
public static bool RepeatButton(Rect position, string text, GUIStyle style)
// 绘制带图片的按钮,按住时持续返回true
public static bool RepeatButton(Rect position, Texture image, GUIStyle style)5)TextField:绘制一个单行文本输入框
// 绘制单行文本框
public static string TextField(Rect position, string text, int maxLength, GUIStyle style)
// 应用
private string str = "Hello World!";
private void OnGUI() {
str = GUI.TextField(new Rect (10, 10, 100, 20), str);
Debug.Log(str);
}![]()
6)PasswordField:绘制一个秘密输入框
// 绘制密码框,maskChar为显示的符号,通常为"*"号
public static string PasswordField(Rect position, string password, char maskChar, int maxLength, GUIStyle style)![]()
7)TextArea:绘制一个多行文本输入框
// 绘制多行文本输入框
public static string TextArea(Rect position, string text, int maxLength, GUIStyle style)
8)Toggle:绘制一个开关
// 绘制带文本的开关
public static bool Toggle(Rect position, bool value, string text, GUIStyle style)
// 绘制带图片的开关
public static bool Toggle(Rect position, bool value, Texture image, GUIStyle style)![]()
9)Toolbar:绘制工具条
// 绘制文本工具条
public static int Toolbar(Rect position, int selected, string[] texts, GUIStyle style)
// 绘制图片工具条
public static int Toolbar(Rect position, int selected, Texture[] images, GUIStyle style)
// 应用
int selected = GUI.Toolbar(new Rect (10, 10, 300, 50), 1, new string[]{"first", "second", "third", "four"});
10)SelectionGrid:绘制一组网格按钮
// 绘制文本网格按钮, xCount为水平按钮数
public static int SelectionGrid(Rect position, int selected, string[] texts, int xCount, GUIStyle style)
// 绘制图片网格按钮, xCount为水平按钮数
public static int SelectionGrid(Rect position, int selected, Texture[] images, int xCount, GUIStyle style)
// 应用
int selected = GUI.SelectionGrid(new Rect (10, 10, 100, 50), 1, new string[]{"first", "second", "third", "four"}, 2);
11)HorizontalSlider:绘制一个水平方向的滑动条
// 绘制水平滑动条, value: 滑动条显示值, leftValue: 滑动条左值, rightValue: 滑动条右值
public static float HorizontalSlider(Rect position, float value, float leftValue, float rightValue, GUIStyle slider, GUIStyle thumb)
// 应用
float process = GUI.HorizontalSlider(new Rect (10, 10, 100, 50), 9f, 5f, 10f);![]()
12)VerticalSlider:绘制一个垂直方向的滑动条
// 绘制垂直滑动条, value: 滑动条显示值, leftValue: 滑动条左值, rightValue: 滑动条右值
public static float VerticalSlider(Rect position, float value, float leftValue, float rightValue, GUIStyle slider, GUIStyle thumb)
// 应用
float process = GUI.VerticalSlider(new Rect (10, 10, 50, 100), 9f, 5f, 10f);![]()
13)HorizontalScrollbar:绘制一个水平方向的滚动条
// 绘制水平滚动条, value: 滑动条显示值, size: 活塞大小, leftValue: 滑动条左值, rightValue: 滑动条右值
public static float HorizontalScrollbar(Rect position, float value, float size, float leftValue, float rightValue, GUIStyle style)
// 应用
float process = GUI.HorizontalScrollbar(new Rect (10, 10, 100, 50), 7f, 3f, 5f, 10f);![]()
14)VerticalScrollbar:绘制一个垂直方向的滚动条
// 绘制垂直滚动条, value: 滑动条显示值, size: 活塞大小, leftValue: 滑动条左值, rightValue: 滑动条右值
public static float VerticalScrollbar(Rect position, float value, float size, float leftValue, float rightValue, GUIStyle style)
// 应用
float process = GUI.VerticalScrollbar(new Rect (10, 10, 100, 50), 7f, 3f, 5f, 10f);![]()
15)Window:绘制一个窗口,可以用于放置控件
// 绘制窗口
public static Rect Window(int id, Rect clientRect, WindowFunction func, Texture image, GUIStyle style)
public static Rect Window(int id, Rect clientRect, WindowFunction func, string text)
public static Rect Window(int id, Rect clientRect, WindowFunction func, Texture image)
public static Rect Window(int id, Rect clientRect, WindowFunction func, GUIContent content)
public static Rect Window(int id, Rect clientRect, WindowFunction func, string text, GUIStyle style)
public static Rect Window(int id, Rect clientRect, WindowFunction func, GUIContent title, GUIStyle style)3 GUILayout 控件
GUILayout 中也有 1) ~ 15) 中控件,但是不需要传入Rect 属性,以下列举部分控件的例子:
GUILayout.Label("Hello world");
GUILayout.Button("您好");
4 GUISkin
在 Assets 窗口右键,选择【Create → GUI Skin】,创建 GUISkin 资源,定制 GUI 控件的属性。

在代码中定义和使用 GUISkin 如下:
public GUISkin skin;
private void Awake() {
GUI.skin = skin;
}将编辑后的 GUISkin 资源文件拖拽到如下红框中,以实现自定义 GUI 控件显示效果。

边栏推荐
- RobotFramework入门(三)WebUI自动化之百度搜索
- Zero basic self-study STM32 wildfire review of GPIO use absolute address to operate GPIO
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23
- 3D drawing ()
- Zero foundation self-study STM32 - Review 2 - encapsulating GPIO registers with structures
- [postgraduate entrance examination English] prepare for 2023, learn list5 words
- Data preparation
- Trends in DDoS Attacks
- 模板_快速排序_双指针
- 2022 eye health exhibition, vision rehabilitation exhibition, optometry equipment exhibition, eye care products exhibition, eye mask Exhibition
猜你喜欢
![[untitled] a query SQL execution process in the database](/img/de/700ee20934fc2cd4a019f761148ef9.png)
[untitled] a query SQL execution process in the database
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19](/img/7c/f728e88ca36524f92c56213370399b.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19

主数据管理理论与实践

深度解析链动2+1模式,颠覆传统卖货思维?

Solution: attributeerror: 'STR' object has no attribute 'decode‘
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9](/img/ed/0edff23fbd3880bc6c9dabd31755ac.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9

Pure QT version of Chinese chess: realize two-man, man-machine and network games

A copy can also produce flowers
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21](/img/73/4050a592fdd99bf06e8fd853b157b6.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21

The third level of C language punch in
随机推荐
Patch NTP server at the beginning of DDoS counterattack
Redis skip table
米家、涂鸦、Hilink、智汀等生态哪家强?5大主流智能品牌分析
Referenceerror: primordials is not defined error resolution
SQL table name is passed as a parameter
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
ReferenceError: primordials is not defined错误解决
Shell脚本更新存储过程到数据库
球面透镜与柱面透镜
Differences and usage scenarios between TCP and UDP
LeetCode 103. Binary tree zigzag level order transverse - Binary Tree Series Question 5
Black high-end responsive website dream weaving template (adaptive mobile terminal)
Redis cluster deployment based on redis5
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
How to check the lock information in gbase 8C database?
大厂镜像库
剑指 Offer 29. 顺时针打印矩阵
RobotFramework入门(二)appUI自动化之app启动
Number conclusion LC skimming review - 1
Spherical lens and cylindrical lens