发布时间:2025-12-10 11:37:08 浏览次数:6
Unity版本: 2019.4.26
Visual Studio版本: 2019
微软官方C#函数文档
Unity官方API文档(英文)
Unity官方API文档(中文)
Unity官方用户手册(中文)
Unity游戏蛮牛API文档(中文)
Unity教程-C语言中文网
Unity用户手册-unity圣典
Unity组件手册-unity圣典
Unity脚本手册-unity圣典
Unity(9)-TileMap瓦片地图
相关参考链接如下:
Unity中Rect类型详解-博客园
Unity 3D Label控件-C语言中文网
GUIContent-Unity官方API文档(中文)
Unity 3D ToolTip控件-C语言中文网
| content | 显示文本、图片,类型为GUIContent |
| image | 图片素材,类型为Texture |
| position | 文本放置的坐标及宽高,类型为四元组Rect |
| style | 样式,类型为GUIStyle |
| text | 文本内容,类型为字符串string |
| x | 横轴坐标,值为0时为屏幕最左侧,整个屏幕的宽度为Screen.width,类型为浮点型float |
| y | 纵轴坐标,值为0时为屏幕最上侧,整个屏幕的高度为Screen.height,类型为浮点型float |
| width | 矩形宽度(横轴x),类型为浮点型float |
| height | 矩形高度(纵轴y),类型为浮点型float |
将gui脚本绑定到Main Camera中
播放游戏
using System.Collections;using System.Collections.Generic;using UnityEngine;public class gui : MonoBehaviour{private void OnGUI(){GUI.Label(new Rect(25,25, 100, 30),"文本");}}将gui脚本绑定到Main Camera中
并引用图片素材
播放游戏
using System.Collections;using System.Collections.Generic;using UnityEngine;public class gui : MonoBehaviour{//引用public Texture img;private void OnGUI(){GUI.Label(new Rect(0,0, 100, 30),img);}}相关参考链接如下:
Unity3D TextField控件-C语言中文网
GUIStyle 界面样式-unity圣典
Unity中Rect类型详解-博客园
| GUI.TextField | 单行文本框,无法换行 |
| GUI.TextArea | 多行文本框 |
| GUI.PasswordField | 加密文本框,用指定的字符替换输入的字符,适用于输入密码的时候 |
| maxLenght | 可显示的最大字符个数,类型为整型int |
| position | 文本放置的坐标及宽高,类型为四元组Rect |
| style | 样式,类型为GUIStyle |
| text | 文本内容,类型为字符串string |
| maxLenght | 可显示的最大字符个数,类型为整型int |
| position | 文本放置的坐标及宽高,类型为四元组Rect |
| style | 样式,类型为GUIStyle |
| text | 文本内容,类型为字符串string |
| maskChar | 指定的替换字符,类型为字符char |
| maxLenght | 可显示的最大字符个数,类型为整型int |
| password | 文本内容,类型为字符串string |
| position | 文本放置的坐标及宽高,类型为四元组Rect |
| style | 样式,类型为GUIStyle |
| x | 横轴坐标,值为0时为屏幕最左侧,整个屏幕的宽度为Screen.width,类型为浮点型float |
| y | 纵轴坐标,值为0时为屏幕最上侧,整个屏幕的高度为Screen.height,类型为浮点型float |
| width | 矩形宽度(横轴x),类型为浮点型float |
| height | 矩形高度(纵轴y),类型为浮点型float |
绑定gui脚本至MainCamera摄像机
播放游戏
using System.Collections;using System.Collections.Generic;using UnityEngine;public class gui : MonoBehaviour{string txt = "这是一串文本";private void OnGUI(){GUI.TextField(new Rect(0,0,100,100),txt); }}多行文本、加密文本实现原理类似
using System.Collections;using System.Collections.Generic;using UnityEngine;public class gui : MonoBehaviour{string txt = "陕西考古人员最近在榆林市靖边县发现一处国内罕见的明代长城营堡遗址";private void OnGUI(){GUI.TextField(new Rect(0,0,100,100),txt,5); }}通过一个新的公有变量txt1来保存文本的内容,在运行游戏时在界面显示文本框,在脚本变量中显示txt1内容。
多行文本、加密文本实现原理类似
相关参考链接如下:
控件-Unity官方用户手册(中文)
Button和RepeatButton控件-CSDN
| Button | 基本的按钮,按下只响应一次 |
| RepeatButton | Button的扩展按钮,支持按下时每帧都可以响应 |
| content | 显示文本、图片,类型为GUIContent |
| image | 图片,类型为Texture |
| position | 按钮放置的坐标及宽高,类型为四元组Rect |
| style | 样式,类型为GUIStyle |
| text | 文本内容,类型为字符串string |
| content | 显示文本、图片,类型为GUIContent |
| image | 图片,类型为Texture |
| position | 按钮放置的坐标及宽高,类型为四元组Rect |
| style | 样式,类型为GUIStyle |
| text | 文本内容,类型为字符串string |
| x | 横轴坐标,值为0时为屏幕最左侧,整个屏幕的宽度为Screen.width,类型为浮点型float |
| y | 纵轴坐标,值为0时为屏幕最上侧,整个屏幕的高度为Screen.height,类型为浮点型float |
| width | 矩形宽度(横轴x),类型为浮点型float |
| height | 矩形高度(纵轴y),类型为浮点型float |
绑定gui脚本至MainCamera摄像机
播放游戏
using System.Collections;using System.Collections.Generic;using UnityEngine;public class gui : MonoBehaviour{private void OnGUI(){GUI.Button(new Rect(100,100,100,20),"按钮");}}
先将gui脚本添加到Main Camera摄像机,再引用两个素材
using System.Collections;using System.Collections.Generic;using UnityEngine;public class gui : MonoBehaviour{//引用public Texture img1;//未按下按钮时public Texture img2;//按下按钮时private void OnGUI(){if(GUI.RepeatButton(new Rect(100,100,60,20),img1))//按下按钮时{GUI.RepeatButton(new Rect(100,100,60,20), img2);//替换图片}}}| content | 显示文本、图片,类型为GUIContent |
| image | 图片,类型为Texture |
| position | 复选框放置的坐标及宽高,类型为四元组Rect |
| style | 样式,类型为GUIStyle |
| text | 文本内容,类型为字符串string |
| value | 复选框的状态,类型为布尔bool |
| x | 横轴坐标,值为0时为屏幕最左侧,整个屏幕的宽度为Screen.width,类型为浮点型float |
| y | 纵轴坐标,值为0时为屏幕最上侧,整个屏幕的高度为Screen.height,类型为浮点型float |
| width | 矩形宽度(横轴x),类型为浮点型float |
| height | 矩形高度(纵轴y),类型为浮点型float |
此时点击之后不会有效果
using System.Collections;using System.Collections.Generic;using UnityEngine;public class gui : MonoBehaviour{private void OnGUI(){GUI.Toggle(new Rect(100,100,100,20),false,"复选框");}}下图是unity的工具栏,通过Toolbar也可以实现同样的效果
Toolbar包含有多个按钮,用户一次只能选择其中的一个按钮来使用
| buttonSize | 按钮的大小,类型为GUI.ToolbarButtonSize |
| contents | 显示文本、图片,类型为GUIContent数组 |
| images | 图片,类型为Texture数组 |
| position | 每个按钮放置的坐标及宽高,类型为四元组Rect |
| selected | 当前按下的按钮索引,类型为整型int |
| style | 样式,类型为GUIStyle |
| x | 横轴坐标,值为0时为屏幕最左侧,整个屏幕的宽度为Screen.width,类型为浮点型float |
| y | 纵轴坐标,值为0时为屏幕最上侧,整个屏幕的高度为Screen.height,类型为浮点型float |
| width | 矩形宽度(横轴x),类型为浮点型float |
| height | 矩形高度(纵轴y),类型为浮点型float |
通过一个变量index来存储每一次用户按下按钮的索引值,并在下一帧更新工具栏
using System.Collections;using System.Collections.Generic;using UnityEngine;public class gui : MonoBehaviour{string[] txt = { "按钮1","按钮2","按钮3","按钮4"};int index = 0;private void OnGUI(){index = GUI.Toolbar(new Rect(0,100,200,20),index,txt);}}| contents | 显示文本、图片,类型为GUIContent数组 |
| images | 图片,类型为Texture数组 |
| position | 每个按钮放置的坐标及宽高,类型为四元组Rect |
| selected | 当前按下的按钮索引,类型为整型int |
| style | 样式,类型为GUIStyle |
| xCount | 横轴的按钮个数,类型为整型int |
| x | 横轴坐标,值为0时为屏幕最左侧,整个屏幕的宽度为Screen.width,类型为浮点型float |
| y | 纵轴坐标,值为0时为屏幕最上侧,整个屏幕的高度为Screen.height,类型为浮点型float |
| width | 矩形宽度(横轴x),类型为浮点型float |
| height | 矩形高度(纵轴y),类型为浮点型float |
-传入参数index值为0,表示第一个按钮被选中
将选中的按钮索引,又传入index,在下一帧时更新状态
using System.Collections;using System.Collections.Generic;using UnityEngine;public class gui : MonoBehaviour{string[] txt = { "按钮1","按钮2","按钮3","按钮4","按钮5"};int index = 0;//选择的按钮索引private void OnGUI(){index = GUI.SelectionGrid(new Rect(100,100,200,90),index,txt,2);}}| GUI.HorizontalSlider | 水平滑块 |
| GUI.VerticalSlider | 垂直滑块 |
| leftValue | 滑动区域左侧点,类型为浮点float |
| position | 每个按钮放置的坐标及宽高,类型为四元组Rect |
| rightValue | 滑动区域右侧点,类型为浮点float |
| slider | 滑动区域的样式,类型为GUIStyle |
| thumb | 滑块的样式,类型为GUIStyle |
| thumbExtent | 滑块触发事件的样式,类型为GUIStyle |
| value | 滑块的位置,类型为浮点float |
| bottomValue | 滑动区域的顶部,类型为浮点float |
| position | 每个按钮放置的坐标及宽高,类型为四元组Rect |
| slider | 滑动区域的样式,类型为GUIStyle |
| thumb | 滑块的样式,类型为GUIStyle |
| thumbExtent | 滑块触发事件的样式,类型为GUIStyle |
| topValue | 滑动区域的底部,类型为浮点float |
| value | 滑块的位置,类型为浮点float |
| x | 横轴坐标,值为0时为屏幕最左侧,整个屏幕的宽度为Screen.width,类型为浮点型float |
| y | 纵轴坐标,值为0时为屏幕最上侧,整个屏幕的高度为Screen.height,类型为浮点型float |
| width | 矩形宽度(横轴x),类型为浮点型float |
| height | 矩形高度(纵轴y),类型为浮点型float |
创建了一个起点为0,终点为10的滑动区域
传入的5为滑块创建时所处位置
用一个变量来控制滑块的位置,在下一帧更新滑块位置
垂直滑块和水平滑块的实现方法一样