【问题标题】:Unity - Everything freezes on " yield return new WaitForSeconds(); "?Unity - 一切都冻结在“ yield return new WaitForSeconds(); ”?
【发布时间】:2018-11-09 09:08:38
【问题描述】:

好的!我在这个场景中的所有代码都在一个脚本和一个管理器对象中。 所有这些都是大约 700 行。所以我不能把它放在这里。 我测试了不同的东西:

  • 1) 将平台从安卓切换到 电脑/mac
  • 2) 在以前的版本上测试 统一的(2017 年以前,现在 上是 2018.1)

他们都没有解决问题。 然后我更改了我怀疑会导致问题的代码的某些部分。 (他们都没有解决解决方案)。 然后我开始把 Debug.Log()s 到处放。所以我找到了它冻结的地方。

代码如下:

    IEnumerator ShowSigns(int Button1State, int EqualState, int Button2State)
    {

        Debug.Log("ShowSigns");
        if (Button1State == 1)
        {
            OperationOneCorrectSign.GetComponent<CanvasGroup>().alpha = 1;
        }
        else if (Button1State == 2)
        {
            OperationOneIncorrectSign.GetComponent<CanvasGroup>().alpha = 1;
        }
        if (EqualState == 1)
        {
            EqualCorrectSign.GetComponent<CanvasGroup>().alpha = 1;
        }
        else if (EqualState == 2)
        {
            EqualIncorrectSign.GetComponent<CanvasGroup>().alpha = 1;
        }
        if (Button2State == 1)
        {
            OperationTwoCorrectSign.GetComponent<CanvasGroup>().alpha = 1;
        }
        else if (Button2State == 2)
        {
            OperationTwoIncorrectSign.GetComponent<CanvasGroup>().alpha = 1;
        }

        Debug.Log("BeforeWaiting");
        yield return new WaitForSeconds(0.3f);

        Debug.Log("AfterWaiting");
        OperationOneCorrectSign.GetComponent<CanvasGroup>().alpha = 0;
        OperationOneIncorrectSign.GetComponent<CanvasGroup>().alpha = 0;
        EqualCorrectSign.GetComponent<CanvasGroup>().alpha = 0;
        EqualIncorrectSign.GetComponent<CanvasGroup>().alpha = 0;
        OperationTwoCorrectSign.GetComponent<CanvasGroup>().alpha = 0;
        OperationTwoIncorrectSign.GetComponent<CanvasGroup>().alpha = 0;
        state = GameState.CreateNewProblem;

        Debug.Log("EndSigns");
    }

我发现它冻结了:

        yield return new WaitForSeconds(0.3f);

很奇怪!!!

这是游戏的图片。

该游戏是一个简单的游戏,显示 2 个数学短语,玩家应该选择更大或相等。 逻辑是这样的:

  • 1) 制作新短语并将游戏状态更改为“ChooseAnswer”
  • 2) 玩家按下 3 个按钮之一,检查的答案、得分和其他事情发生变化,ShowSigns 协程将在 0.3 秒后开始和结束。正如您在协程结束时看到的那样,状态更改为“CreateNewProblem”。
  • 3) 在更新中,当 CreateNewProblem 检测到时,代码调用 NewProblem() 函数来生成新短语,并且在游戏结束时状态更改为“ChooseAnswer”。

这个逻辑一遍又一遍地重复,直到时间达到零。 任何正确和错误的答案都会使“步长”变量增加和减少 1。而一个变量 level = steps/10 决定了短语的难度。

游戏在 %98 点击按钮上正常运行。但通常,它会在第 20 步之后冻结。在 21、23、27、34 ......非常随机。但总是在 20 岁之后,并且有一段时间没有冻结,直到时间结束。并且总是在收益回报之前。完全在同一行。

我阅读了很多问题和答案,但没有一个是有帮助的。我没有while循环,没有while(true),只要我知道并检查我的代码没有无限循环,在StopAllCoroutines上......什么都没有。我坚持了2天。 感谢大家的帮助。

OH,and Here Is the code file

【问题讨论】:

    标签: unity3d freeze coroutine


    【解决方案1】:

    冻结的原因是使用Random.Range 来控制while 循环,该循环位于您问题的代码linked 中。在不使用while 循环的情况下获取随机数的一种方法是将它们生成为List,然后删除您使用的每一个。这应该可以防止 Random.Range 冻结 Unity。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-19
      • 2016-06-15
      • 2012-10-14
      • 1970-01-01
      • 2015-03-02
      • 2017-12-08
      • 2018-12-18
      • 1970-01-01
      相关资源
      最近更新 更多