我制作了一个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..
}
}
}
有什么想法吗?我一直在寻找几个小时..
答案 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
方法Pause
将Time.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