猿问

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

我制作了一个 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..

        }

    }

}

这是画布屏幕截图: 

http://img4.mukewang.com/60e96bf7000126df03800600.jpg

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


慕姐8265434
浏览 328回答 2
2回答

牧羊人nacy

我认为你的问题出在你的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。试试这个 -一旦它设置为 0,它就returns来自Pause方法Time.timeScale。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..    }}

青春有我

如果你的第一个条件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..        }    }
随时随地看视频慕课网APP
我要回答