当在协程内部停止自身后,后面的代码块还会继续执行,直到遇到yield语句才会终止。

经测试:停止协程,意味着就是停止yield,所以在停止协程后,yield之后的语句也就不会执行了。

代码如下:

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class Test : MonoBehaviour {
 5 
 6     void Start () {
 7         StartCoroutine("Method");
 8     }
 9 
10     IEnumerator Method() {
11         StopCoroutine("Method");
12         Debug.Log("协程停止");
13         yield return 0;
14         Debug.Log("协程yield之后的代码");
15     }
16 }

执行结果如下:

Unity在协程内部停止协程自身后代码执行问题

 

相关文章:

  • 2021-07-30
  • 2021-07-07
  • 2021-12-09
  • 2022-12-23
  • 2021-10-12
  • 2022-12-23
  • 2021-04-05
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-15
  • 2021-12-25
相关资源
相似解决方案