【问题标题】:Unity3d Pause Button doesn't pause game onClick()Unity3d暂停按钮不会暂停游戏onClick()
【发布时间】:2018-05-29 15:29:34
【问题描述】:

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

我创建了一个 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..
        }
    }
}

这是画布截图:

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

【问题讨论】:

  • 当您按下按钮时,您是否在控制台选项卡中看到 "Entered Pause Func"
  • @Programmer 是的

标签: c# user-interface unity3d


【解决方案1】:

我认为您的问题出在您的 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。

试试这个 - 将 Time.timeScale 设置为 0 后,将 Pause 方法中的 returnss 设置为 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..
    }
}

【讨论】:

    【解决方案2】:

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

     public void Pause() 
        {
            if (Time.timeScale == 1) 
            {
                Time.timeScale = 0;
            }
           else if (Time.timeScale == 0)
            {
                Time.timeScale = 1; //Resume Game..
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多