unity game Developemnt in 24 hours 第13章 GUI
1. GUI是什么? Graphical User Interface ,图形化用户接口,也就是我们在界面上看到的操作界面,比如按钮button、文字说明,选项等等,他是2D的,目前unity3d只支持在代码中编写,没有编辑器。
注:建议在编写代码时吧此类GUI相关的写到一起统一管理。
2. 内置的GUI组件分为: label, box,toggle等
cs代码如下:
int someValue = 0;
bool toggleState = false;
int buttonInt = 0;
string[] list = {"Easy" , "Normal", "Hard"} ;
// string textString = "Enter text Here" ;
string textString = "" ;
string textString2 = "" ;
void OnGUI(){
// GUI.Label (new Rect ( Screen.width/2 , Screen.height/2 , 80, 20), "Hello World");
// GUI.Label (new Rect ( Screen.width/2 , Screen.height/2 , 100 , 50), "Hello World");
// GUI.Box (new Rect ( Screen.width/2 - 50 , 0 , 100 , 50), "Hello World");
toggleState = GUI.Toggle (new Rect ( 80, 0, 80, 30), toggleState , "My Toggle" ) ;
buttonInt = GUI.Toolbar (new Rect ( 200, 0, 200, 30), buttonInt, list);
if (GUI.Button (new Rect (Screen.width / 2 - 50, 0, 100, 50), "下一步")) {
print ("22222222222222222" ) ;
}
if (GUI.RepeatButton ( new Rect ( 0, 0, 40, 20), "+1")) {
someValue += 1 ;
print(someValue );
}
textString = GUI.TextField( new Rect ( 0, 30, 200, 30), textString );
textString2 = GUI.TextArea( new Rect ( 0, 80, 200, 100), textString2 );
vValue = GUI.VerticalSlider( new Rect ( 305, 150, 20, 150), vValue , 0, 100 );
hValue = GUI.HorizontalSlider( new Rect ( 300 , 130, 150, 20), hValue , 0, 100 );
}
float hValue = 0;
float vValue = 0 ;
3. 自定义GUI样式,cs代码如下:
public GUISkin skin ;
void OnGUI(){
GUI.skin = skin;
if ( GUI.Button (new Rect (5, 5, 200, 50), "Hello World") );
}
其中skin是在Asset中create的,可以再Inspector界面编辑属性