如何隐藏统一按钮

时间:2012-09-05 16:09:45

标签: unity3d

我想创建一个按钮,按下它时会打开一个新按钮。我知道如何制作两个按钮,但是我无法在点击后隐藏第一个按钮。

到目前为止,这是我的代码:

#pragma strict

function Start () 
{
}

function Update () 
{
}

var isButtonVisible  :  boolean  =  true;  

var buttonRectangle  :  Rect     =  Rect(100, 100, 100, 50);

function OnGUI ()
{
    var NewButton = GUI.Button(Rect (Screen.width / 2 - 75, Screen.height / 2 -25,150,50), "this is also a button");

    if ( isButtonVisible ) 
    {
        if ( GUI.Button(Rect (Screen.width / 2 - 75, Screen.height / 2 -25,150,50), "button") ) 
        {
            isButtonVisible = false;


            if ( isButtonVisible ) 
            {
                return NewButton;
            }
        }
    }
}

我是编程的新手,所以这个问题可能有点不清楚。

2 个答案:

答案 0 :(得分:2)

我同意" Happy Apple"解决方案,如果您想要集成反向功能,只需对其进行扩展,您可以按如下方式更改代码:

var isButtonVisible : boolean = true;
var buttonRectangle : Rect = Rect(100, 100, 100, 50);

function OnGUI ()

{

if(isButtonVisible)
{

    if(GUI.Button(Rect(Screen.width/2 - 75,Screen.height/2 - 25,150,50),"button"))
    {
        isButtonVisible = false;
    }

}
else
{

    if(GUI.Button(Rect(Screen.width/2 - 75,Screen.height/2 -25,150,50),"this is also a button"))
    {
        isButtonVisible = true;
    }

}

}

希望这有用。

答案 1 :(得分:1)

这只是一个逻辑错误。首先,你要检查另一个if(isButtonVisible)括号内是否(isButtonVisible),这是多余的。其次,如果我们知道第二个按钮出现的条件(单击第一个按钮)和单击所述按钮的布尔标志(isButtonVisible == false),我们可以分支isButtonVisible条件以显示第二个按钮,当这是假的。

假设你想要第一个按钮让另一个按钮出现并在点击时隐藏自己,这应该做你想要的(虽然它只会在逻辑上单向流动,这是第一个按钮会隐藏自己并显示第二个按钮,但不可逆转)。所以你的原始代码非常接近。

var isButtonVisible  :  boolean  =  true;  

var buttonRectangle  :  Rect     =  Rect(100, 100, 100, 50);

function OnGUI ()
{
    if ( isButtonVisible ) 
    {
        if ( GUI.Button(Rect (Screen.width / 2 - 125, Screen.height / 2 -175,150,50), "button") ) 
        {
            isButtonVisible = false;
        }
    }
    else
    {
        var NewButton = GUI.Button(Rect (Screen.width / 2 - 75, Screen.height / 2 -25,150,50), "this is also a button");
    }
}

不可否认,有几种更好的方法可以实现这一点,但我希望它能解决您的问题。