OnGUI
OnGUI是Unity中通过代码驱动的GUI系统
主要用来创建调试工具、创建自定义属性面板、创建新的Editor窗口和工具达到扩展编辑器效果。
OnGUI不建议使用在项目UI中。
布局上,坐标系原点在屏幕左上角。
https://docs.unity3d.com/Manual/gui-Controls.html
Box矩形框
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
void OnGUI()
{
GUI.Box(new Rect(10,10,100,90),"看这里!");
}
}
Button按钮
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
void OnGUI()
{
if (GUI.Button(new Rect(20, 40, 80, 20), "点这里!"))
{
Debug.Log("OK");
}
}
}
Label标签
标签是非交互式的。只供展览。无法单击或以其他方式移动。最好只显示信息。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
void OnGUI()
{
GUI.Label(new Rect(25, 25, 100, 30), "Label");
}
}
RepeatButton按住后会重复执行单击操作的按钮
点击按钮时,每帧执行一次。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
void OnGUI()
{
if (GUI.RepeatButton(new Rect(25, 25, 100, 30), "RepeatButton"))
{
Debug.Log("OK");
}
}
}
TextField单行输入框
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
private string textFieldString = "text field";
void OnGUI()
{
textFieldString = GUI.TextField(new Rect(25, 25, 100, 30), textFieldString);
}
}
TextArea多行输入框
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
private string textAreaString = "text area \ntext area";
void OnGUI()
{
textAreaString = GUI.TextArea(new Rect(25, 25, 100, 60), textAreaString);
}
}
Toggle单选框
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
private bool toggleBool = true;
void OnGUI()
{
toggleBool = GUI.Toggle(new Rect(25, 25, 100, 30), toggleBool, "Toggle");
}
}
Toolbar工具栏
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
private int toolbarInt = 0;
private string[] toolbarStrings = { "Toolbar1", "Toolbar2", "Toolbar3" };
void OnGUI()
{
toolbarInt = GUI.Toolbar(new Rect(25, 25, 250, 30), toolbarInt, toolbarStrings);
}
}
SelectionGrid网格选择形式
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
private int selectionGridInt = 0;
private string[] selectionStrings = { "Grid 1", "Grid 2", "Grid 3", "Grid 4" };
void OnGUI()
{
selectionGridInt = GUI.SelectionGrid(new Rect(25, 25, 300, 60), selectionGridInt, selectionStrings, 2);
}
}
HorizontalSlider水平滑动条
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
private float hSliderValue = 0.0f;
void OnGUI()
{
hSliderValue = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), hSliderValue, 0.0f, 10.0f);
}
}
VerticalSlider垂直滑动条
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
private float vSliderValue = 0.0f;
void OnGUI()
{
vSliderValue = GUI.VerticalSlider(new Rect(25, 25, 100, 30), vSliderValue, 10.0f, 0.0f);
}
}
HorizontalScrollbar水平滚动条
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
private float hScrollbarValue;
void OnGUI()
{
hScrollbarValue = GUI.HorizontalScrollbar(new Rect(25, 25, 100, 30), hScrollbarValue, 1.0f, 0.0f, 10.0f);
}
}
VerticalScrollbar垂直滚动条
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
private float vScrollbarValue;
void OnGUI()
{
vScrollbarValue = GUI.VerticalScrollbar(new Rect(25, 25, 100, 30), vScrollbarValue, 1.0f, 10.0f, 0.0f);
}
}
ScrollView滚动视图
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
private Vector2 scrollViewVector = Vector2.zero;
private string innerText = "I am inside the ScrollView";
void OnGUI()
{
// Begin the ScrollView
scrollViewVector = GUI.BeginScrollView(new Rect(25, 25, 100, 100), scrollViewVector, new Rect(0, 0, 400, 400));
// Put something inside the ScrollView
innerText = GUI.TextArea(new Rect(0, 0, 400, 400), innerText);
// End the ScrollView
GUI.EndScrollView();
}
}
Window窗口,控件的可拖动容器
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
private Rect windowRect = new Rect(20, 20, 120, 50);
void OnGUI()
{
windowRect = GUI.Window(0, windowRect, WindowFunction, "My Window");
}
void WindowFunction(int windowID)
{
// Draw any Controls inside the window here
Debug.Log("WindowFunction");
}
}
changed判断数据变化
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
private float hSliderValue = 0.0f;
void OnGUI()
{
hSliderValue = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), hSliderValue, 0.0f, 10.0f);
if (GUI.changed)
{
Debug.Log(hSliderValue);
}
}
}
BeginGroup/EndGroup
分组
组内成员坐标相对于组的坐标。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestOnGUI : MonoBehaviour
{
void OnGUI()
{
GUI.BeginGroup(new Rect(Screen.width/2-50,Screen.height/2-50,300,300));
GUI.Box(new Rect(0,0,200,200),"我的界面");
GUI.Button(new Rect(20, 50, 100, 30),"我是个按钮!");
GUI.EndGroup();
}
}
http://www.cnblogs.com/sanyejun/p/8483759.html
https://blog.csdn.net/u011480667/article/details/77542226