很抱歉,如果这是一个新手的问题,但我环顾四周,找不到任何线索来解决我的问题。当我试图在一起运行两个协同机制时,我目前遇到了一个冻结。
我的最终目标是:
这两个协旅独立工作,或者当第二个协同线直接嵌入到第一个时,而不检查endDay bool。
但是,当我尝试包含while循环时,单元冻结(它到达下面代码的“DayStart.SetActive(True)”,但不会更进一步)。考虑到endDay bool位于第一个协同线的末尾可能是问题的原因,我试图构建两个完全独立的coroutines (只有endDay bool作为两者之间的链接的非嵌入式coroutines),但是它没有变得更好。
有人知道我该怎么做吗?
关于信息,下面是我最初使用的代码,它恢复了错误。我不谈第二个协同线的细节,因为它很长,而且可能不是这里的问题。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Transaction : MonoBehaviour
{
public GameObject DayStart;
public GameObject DayTracker;
private bool endDay;
private void Awake()
{
instance = this;
}
public void MainDayCycle()
{
StartCoroutine(MainDayCycleCO());
}
IEnumerator MainDayCycleCO()
{
endDay = false;
// Beginning of day picture
DayStart.SetActive(true);
yield return new WaitForSeconds(3);
DayStart.SetActive(false);
// [To be completed] Day Tracker bar
DayTracker.SetActive(true);
TradeCycle();
// Lancement de délai de journée et enregistrement de la fin
yield return new WaitForSeconds(20);
endDay = true;
}
public void TradeCycle()
{
while (endDay == false)
{
StartCoroutine(TradeCycleCO());
}
}发布于 2021-05-21 15:44:36
就像我说的,这个
public void TradeCycle()
{
while (endDay == false)
{
StartCoroutine(TradeCycleCO());
}
}没有任何yield,这个循环会冻结,因为endDay中的任何地方都不会改变!
听起来你更想做的是(仍然没有TradeCycleCO代码)
// Does this even need to be public?
public void TradeCycle()
{
StartCoroutine(TradeCycleCO());
}
IEnumerator TradeCycleCO()
{
while(!endDay)
{
// Your code here
// Make sure something yield returns in here
// If needed you could also interrupt at certain points via checking again
// Use that to do e.g. some cleaning up after the loop
if(endDay) break;
// or use that to immediately leave the coroutine overall
if(endDay) yield break;
}
// Code that may be executed after the day has ended e.g. for cleaning up stuff
}https://stackoverflow.com/questions/67639783
复制相似问题