统一 - 一切都冻结在“ yield return new WaitForSeconds(); ”?
Unity - Everything freezes on " yield return new WaitForSeconds(); "?
好的!我在这个场景中的所有代码都在一个脚本和一个管理器对象中。
所有这些都是大约 700 行。所以我不能把它放在这里。
我测试了不同的东西:
- 1) 将平台从 android 切换到
pc/mac
- 2) 在以前的版本上测试
统一性(之前的 2017 年和当前的
在 2018.1 )
none 解决了问题。
然后我更改了我怀疑导致问题的部分代码。 (none 他们解决了解决方案)。
然后我开始到处放 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"。
这个逻辑一遍又一遍地重复,直到时间归零。
"step" 变量根据任何正确和错误的答案增加和减少 1。和变量级别 = steps/10 决定短语的难度。
游戏在 %98 单击“打开”按钮时正常运行。但通常,它会在第 20 步之后的某处冻结。在 21、23、27、34 中......非常随机。但总是在 20 点之后,有一段时间没有冻结,直到时间结束。并且总是在 yield return 之前。完全在同一行。
我阅读了很多问题和答案,但其中 none 很有帮助。我没有 while 循环,没有 while(true),只要我知道并检查我的代码没有无限循环,在 StopAllCoroutines 上……什么都没有。我坚持了 2 天。
谢谢大家的帮助。
冻结的原因是使用 Random.Range
来控制问题中代码 linked 中的 while
循环。在不使用 while
循环的情况下获得随机数的一种方法是将它们生成到 List
中,然后删除您使用的每个随机数。这应该可以防止 Random.Range
冻结 Unity。
好的!我在这个场景中的所有代码都在一个脚本和一个管理器对象中。 所有这些都是大约 700 行。所以我不能把它放在这里。 我测试了不同的东西:
- 1) 将平台从 android 切换到 pc/mac
- 2) 在以前的版本上测试 统一性(之前的 2017 年和当前的 在 2018.1 )
none 解决了问题。 然后我更改了我怀疑导致问题的部分代码。 (none 他们解决了解决方案)。 然后我开始到处放 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);
很奇怪!!!
这是游戏图片
- 1) 制作新词组并将游戏状态更改为"ChooseAnswer"
- 2) 玩家按下 3 个按钮之一,答案被检查,得分和其他内容发生变化,ShowSigns 协程将在 0.3 秒后开始和结束。正如您在协程结束时看到的那样,状态更改为 "CreateNewProblem".
- 3) 在 CreateNewProblem 检测到更新时,代码调用 NewProblem() 函数生成新短语,并在该游戏结束时状态更改为 "ChooseAnswer"。
这个逻辑一遍又一遍地重复,直到时间归零。 "step" 变量根据任何正确和错误的答案增加和减少 1。和变量级别 = steps/10 决定短语的难度。
游戏在 %98 单击“打开”按钮时正常运行。但通常,它会在第 20 步之后的某处冻结。在 21、23、27、34 中......非常随机。但总是在 20 点之后,有一段时间没有冻结,直到时间结束。并且总是在 yield return 之前。完全在同一行。
我阅读了很多问题和答案,但其中 none 很有帮助。我没有 while 循环,没有 while(true),只要我知道并检查我的代码没有无限循环,在 StopAllCoroutines 上……什么都没有。我坚持了 2 天。 谢谢大家的帮助。
冻结的原因是使用 Random.Range
来控制问题中代码 linked 中的 while
循环。在不使用 while
循环的情况下获得随机数的一种方法是将它们生成到 List
中,然后删除您使用的每个随机数。这应该可以防止 Random.Range
冻结 Unity。