所有GUI组件都必须写在OnGUI函数。
GUI组件所用到的图片需将Texture Type改为 Editor GUI and Legacy GUI,否则图片会有拉伸
GUI.Label
public static void Label(Rect position, string text);
public static void Label(Rect position, Texture image);
public static void Label(Rect position, GUIContent content);
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 content, GUIStyle style);
void OnGUI() {
GUI.Label(new Rect(10, 10, 100, 20), "Hello World!");
}
GUI.Box
public static void Box(Rect position, string text);
public static void Box(Rect position, Texture image);
public static void Box(Rect position, GUIContent content);
public static void Box(Rect position, string text, GUIStyle style);
public static void Box(Rect position, Texture image, GUIStyle style);
public static void Box(Rect position, GUIContent content, GUIStyle style);
GUI.Button
每次点击只响应一次点击事件
GUI.RepeatButton
只要点击后不松开则一直响应点击事件
GUI.TextField
必须要有输入值和返回值,否则内容不会根据键盘输入而变化
public string text = "This is TextField";
void OnGUI()
{
text = GUI.TextField(new Rect(0, 0, 100, 20), text);
}
GUI.PasswordField
public string text = "This is TextField";
void OnGUI()
{
text = GUI.PasswordField(new Rect(0, 0, 100, 20), text, '*');
}
GUI.TextArea
public string text = "This is TextField";
void OnGUI()
{
text = GUI.TextArea(new Rect(0, 0, 100, 100), text);
}
例:登陆窗口
private string strUserName = string.Empty;
private string strPWD = string.Empty;
private string strMSG = string.Empty;
void OnGUI()
{
GUI.Label(new Rect(0, 0, 100, 20), "UserName :");
GUI.Label(new Rect(0, 30, 100, 20), "Password : ");
strUserName = GUI.TextField(new Rect(120, 0, 100, 20), strUserName);
strPWD = GUI.PasswordField(new Rect(120, 30, 100, 20), strPWD, '*');
if(GUI.Button(new Rect(30, 60, 100, 20), "Login"))
{
strMSG = "Hello";
}
GUI.Label(new Rect(30, 90, 100, 20), strMSG);
}
GUI.Toggle
bool isCheck;
void OnGUI()
{
isCheck = GUI.Toggle(new Rect(0, 0, 100, 20), isCheck, "Toggle");
}
GUI.Toolbar
int selectedIndex = 0;
void OnGUI()
{
selectedIndex = GUI.Toolbar(new Rect(0, 0, 100, 20), selectedIndex, new string[] {"Tool1", "Tool2", "Tool3"});
}
GUI.SelectionGrid
int selectedIndex = 0;
void OnGUI()
{
selectedIndex = GUI.SelectionGrid(new Rect(0, 0, 100, 100), selectedIndex, new string[] {"Tool1", "Tool2", "Tool3"}, 1);
}
GUI.HorizontalSlider
float selectedValue = 0;
void OnGUI()
{
selectedValue = GUI.HorizontalSlider(new Rect(0, 0, 100, 20), selectedValue, -100f, 100f);
}
GUI.HorizontalScrollbar
float selectedValue = 0;
void OnGUI()
{
selectedValue = GUI.HorizontalScrollbar(new Rect(0, 0, 100, 20), selectedValue, 20f, -100f, 100f);
}
GUI.BeginScrollView
GUI.EndScrollView
public Rect area;
Vector2 position = new Vector2(0, 0);
void OnGUI()
{
position = GUI.BeginScrollView(area, position, new Rect(0, 0, 100, 100));
GUI.Button(new Rect(0, 0, 100, 20), "Button1");
GUI.Button(new Rect(0, 30, 100, 20), "Button2");
GUI.Button(new Rect(0, 60, 100, 20), "Button3");
GUI.Button(new Rect(0, 90, 100, 20), "Button4");
GUI.EndScrollView();
}
GUI.Window
public Rect area;
void OnGUI()
{
area = GUI.Window(0, area, windfuc, "Window");
}
void windfuc(int windowID)
{
GUI.Button(new Rect(20, 20, 100, 20), "Button");
GUI.DragWindow();
}
GUILayout.BeginArea
GUILayout.EndArea
void OnGUI()
{
GUILayout.BeginArea(new Rect(10, 10, 100, 100));
GUILayout.Button("Click me");
GUILayout.Button("Or me");
GUILayout.EndArea();
}
例:组合使用
public Rect area = new Rect(20, 20, 200, 200);
void OnGUI()
{
GUILayout.BeginArea(area);
GUILayout.BeginHorizontal();
#region Vertical A
GUILayout.BeginVertical();
GUILayout.Box("Text1");
//控件与控件之间空隙一个距离
GUILayout.Space(20f);
GUILayout.Box("Text1");
GUILayout.EndVertical();
#endregion
#region Vertical B
GUILayout.BeginVertical();
GUILayout.Box("Text3");
//自动测算空隙距离
GUILayout.FlexibleSpace();
GUILayout.Box("Text4");
GUILayout.EndVertical();
#endregion
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
public GUISkin skin;
private string strUserName = string.Empty;
private string strPWD = string.Empty;
private string strMSG = string.Empty;
private Rect area;
void Start()
{
area = new Rect(20, 20, 400, 400);
}
void OnGUI()
{
GUI.skin = skin;
area = GUI.Window(0, area, windfuc, "Login");
}
void windfuc(int windowID)
{
GUI.Label(new Rect(40, 100, 320, 30), "UserName");
GUI.Label(new Rect(40, 190, 320, 30), "Password");
strUserName = GUI.TextField(new Rect(60, 150, 280, 20), strUserName);
strPWD = GUI.PasswordField(new Rect(60, 240, 280, 20), strPWD, '*', skin.textField);
GUI.Button(new Rect(150, 280, 100, 40), "Login");
GUI.DragWindow();
}