GUI 界面
界面类是Unity手工定制的GUI的接口
GUI.backgroundColor 背景颜色
全局染色由GUI渲染的所有背景元素,得到乘以颜色
void OnGUI() {
GUI.backgroundColor = Color.yellow;
GUI.Button(new Rect(10, 10, 70, 30), "A button");
}
GUI.BeginGroup 开始组
开始组,必须配套以EndGroup结束关闭容器
void OnGUI() {
GUI.BeginGroup(new Rect(Screen.width / 2 - 400, Screen.height / 2 - 300, 800, 600));
GUI.Box(new Rect(0, 0, 800, 600), "This box is now centered! - here you would put your main menu");
GUI.EndGroup();
}
GUI.BeginScrollView 开始滚动视图
在你的GUI里,开始一个滚动视图, 注意BeginScrollView和EndScrollView它们是成对出现的
public Vector2 scrollPosition = Vector2.zero;
void OnGUI() {
scrollPosition = GUI.BeginScrollView(new Rect(10, 300, 100, 100), scrollPosition, new Rect(0, 0, 220, 200));
GUI.Button(new Rect(0, 0, 100, 20), "Top-left");
GUI.Button(new Rect(120, 0, 100, 20), "Top-right");
GUI.Button(new Rect(0, 180, 100, 20), "Bottom-left");
GUI.Button(new Rect(120, 180, 100, 20), "Bottom-right");
GUI.EndScrollView();
}
GUI.Box 盒子
在界面布局创建盒子。盒子包含文本,图像或者带有工具提示的这些的组合,通过使用GUIContent参数。你也可以使用GUIStyle去调整盒子中所有的物体的布局,文本颜色和其他属性
void OnGUI() {
GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "This is a box");
}
GUI.BringWindowToBack 使窗口到后面
使一个特定的窗口到浮动窗口的后面
private Rect windowRect = new Rect(20, 20, 120, 50);
private Rect windowRect2 = new Rect(80, 20, 120, 50);
void OnGUI() {
windowRect = GUI.Window(0, windowRect, DoMyFirstWindow, "First");
windowRect2 = GUI.Window(1, windowRect2, DoMySecondWindow, "Second");
}
void DoMyFirstWindow(int windowID) {
if (GUI.Button(new Rect(10, 20, 100, 20), "Put Back"))
GUI.BringWindowToBack(0);
GUI.DragWindow(new Rect(0, 0, 10000, 20));
}
void DoMySecondWindow(int windowID) {
if (GUI.Button(new Rect(10, 20, 100, 20), "Put Back"))
GUI.BringWindowToBack(1);
GUI.DragWindow(new Rect(0, 0, 10000, 20));
}
GUI.BringWindowToFront 使窗口到前面
使一个特定的窗口到浮动窗口的前面
private Rect windowRect = new Rect(20, 20, 120, 50);
private Rect windowRect2 = new Rect(80, 20, 120, 50);
void OnGUI() {
windowRect = GUI.Window(0, windowRect, DoMyFirstWindow, "First");
windowRect2 = GUI.Window(1, windowRect2, DoMySecondWindow, "Second");
}
void DoMyFirstWindow(int windowID) {
if (GUI.Button(new Rect(10, 20, 100, 20), "Bring to front"))
GUI.BringWindowToFront(1);
GUI.DragWindow(new Rect(0, 0, 10000, 20));
}
void DoMySecondWindow(int windowID) {
if (GUI.Button(new Rect(10, 20, 100, 20), "Bring to front"))
GUI.BringWindowToFront(0);
GUI.DragWindow(new Rect(0, 0, 10000, 20));
}
GUI.Button 按钮
创建一个单次按下按钮。用户点击按钮事件立即触发
public Texture btnTexture;
void OnGUI() {
if (!btnTexture) {
Debug.LogError("Please assign a texture on the inspector");
return;
}
if (GUI.Button(new Rect(10, 10, 50, 50), btnTexture))
Debug.Log("Clic