【Unity 3D】学习笔记五:Toggle(开关按钮)

Unity 学习笔记五

 

学习资料:《Unity 3D游戏开发》 宣雨松

 

Toggle控件用来创建一个开关按钮,跟单选框一样。其返回值为bool类型。

 

例子

 

//工具栏选择按钮的ID
var select : int;

//工具栏显示按钮的字符串
var barResource : String[];

var selectToggle0:boolean;
var selectToggle1:boolean;

var info:String;
function Start()
{
	//初始化
	select = 0;
	barResource = ["File","Edit","Assets","GameObject","component","terrain","window","life"];
	info = "Grandpa I miss you, I hope you can live a good life over other new world";
	selectToggle0 = false;
	selectToggle1 = false;
}

function OnGUI () 
{
    //
    var oldSelect = select;
	//重新计算本次工具栏选择的ID
	select = GUI.Toolbar(Rect (10, 10, barResource.length * 80, 30), select, barResource);
	//如果两次选择的是不同的工具栏,将选择按钮全部释放掉
    if (oldSelect != select) {
        selectToggle0 = false;
        selectToggle1 = false;
    }
	
	//根据工具栏选择的ID 显示不同的信息
	switch(select)
	{
	case 0:
	    selectToggle0 = GUI.Toggle(Rect(10, 50, 200, 30), selectToggle0, "子选项1");
		selectToggle1 = GUI.Toggle(Rect(10, 80, 200, 30), selectToggle1, "子选项2");
		GUI.Label(Rect(10, 110, 200, 30), "你选择的是:" + barResource[select]);
		break;
	case 1:
	    selectToggle0 = GUI.Toggle(Rect(90, 50, 200, 30), selectToggle0, "子选项1");
		selectToggle1 = GUI.Toggle(Rect(90, 80, 200, 30), selectToggle1, "子选项2");
		GUI.Label(Rect(90, 110, 200, 30), "你选择的是:" + barResource[select]);
		break;	
	case 2:
	    selectToggle0 = GUI.Toggle(Rect(170, 50, 200, 30), selectToggle0, "子选项1");
		selectToggle1 = GUI.Toggle(Rect(170, 80, 200, 30), selectToggle1, "子选项2");
		GUI.Label(Rect(170, 110, 200, 30), "你选择的是:" + barResource[select]);
	    break;
	case 3:
	    selectToggle0 = GUI.Toggle(Rect(250, 50, 200, 30), selectToggle0, "子选项1");
		selectToggle1 = GUI.Toggle(Rect(250, 80, 200, 30), selectToggle1, "子选项2");
		GUI.Label(Rect(250, 110, 200, 30), "你选择的是:" + barResource[select]);
		break;
	case 4:
	    selectToggle0 = GUI.Toggle(Rect(330, 50, 200, 30), selectToggle0, "子选项1");
		selectToggle1 = GUI.Toggle(Rect(330, 80, 200, 30), selectToggle1, "子选项2");
		GUI.Label(Rect(330, 110, 200, 30), "你选择的是:" + barResource[select]);
		break;
	case 5:
	    selectToggle0 = GUI.Toggle(Rect(410, 50, 200, 30), selectToggle0, "子选项1");
		selectToggle1 = GUI.Toggle(Rect(410, 80, 200, 30), selectToggle1, "子选项2");
		GUI.Label(Rect(410, 110, 200, 30), "你选择的是:" + barResource[select]);
		break;
	case 6:
		selectToggle0 = GUI.Toggle(Rect(490, 50, 200, 30), selectToggle0, "子选项1");
		selectToggle1 = GUI.Toggle(Rect(490, 80, 200, 30), selectToggle1, "子选项2");
		GUI.Label(Rect(490, 110, 200, 30), "你选择的是:" + barResource[select]);
		break;
	case 7:
		selectToggle0 = GUI.Toggle(Rect(570, 50, 200, 30), selectToggle0, "子选项1");
		selectToggle1 = GUI.Toggle(Rect(570, 80, 200, 30), selectToggle1, "子选项2");
		GUI.Label(Rect(570, 110, 200, 100), "life" + info);
		break;
	}
	
	
}


 

【Unity 3D】学习笔记五:Toggle(开关按钮)_第1张图片

 

【Unity 3D】学习笔记五:Toggle(开关按钮)_第2张图片

 

【Unity 3D】学习笔记五:Toggle(开关按钮)_第3张图片

 

Toggle(开关按钮)技术文档:

【Unity 3D】学习笔记五:Toggle(开关按钮)_第4张图片

 

最后,虽然我写的学习笔记很没有技术含量,基本是照本宣科似的写。但我还是想以此文,纪念下我的外公。原谅我没有回去见您。

你可能感兴趣的:(Unity)