GUI 控件使用示例
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
public string ar_str = "";
public string pwd_str = "";
public bool tg_val = false;
void OnGUI (){
// 文本标签
GUI.Label(new Rect(10, 30, 100, 50), "label str");
// 按钮
if (GUI.Button (new Rect (10, 70, 100, 50), "Buttion")) {
print ("btn");
}
// 长按按钮
if (GUI.RepeatButton (new Rect (10, 130, 100, 50), "RepeatButton")) {
print("rpt btn");
}
// 输入框
ar_str = GUI.TextArea(new Rect(10, 190, 100, 30), ar_str);
// 密码输入框
pwd_str = GUI.PasswordField(new Rect(10, 230, 100, 30), pwd_str, '*');
// 选择按钮
tg_val = GUI.Toggle(new Rect(120, 30, 100, 30), tg_val, "item");
}
}
GUILayout 控件使用示例
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
public string ar_str = "";
public string pwd_str = "";
public bool tg_val = false;
void OnGUI (){
GUILayout.BeginVertical();
// 文本标签
GUILayout.Label("label str");
// 按钮
if (GUILayout.Button ("Buttion")) {
print ("btn");
}
// 长按按钮
if (GUILayout.RepeatButton ("RepeatButton")) {
print("rpt btn");
}
// 输入框
ar_str = GUILayout.TextArea(ar_str);
// 密码输入框
pwd_str = GUILayout.PasswordField(pwd_str, '*');
// 选择按钮
tg_val = GUILayout.Toggle(tg_val, "item");
GUILayout.EndVertical();
}
}
可以看到,GUI控件位置大小固定,GUILayout控件位置是相对的,大小根据内容自动适配。
Application
Application 常用类方法:
public static void LoadLevel(string name/int indxe) 加载场景
public static void CaptureScreen(string filename) 截屏
public static void OpenURL(string url) 打开链接
public static void Quit() 退出