Unity3d暂停按钮不会暂停游戏onClick()

时间:2018-05-29 15:29:34

标签: c# user-interface unity3d

我制作了一个unity3d游戏,我想添加UI。我已经开始使用暂停按钮,但它似乎不起作用。 这是按钮信息: button info

我已经创建了一个uiManager脚本来管理按钮,如上图所示,这里是代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class myUIManager : MonoBehaviour {


    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {

    }

    public void Pause() //Function to take care of Pause Button.. 
    {

        print("Entered Pause Func");
        if (Time.timeScale == 1 && paused == false) //1 means the time is normal so the game is running..
        {
            print("Enterer first if");
            Time.timeScale = 0; //Pause Game..
        }

        if (Time.timeScale == 0)
        {
            Time.timeScale = 1; //Resume Game..
        }
    }
}

这是画布截图: enter image description here

有什么想法吗?我一直在寻找几个小时..

2 个答案:

答案 0 :(得分:2)

我认为您的问题出在Pause方法中:

public void Pause() //Function to take care of Pause Button.. 
{

    print("Entered Pause Func");
    if (Time.timeScale == 1 && paused == false) //1 means the time is normal so the game is running..
    {
        print("Enterer first if");
        Time.timeScale = 0; //Pause Game..
    }

    if (Time.timeScale == 0)
    {
        Time.timeScale = 1; //Resume Game..
    }
}

如果您输入第一个if语句,则设置Time.timeScale = 0 - 然后立即进入第二个if并将其重新设置为1.

尝试此操作 - returns方法PauseTime.timeScale设置为0。

public void Pause() //Function to take care of Pause Button.. 
{

    print("Entered Pause Func");
    if (Time.timeScale == 1 && paused == false) //1 means the time is normal so the game is running..
    {
        print("Enterer first if");
        Time.timeScale = 0; //Pause Game..
        return;
    }

    if (Time.timeScale == 0)
    {
        Time.timeScale = 1; //Resume Game..
    }
}

如果您在Pause方法中要做的唯一两件事就是将Time.timeScale设置为0或1,您甚至可以将其简化为:

public void Pause() //Function to take care of Pause Button.. 
{

    print("Entered Pause Func");
    if (Time.timeScale == 1 && paused == false) //1 means the time is normal so the game is running..
    {
        print("Enterer first if");
        Time.timeScale = 0; //Pause Game..
    }
    else
    {
        Time.timeScale = 1; //Resume Game..
    }
}

答案 1 :(得分:2)

如果您的第一个if语句的条件为true,那么您将timeScale设置为0,那么第二个if的条件将变为{{ 1}}然后你将它设置回true你应该将你的第二个1改为if,这样如果第一个条件是else if那么你的程序不会检查第二个。

true
相关问题