需要单击按钮两次才能隐藏

时间:2016-01-20 06:35:42

标签: c# unity3d

我开始学习Unity和C#,因为我想制作游戏。但是我因为按钮问题而被困在这里。

当我使用Unity运行以下代码时,我得到标签并且"跳过"我想要的按钮。

但是当我按下按钮时,只有肘节出现。标签和按钮不会被隐藏。然后,只有当我第二次点击它时,它们才被隐藏。

如何解决此问题,以便点击按钮"跳过"隐藏按钮和标签以及显示切换。

感谢您查看我的问题。

void OnGUI()
{
    if (!textfin)
    {
        GUI.Label(new Rect(0, 0, Screen.width, Screen.height), text, guiStyle);
        if (GUI.Button(new Rect(Screen.width * 4 / 5, 0, Screen.width / 5, Screen.height / 5), "SKIP"))
        {
            textfin = true;
        }
    }
    else{
        easy = GUI.Toggle(new Rect(Screen.width / 4, 0, Screen.width / 2, Screen.height * 3 / 8), easy, "easy");
        if (easy)
        {
            normal = false;
            hard = false;
            Title.difficulty = 1;
        }
        normal = GUI.Toggle(new Rect(Screen.width / 4, Screen.height * 5 / 16, Screen.width / 2, Screen.height / 4), normal, "normal");
        if (normal)
        {
            easy = false;
            hard = false;
            Title.difficulty = 2;
        }
        hard = GUI.Toggle(new Rect(Screen.width / 4, Screen.height * 9 / 16, Screen.width / 2, Screen.height / 4), hard, "hard");
        if (hard)
        {
            normal = false;
            easy = false;
            Title.difficulty = 3;
        }
        if (easy || normal || hard)
        {
            if (GUI.Button(new Rect(Screen.width / 4, Screen.height * 13 / 16, Screen.width / 2, Screen.height / 8), "Proceed"))
            {
                Application.LoadLevel("Home");
            }
        }
    }
}

void Start () {
    message = "THis is Text for trial slow slow";
    text = "";
    StartCoroutine(TypeText());
}

void Update () {
    if (text == message){
        textfin = true;
    }
}

IEnumerator TypeText()
{
    foreach (char letter in message.ToCharArray())
    {
        text += letter;
        yield return new WaitForSeconds(letterPause);
    }
}

1 个答案:

答案 0 :(得分:3)

当我尝试您的代码时,单击时SKIP按钮消失。

在您的情况下,脚本可能会在场景中添加两次。

还要考虑Unity 4.6引入了您应该使用的新UI系统。请参阅this video

相关问题