unity3d - Unity - 一切都冻结在“ yield return new WaitForSeconds(); ”?
问题描述
好的!我在这个场景中的所有代码都在一个脚本和一个管理器对象中。所有这些都是大约 700 行。所以我不能把它放在这里。我测试了不同的东西:
- 1) 将平台从android切换到pc/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 点击 On 按钮上正常运行。但通常,它会在第 20 步之后冻结。在 21、23、27、34 ......非常随机。但总是在 20 岁之后,并且有一段时间没有冻结,直到时间结束。并且总是在收益回报之前。完全在同一行。
我阅读了很多问题和答案,但没有一个是有帮助的。我没有while循环,没有while(true),只要我知道并检查我的代码没有无限循环,在StopAllCoroutines上......什么都没有。我坚持了2天。谢谢大家的帮助。
解决方案
冻结的原因是Random.Range
用于控制问题中链接while
的代码中的循环。在不使用循环的情况下获取随机数的一种方法是将它们生成到然后删除您使用的每一个。这应该可以防止冻结 Unity。while
List
Random.Range
推荐阅读
- java - 系统资源不足。有关详细信息,请参阅以下堆栈跟踪。java.lang.StackOverflowError
- flutter - 我如何离线获取颤振网站文档
- list - 与“句子”函数类似的函数,但它与列表一起使用
- c - C - 递归而不是循环
- mysql - 如何使用 docker compose 将 phpmyadmin 连接到 mySql 服务器
- linux - 使用 shell gitlab-runner ERRO [0000] 时出现 docker 错误
- librosa - Mel mfcc的组件有哪些
- azure - 尝试克隆 Azure 管道但出现“引用服务连接...未找到”错误
- ruby - ActionCable 5 分钟后与 redis 服务器断开连接
- firebase - 只要应用程序打开并连接,Firestore 侦听器是否会无限期运行?