从零开始学习UNITY3D(GUI篇 群组视图控件)

从零开始学习UNITY3D(GUI篇 群组视图控件)

控件组可以看成一个大的容器,控件组里面的控件,相对位置已该控件组为基准,而不再已屏幕左上角为基准。

下面来看一下代码实例及其效果截图:

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 ();
}
}

从零开始学习UNITY3D(GUI篇 群组视图控件)

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(,,,));
GUI.Box (new Rect (, , , ),bgimg);
GUI.BeginGroup (new Rect (, , play * ,));
GUI.Box (new Rect (, , , ),"");
GUI.EndGroup ();
GUI.EndGroup ();
}
}

这里嵌套了2个控件组,然后根据每帧里面的box大小发生变化,从而实现我们想要的效果。

从零开始学习UNITY3D(GUI篇 群组视图控件)

上一篇:(详细)php实现留言板---会话控制-----------2017-05-08


下一篇:HDU 3549 Flow Problem (最大流ISAP)