Unity3D button控件 灰太狼 2021-12-02 01:26 359阅读 0赞 按钮有**三个基本的状态**组成:未点击状态、击中状态、点击后状态。 > GUI.Button(位置, 按钮显示内容); 例如: **GUI.Button(new Rect(10,20,100,40), "我是按钮");** **将添加一个显示“我是按钮”的按钮,位置在界面的Rect(10,20,100,40)位置。**[点击查看Rect()位置图解][Rect] 下面示例中包含一个文字按钮,和一个连续按钮(都是文字按钮种类) 示例: using UnityEngine; using System.Collections; public class lable : MonoBehaviour { string str = "未点击状态!"; //提示信息 int frameTime = 0; //时间计数器 void OnGUI(){ GUI.color = Color.red; //设置标签中文字的颜色 //显示提示信息内容 GUI.Label (new Rect (Screen.width-100,30, 100, 40),str); GUI.color = Color.red; //设置按钮中文字的颜色 //点击按钮修改提示信息 if (GUI.Button (new Rect (10, 30, 80, 40), "我是按钮")) { str = "点击中"; } GUI.color = Color.green; //点击按钮修改提示信息 if (GUI.RepeatButton (new Rect (10, 100, 100, 40), "点击按钮中!")) { str = "按按钮持续的时间"+frameTime; frameTime++; //时间计时器 } } } 运行结果 ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5NTg4MDAz_size_16_color_FFFFFF_t_70][] 一般,游戏界面的按钮只监听“**未点击状态**”和“**点击后状态**”。 函数原型(JavaScript)如下: > public static function Button(position:Rect, text:string):bool; > public static function Button(position:Rect, image:Texture):bool; > public static function Button(position:Rect, content:GUIContent):bool; > public static function Button(position:Rect, text:string, style:GUIStyle):bool; > public static function Button(position:Rect, image:Texture, style:GUIStyle):bool; > public static function Button(position:Rect, content:GUIContent, style:GUIStyle):bool; Button 控件的参数如下表所示。 <table> <tbody> <tr> <th>参 数</th> <th>功 能</th> <th>参 数</th> <th>功 能</th> </tr> <tr> <td>position</td> <td>设置控件在屏幕上的位置及大小</td> <td>text</td> <td>设置控件上显示的文本</td> </tr> <tr> <td>image</td> <td>设置控件上显示的纹理图片</td> <td>content</td> <td>设置控件的文本、图片和提示</td> </tr> <tr> <td>style</td> <td>设置控件使用的样式</td> <td> </td> <td> </td> </tr> </tbody> </table> [Rect]: https://blog.csdn.net/qq_39588003/article/details/96716213 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5NTg4MDAz_size_16_color_FFFFFF_t_70]: /images/20211201/109299e9c4084c6eb5ff66d49117076c.png
还没有评论,来说两句吧...