Unity协程

启动协程

        StartCoroutine(IEnumerator ie);

关闭协程

       关闭当前脚本中开启的协程。StopAllCoroutines();
       关闭指定的协程。 StopCoroutine(Coroutine cor);

       如果调用关闭协程的语句之后,被要求关闭的协程会在执行最近的一次yield return语句之后被关闭。

  •     private Coroutine _IE;
  •     void Start()
  •     {
  •       _IE=  StartCoroutine(IE());
  •     }
  •     private IEnumerator IE()
  •     {
  •         yield return new WaitForSeconds(1);
  •         Debug.Log("First");
  •         yield return new WaitForSeconds(1);
  •         Debug.Log("Second");
  •         StopAllCoroutines();
  •         Debug.Log("Third");
  •         yield return new WaitForSeconds(1);
  •         Debug.Log("Five");
  •     }

执行结果:

可以看到,当关闭协程语句调用之后,当距离最近的yield return语句调用之后,协程便被关闭。

协程的具体使用

  • yield return StartCoroutine(IEnumerator ie);   等待开启的协程执行完毕后再继续往下执行 
  • yield return IEnumerator ie;                               等待该协程执行完毕后再继续往下执行 
  • yield return new WaitForEndOfFrame();            等待直到所有的摄像机和GUI被渲染完成后,在该帧显示在屏幕之前执行
  • yield return new WaitForFixedUpdate();            等待下一次FixedUpdate开始调用时继续往下执行
  • yield return new WaitForSeconds(5);                等待设定时间之后继续往下执行,受Time.TimeScale影响
  • yield return new WaitForSecondsRealtime(5);  等待设定时间之后继续往下执行,不收Time.TimeScale影响
  • yield return new WaitUntil(() => _Until);             当委托返回值为true时,继续往下执行
  • yield return new WaitWhile(() => _While);         当委托返回值为false时,继续往下执行
  •  如果在协程中直接使用   StartCoroutin(IEnumrator ie),只是开启携程,但并不会等待携程执行完毕才会往下执行。
  • private Coroutine _IE;
  •     void Start()
  •     {
  •         _IE = StartCoroutine(IE());
  •     }
  •     private IEnumerator IE()
  •     {
  •         yield return StartCoroutine(IE02()); //等待开启的协程执行完毕后再继续往下执行
  •         Debug.Log(" yield return StartCoroutine(IE02()) And CurrentTime: " + System.DateTime.Now);
  •         yield return new WaitForEndOfFrame();// 等待直到所有的摄像机和GUI被渲染完成后,在该帧显示在屏幕之前执行
  •         Debug.Log("WaitForEndOfFrame And CurrentTime: " + System.DateTime.Now);
  •         yield return new WaitForFixedUpdate(); //等待下一次FixedUpdate开始调用时继续往下执行
  •         Debug.Log("WaitForFixedUpdate And CurrentTime: " + System.DateTime.Now);
  •         yield return new WaitForSeconds(5); //等待设定时间之后继续往下执行,受Time.TimeScale影响
  •         Debug.Log("WaitForSeconds And CurrentTime: " + System.DateTime.Now);
  •         yield return new WaitForSecondsRealtime(5); //等待设定时间之后继续往下执行,不收Time.TimeScale影响
  •         Debug.Log("WaitForSecondsRealtime And CurrentTime: " + System.DateTime.Now);
  •         yield return new WaitUntil(UntilFunc); //当委托返回值为true时,继续往下执行
  •         Debug.Log("WaitUntil And CurrentTime: " + System.DateTime.Now);
  •         yield return new WaitWhile(WhileFunc); //当委托返回值为false时,继续往下执行
  •         Debug.Log("WaitWhile And CurrentTime: " + System.DateTime.Now);
  •         StartCoroutine(IE02());
  •         Debug.Log("StartCoroutine(IE02()) CurrentTime: " + System.DateTime.Now);
  •     }
  •     private IEnumerator IE02()
  •     {
  •         yield return new WaitForSeconds(5);
  •         Debug.Log("IE02 is End And CurrentTime: " + System.DateTime.Now);
  •     }
  •     private bool UntilFunc()
  •     {
  •         return true;
  •     }
  •     public bool WhileFunc()
  •     {
  •         return false;
  •     }

 执行结果:

最后需要注意的是,协程虽然看起来与线程相似,但其实协程是在主线程上执行的。

发布了13 篇原创文章 · 获赞 9 · 访问量 8258

猜你喜欢

转载自blog.csdn.net/qq_39025293/article/details/84194015