2,584
社区成员




private void Start()
{
StartCoroutine(DoSomething1());
}
private IEnumerator DoSomething1()
{
Debug.Log("Do Something1");
yield return DoSomething2();
Debug.Log("Done 2");
}
private IEnumerator DoSomething2()
{
Debug.Log("Do Something2");
yield return new WaitForSeconds(1);
Debug.Log("Done 1");
}
debug顺序是:Do Something1,Do Something2,Done 1,Done 2
这样做的前提是你正常执行的代码也要放到协程里去,但是由于第一个yield return之前的代码会直接顺序执行,所以可以将第一个yield return之前的代码当做主线程代码用。
public void DoSomething1()
{
StartCoroutine(NewCoroutine(()=>{Debug.Log("Done2");}));
}
public void DoSomething2()
{
StartCoroutine(NewCoroutine(()=>{Debug.Log("Done3");}));
}
IEnumerator NewCoroutine(Action onComplete)
{
yield return null;
Debug.Log("Done1");
onComplete?.Invoke();
}
就像这样.