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

Unity 文字按钮、图片按钮、有持续按下状态的按钮

2013-05-31 23:08 183 查看
#pragma strict

var buttonTexture : Texture2D;  //按钮的图片,public,可以在外面设置

private var str : String;//一个string

private var frameTime : int;   //记录时间的数字 int型

function Start () {
str="请点击按钮"; //初始化赋值
}

function OnGUI()
{
GUI.Label(Rect(10,10,Screen.width,30),str);  //新建一个label显示str

if(GUI.Button(Rect(10,50,buttonTexture.width,buttonTexture.height),buttonTexture))
{
str="点击了图片按钮";
}
GUI.color=Color.green;  //设置str显示颜色
GUI.backgroundColor=Color.red; //按钮的背景颜色

if(GUI.Button(Rect(10,200,70,30),"文字按钮"))//只有按下和没按下状态,没有持续按下这个状态
{
str="点击了文字按钮"+frameTime;
frameTime++;
}
GUI.color=Color.yellow;
GUI.backgroundColor=Color.black;
if(GUI.RepeatButton(Rect(10,250,100,30),"按钮按下中")) //持续按下的状态
{
str="按钮按下中的时间:"+frameTime;
frameTime++;
}

}

function Update () {

}
新建脚本,拖到工程的摄像头。RepeatButton这个按钮有监听持续按下的功能。

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: