分享

Unity3D GUI按钮的使用方法

 雪柳花明 2016-04-01
unity3d 2014-10-26 10:40:25 阅读(12319) 评论(0)

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++; 
        
    }
}

代码中显示了三种类型的按钮:文字、图片、持续按下的按钮。

 

unity3d buttons

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多