欢迎来到unity学习、unity培训、unity企业培训教育专区,这里有很多U3D资源、U3D培训视频,我们致力于打造业内unity3d培训、学习第一品牌。
今天学习了新的内容:GUI
GUI使用OnGUI()调用,OnGUI每帧调用一次。
下面讲一下OnGUI中常用到的的几个控件。
1.Lable 文本
Label (position : Rect, text : string) : void //添加文字
Label (position : Rect, image : Texture) : void //添加图片
例:
public Texture img;
void OnGUI()
{
GUI.Label(new Rect(10,10,100,20),"Hello Word" );
GUI.Label(new Rect(10,40,img.width,img.height), img);
}
2.Box盒子边框
Box的特点为会有一圈边框。
Box (position : Rect, text : string) : void //添加文字
Box (position : Rect, image : Texture) : void //添加图片
例:
public Texture img;
void OnGUI()
{
GUI.Box(new Rect(10, 10, 100, 20), "Hello Word");
GUI.Box(new Rect(10, 40, img.width, img.height), img);
}
3.Button按钮框
Button中如果鼠标选中会有一个按钮被选中的效果,可以有点击效果提示
Button (position : Rect, text : String) : bool //返回值为Bool类型
Button (position : Rect, image : Texture) : bool
例:
void OnGUI()
{
GUI.Button(new Rect(10, 10, 100, 20), new GUIContent("Hello Word","GG") );
GUI.Label (new Rect (130,30,50,20),GUI.tooltip );
}
4.RepeatButton双击按钮
在RepeatButton中如果鼠标选中会有双击效果,与button一样也会有点击提示。
RepeatButton (position : Rect, text : String) : bool //返回值为bool类型
RepeatButton (position : Rect, image : Texture) : bool
public Texture img;
void OnGUI()
{
GUI.RepeatButton(new Rect(10, 10, 100, 20), "Hello World!");
GUI.RepeatButton(new Rect(10, 50, img.width, img.height), img);
}
5.TextField输入框
使用TextField可以输入字符。
TextField (position : Rect, text : String) : String //返回值为string 类型
TextField (position : Rect, text : String, maxLength : int) : String //输入字符数量有限制
例:
string stc = "Hello Word";
void OnGUI()
{
stc = GUI.TextField(new Rect(10,10,100,20),stc );
stc = GUI.TextField(new Rect (10,10,100,50),stc,10);//限制只能输入十个字符
}
6.PasswordField密码框
可以使输入的字符显示为隐藏
"*"[0]等价于'*' 单引号内的*可以更改为其他字符
PasswordField (position : Rect, password : String, maskChar : char) : String
例:
string stc = "Hello Word";
void OnGUI()
{
stc = GUI.PasswordField(new Rect(10, 10, 100, 20), stc, '*');
}
7.TextArea多行输入
可以输入多行
TextArea (position : Rect, text : String) : String // 输入为string类型
TextArea (position : Rect, text : String, maxLength : int) : String //限制输入字符数量
例:
string stc = "Hello Word";
void OnGUI()
{
stc = GUI.TextArea(new Rect (10,10,200,200),stc);
stc = GUI.TextArea(new Rect (10,10,200,200),stc,200);//只能输入200个字符
}