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;
  •     }

 執行結果:

最後需要注意的是,協程雖然看起來與線程相似,但其實協程是在主線程上執行的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章