Unity3D中GUI的操作还是比较方便的,显示按钮只需要在脚本中用GUI.Button显示即可。
GUI.Button的函数原型:
1 2 3 4 5 6 | static function Button(position: Rect, text: string ): bool ; static function Button(position: Rect, image: Texture): bool ; static function Button(position: Rect, content: GUIContent): bool ; static function Button(position: Rect, text: string , style: GUIStyle): bool ; static function Button(position: Rect, image: Texture, style: GUIStyle): bool ; static function Button(position: Rect, content: GUIContent, style: GUIStyle): bool ; |
参数
position 屏幕中的位置
text 按钮文字
image 按钮图片
content 按钮上文字、图片和工具提示
style 风格
下面是具体的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | public class ButtonsScript : MonoBehaviour { public Texture buttonTexture; private int repeatTime = 0; private string info = "" ; void Start () { } // Update is called once per frame void Update () { } void OnGUI() { // 文本显示 GUI.Label ( new Rect (50, 200, 200, 50), info); // 第一个文字按钮 GUI.color = Color.yellow; //按钮文字颜色 GUI.backgroundColor = Color.red; //按钮背景颜色 if (GUI.Button( new Rect(50,250,200,30), "Button1" )) { info = "按下了Button1" ; } // 第二个图片按钮 GUI.color = Color.white; //按钮文字颜色 GUI.backgroundColor = Color.green; //按钮背景颜色 if (GUI.Button( new Rect(50,300,128,64), buttonTexture)) { info = "按下了Button2" ; } // 持续按下的按钮 if (GUI.RepeatButton( new Rect(50,400,200,30), "按钮按下中" )) { info = "按钮按下中的时间:" + repeatTime; repeatTime++; } } } |
代码中显示了三种类型的按钮:文字、图片、持续按下的按钮。
联系客服