2015-06-19 90 views
0

我一直在处理暂停菜单的脚本,并且在开始执行下一行之前我不知道如何停止脚本 这是代码,我问这是因为多次执行“if”,因为检测到我仍在按取消按钮。 (I使用C#和统一5工作) 由于如何在执行其余命令之前停止一秒钟

using UnityEngine; 
using System.Collections; 

public class MenuPausa : MonoBehaviour { 

public GameObject menuPausa; 

private bool pausaMenu = false; 

// Use this for initialization 
void Start() { 

} 

// Update is called once per frame 
void Update() { 
    if (Input.GetButtonDown("Cancel") || pausaMenu == false) { 
     menuPausa.SetActive (true); 
     Time.timeScale = 0; 
     WaitForSeconds(1); 
     pausaMenu = true; 
    } else { 
     menuPausa.SetActive (false); 
     Time.timeScale = 1; 
     Invoke("waiting",0.3f); 
    } 
} 

}

+0

http://answers.unity3d.com/questions/379440/a-simple-wait-function-without-coroutine-c.html – Benj

+0

也许Thread.Sleep()。虽然,我不确定这是什么意思。你想要做某种类型的投票吗? – PilotBob

+0

如果你想进行某种类型的轮询,这可能会有所帮助。 http://sstut.com/csharpdotnet/javascript-timers-equivalent.php – PilotBob

回答

0

在该方法结束时,把

Thread.Sleep(1000); 

它暂停1000毫秒== 1秒执行。 这是一个简单的解决方案吗?或者你需要一个更复杂的计时器吗?

+0

不工作,我想停止if方法,因此我可以保留暂停菜单 – Fran910

+0

现在我不明白你的目标。是否要保持取消按钮被按下,但让Update()方法只反应一次? – Isolin

+0

我想要做的是: 当我按esc设置为主动游戏objet,直到我可以做我的自我,但如果我使用if(Input.GetButtonDown(“取消”))'检测就像我仍然握着esc按钮一样 – Fran910

1

我会有点用协同程序是这样的:

bool paused = false; 

void Update() 
{ 
    if (paused) // exit method if paused 
     return; 

    if (Input.GetButtonDown("PauseButton")) 
     StartCoroutine(OnPause()); // pause the script here 
} 

// Loops until cancel button pressed 
// Sets pause flag 
IEnumerator OnPause() 
{ 
    paused = true; 

    while (paused == true) // infinite loop while paused 
    { 
     if (Input.GetButtonDown("Cancel")) // until the cancel button is pressed 
     { 
      paused = false; 
     } 
     yield return null; // continue processing on next frame, processing will resume in this loop until the "Cancel" button is pressed 
    } 
} 

这只“暂停”,并恢复该单个脚本。所有其他脚本将继续执行其更新方法。要暂停独立于帧速率的方法(即物理),请在暂停时设置Time.timeScale = 0f,在未暂停时设置为1f。如果需要暂停所有其他脚本,并且它们取决于帧速率(即更新方法),则使用全局暂停标志而不是本示例中使用的本地暂停变量,并检查每个更新方法中是否设置了该标志,就像在这个例子中一样。

1

如果我误解了这一点,我表示歉意,但在我看来,您似乎在使用“取消”按钮打开和关闭暂停菜单,并且您的脚本似乎在打开后立即关闭它,原因是仍然按下“取消”按钮。在我看来,这个脚本也是打开和关闭菜单对象的一个​​组件。如果是这样的话,我会建议如下:

有没有这个脚本用于菜单本身以外的其他对象(如MenuManager对象或其他东西 - 我也将此脚本的名称改为MenuManger以避免混淆它与实际的菜单)将在现场保持活跃。在分配给此MenuManager的“menuPausa”属性的场景中有一个PauseMenu对象。然后我会删除pausaMenu变量。此外,请确保此脚本仅作为组件添加到一个对象(MenuManager)中,否则第二个对象可能会在同一帧中更新并将菜单右移。

相关问题