打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Unity3D GUI按钮的使用方法
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++; 
        
    }
}

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

 

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Unity3D入门之GUI基础以及常用GUI控件使用(2)
【Unity3d】使GUI适应屏幕分辨率
Unity3D之两种方式播放游戏视频
【Unity3D】3D模型的使用
Unity很多容易遗漏的地方
unity 捕捉屏幕的触控
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服