1 按钮生成
view plaincopy
function OnGUI () {
if (GUI.Button (Rect (10,10,150,100), “I am a button”)) {
print (“You clicked the button!”);
}
}
2 按钮场景载入
view plaincopy
/* Example level loader */
function OnGUI () {
// Make a background box
GUI.Box (Rect (10,10,100,90), “Loader Menu”);
// Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
if (GUI.Button (Rect (20,40,80,20), “Level 1”)) {
Application.LoadLevel (1);
}
// Make the second button.
if (GUI.Button (Rect (20,70,80,20), “Level 2”)) {
Application.LoadLevel (2);
}
}
3 按钮点击生效时间
view plaincopy
/* Flashing button example */
function OnGUI () {
if (Time.time % 2 < 1) {
if (GUI.Button (Rect (10,10,200,20), “Meet the flashing button”)) {
print (“You clicked me!”);
}
}
}
4 创建按钮背景BOX
view plaincopy
/* Screen.width & Screen.height example */
function OnGUI () {
GUI.Box (Rect (0,0,100,50), “Top−left”);
//Rect 生成2D矩形的函数,用于摄像机,画面,GUI
GUI.Box (Rect (Screen.width − 100,0,100,50), “Top−right”);
GUI.Box (Rect (0,Screen.height − 50,100,50), “Bottom−right”);
GUI.Box (Rect (Screen.width − 100,Screen.height − 50,100,50), “Bottom−left”);
}
5 在按钮上显示文字
view plaincopy
/* String Content example */
function OnGUI () {
GUI.Label (Rect (0,0,100,50), “This is the text string for a Label Control”);
//显示文字
}
6 显示图像,声明一个公共变量的Texture2D,并通过这样的内容作为参数变量的名称
view plaincopy
/* Texture2D Content example */
var controlTexture : Texture2D; //controlTexture为图像的名称
function OnGUI () {
GUI.Label (Rect (0,0,100,50), controlTexture);
}
7 显示图像的例子
view plaincopy
/* Button Content examples */
var icon : Texture2D;
function OnGUI () {
if (GUI.Button (Rect (10,10, 100, 50), icon)) {
print (“you clicked the icon”);
}
if (GUI.Button (Rect (10,70, 100, 20), “This is text”)) {
print (“you clicked the text button”);
}
}
8 显示在一个图形用户界面控制的图像和文字在一起。可以为内容提供一个参数GUIContent对象,并定义字符串和图像显示的是在GUIContent。
view plaincopy
/* Using GUIContent to display an image and a string */
var icon : Texture2D;
function OnGUI () {
GUI.Box (Rect (10,10,100,50), GUIContent(“This is text”, icon));
}