脚本语言:C#
附上一张图说明Unity GUI编程中可用的控件:(可能有遗漏)
下面列出一些例子来说明:
1、Groups :
在固定Layout模式中起到组织可用项的功能,它让你在屏幕的一个区域中包含多个控件。把定义的控件放在GUI.BeginGroup()和 GUI.EndGroup()这对函数中间,所有控件的位置坐标都以Groups的0坐标为起点,假如更改了group坐标,那么内部的控件也会跟随改变。
示例代码:
using UnityEngine; using System.Collections; public class Test3 : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void OnGUI(){ // 屏幕宽度和高度 int screenWidth = Screen.width; int screenHeight = Screen.height; // group组大小 int groundWidth = 120; int groundHeight = 150; // group组初始位置 int groupx = (screenWidth - groundWidth) / 2; int groupy = (screenHeight - groundHeight) / 2; GUI.BeginGroup( new Rect(groupx,groupy,groundWidth,groundHeight)); GUI.Box( new Rect(0,0,groundWidth,groundHeight), "Level Select"); if(GUI.Button( new Rect(10,30,100,30),"Level 1")) Debug.Log("Level 1"); if(GUI.Button( new Rect(10,70,100,30),"Level 2")) Debug.Log("Level 2"); if(GUI.Button(new Rect(10,110,100,30),"Level 3")) Debug.Log("Level 3"); GUI.EndGroup(); // 改变group坐标,group组的位置随之改变 groupx = (screenWidth - groundWidth) / 4; groupy = (screenHeight - groundHeight) / 4; GUI.BeginGroup( new Rect(groupx,groupy,groundWidth,groundHeight)); GUI.Box( new Rect(0,0,groundWidth,groundHeight), "Level Select"); if(GUI.Button( new Rect(10,30,100,30),"Level 1")) Debug.Log("Level 1"); if(GUI.Button( new Rect(10,70,100,30),"Level 2")) Debug.Log("Level 2"); if(GUI.Button(new Rect(10,110,100,30),"Level 3")) Debug.Log("Level 3"); GUI.EndGroup(); } }