Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在执行其余命令之前停止if一秒钟_C#_Unity3d_Unityscript - Fatal编程技术网

C# 如何在执行其余命令之前停止if一秒钟

C# 如何在执行其余命令之前停止if一秒钟,c#,unity3d,unityscript,C#,Unity3d,Unityscript,我一直在为暂停菜单编写脚本,但我不知道如何在开始执行下一行之前使其停止 这是代码,我之所以要求这样做是因为多次执行“如果”,因为检测到我仍在按取消按钮。(我使用c#和Unity 5) 谢谢 }在方法末尾,将 Thread.Sleep(1000); 它暂停执行1000毫秒==1秒。 这个简单的解决方案够了吗,还是需要一个更复杂的带计时器的解决方案?我会使用类似这样的协同程序: bool paused = false; void Update () { if (paused) // ex

我一直在为暂停菜单编写脚本,但我不知道如何在开始执行下一行之前使其停止 这是代码,我之所以要求这样做是因为多次执行“如果”,因为检测到我仍在按取消按钮。(我使用c#和Unity 5) 谢谢


}

在方法末尾,将

Thread.Sleep(1000);
它暂停执行1000毫秒==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,在取消暂停时将Time.timeScale设置为1f。如果需要暂停所有其他脚本,并且它们取决于帧速率(即更新方法),则使用全局暂停标志,而不是本例中使用的局部暂停变量,并检查是否在每个更新方法中设置了该标志,如本例所示。

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


将此脚本用于菜单本身以外的对象(例如MenuManager对象或其他对象-我也会将此脚本的名称更改为MenuManager,以避免将其与实际菜单混淆),该对象将在场景中保持活动状态。在场景中有一个PauseMenu对象,该对象被指定给此动物园管理员的“menuPausa”属性。然后我将删除pausaMenu变量。此外,请确保此脚本仅作为一个对象(菜单管理器)的组件添加,否则第二个对象可能会在同一帧中更新,并将菜单向右关闭

您可以使用计时器,从工具栏插入,更改以毫秒为单位的间隔,然后在完成后使用脚本
timer.Start()
定时器.Stop()

我就是这么用的。

也许是Thread.Sleep()。虽然,我不确定这有什么意义。您正在尝试进行某种类型的轮询吗?如果您想进行某种类型的轮询,这可能会有所帮助。表示当前概念中不存在收益率。如果不起作用,我想停止if方法,这样我就可以保持暂停菜单。我不理解您的目标。是不是你想一直按下取消按钮,但让Update()方法只做出一次反应?我想做的是:当我按下esc set激活一个游戏对象时,直到我可以自己做,但是如果我使用
if(Input.GetButtonDown(“Cancel”)
检测到我是否仍然按住esc按钮
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
    }
}