标签:

控件组可以看成一个大的容器,控件组里面的控件,相对位置已该控件组为基准,而不再已屏幕左上角为基准。
下面来看一下代码实例及其效果截图:
public class GUI2 : MonoBehaviour {
int toolbarInt=0;//代表默认第n-1个按钮是激活的
string[] toolbarstring={"工具","窗体","帮助"};//按钮名称个数和集合
int selectgrid=0;
string[] selectgridsring = {"grid 1","grid 2","grid 3","grid 4","grid 5"};
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
GUILayout.Button ("这是一个Gui按钮");
GUI.BeginGroup (new Rect (Screen.width/2-50, Screen.height/2-50, 600, 600));
toolbarInt = GUI.Toolbar (new Rect (0, 0, 220, 40), toolbarInt, toolbarstring);
selectgrid = GUI.SelectionGrid (new Rect (150, 60, 250, 80), selectgrid, selectgridsring, 2);//2列数,unity可自动缩放
//检测控件是否发生了改变
if (GUI.changed) {
print("某些控件发生了改变");
//具体控件点击的改变
if(toolbarInt==1)
{
print("toolbar1被点击了");
}
}
GUI.EndGroup ();
}
}

GUI.EndGroup ();是结束距离他最近的GUI.BeginGroup();
下面的我们可以利用控件组的嵌套来实现一些特殊的效果,如视频加载时候的缓冲条~
具体代码如下:
public class NextGroup : MonoBehaviour { public Texture2D bgimg; public float play=1.0f; // Use this for initialization void Start () { } // Update is called once per frame void Update () { play -= 0.002f; if (play <= 0f) { play=1f; } } void OnGUI() { GUI.BeginGroup (new Rect(0,0,256,32)); GUI.Box (new Rect (0, 0, 256, 32),bgimg); GUI.BeginGroup (new Rect (0, 0, play * 256,32)); GUI.Box (new Rect (0, 0, 256, 32),""); GUI.EndGroup (); GUI.EndGroup (); } }
这里嵌套了2个控件组,然后根据每帧里面的box大小发生变化,从而实现我们想要的效果。

标签:
原文地址:http://www.cnblogs.com/liuruitao/p/4271349.html