您的位置:首页 > 移动开发 > Unity3D

Unity3D 基础篇--button入门

2016-03-11 18:41 531 查看
Button是Unity3D中GUI系统的常用控件,其函数原型如下:

staticfunction Button(position: Rect, text: string):bool;
staticfunction Button(position: Rect, image: Texture): bool;
staticfunction Button(position: Rect, content: GUIContent): bool;
staticfunction Button(position: Rect, text: string, style: GUIStyle): bool;
staticfunction Button(position: Rect, image: Texture, style: GUIStyle): bool;
staticfunction Button(position: Rect, content: GUIContent, style: GUIStyle): bool;


参数说明:

position : Rect ——屏幕上的矩形位置(x坐标,y坐标,宽度,高度)。

text : String ——显示的文本内容。

image : Texture ——显示的纹理(图片)。

content : GUIContent ——在标签上显示的文本、图片和信息提示。

style : GUIStyle ——使用的样式,如果不使用,那么标签的样式使用的就是当前的GUISkin皮肤

按钮共有三个基本状态组成:未点击状态、击中状态、点击后状态。一般情况下,游戏界面的按钮只监听“未点击状态”与“点击后状态”两种。

按钮展现方式分为两种:“普通按钮”和“图片按钮”。普通按钮为系统默认显示的按钮,而图片按钮可以设定按钮的背景图案。

下面以实际简单的代码为例:

publicclassButtons : MonoBehaviour {

publicTexture buttonTexture;
privatestringinfo = "";

voidStart () {

}

//Update is called once per frame
voidUpdate () {

}

voidOnGUI()
{
// 文本显示
GUI.Label (newRect (50, 200, 200, 50), info);

// 第一个文字按钮
GUI.color = Color.yellow;  //按钮文字颜色
GUI.backgroundColor = Color.red; //按钮背景颜色

if(GUI.Button(newRect(50,250,200,30),"Button1"))
{
info = "按下了Button1";
}

// 第二个图片按钮
GUI.color = Color.white;  //按钮文字颜色
GUI.backgroundColor = Color.green; //按钮背景颜色

if(GUI.Button(newRect(50,300,128,64), buttonTexture))
{
info = "按下了Button2";
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity3d c#