/DinnerCoroutine

Easily use coroutines any where.

Primary LanguageC#MIT LicenseMIT

DinnerCoroutine

openupm

点击这里查看中文介绍

DinnerCoroutine is a simple enhancement of Unity's coroutine. The usage of DinnerCoroutine is similar with original Unity coroutine, you can change the original coroutine into DinnerCoroutine without modifying your coroutine code.

Features

  • Support original yield instructions.
  • Support original custom yield instructions.
  • Support both editor and in game coroutine.
  • Support yield return fixed update in editor.
  • Provides full control to coroutine (manually start, pause and recover, stop or interrupt).
  • Provides asynchronous coroutine (you cannot access some Unity methods in this kind of coroutine).
  • Allow global coroutine (won't destroy when MonoBehaviour destroyed).
  • No dependency.
  • More features...

Installation

DinnerCoroutine is easy to install, you can use any of following methods to install it.

OpenUPM (Recommended)

  1. If you are new to OpenUPM, install openupm-cli first.

  2. Go to your Unity project root folder (you can find an Assets folder under it), run this command:

    openupm add com.canstudio.dinner-coroutine
  3. Open your Unity editor, DinnerCoroutine should be installed successfully.

UPM

  1. If you haven't installed Git, download and install it here: download Git

  2. Open your Unity editor, open Window -> Package Manager in the toolbar.

  3. In Package Manager, click + -> add package from git URL in the top left corner.

  4. Add following package:

    https://github.com/SUSTech-CANStudio/DinnerCoroutine.git#upm

Quick Start

A sample script

using System.Collections;
using UnityEngine;
using CANStudio.DinnerCoroutine;

public class MyScript : MonoBehaviour
{
    public float time = 2f;
    
    private void Start()
    {
        this.StartSpoon("MyCoroutine", time);
    }
    
    private IEnumerator MyCoroutine(float waitTime)
    {
        Debug.Log("Ping!");
        yield return new WaitForSeconds(waitTime);
        Debug.Log("Pong!");
    }
}

Control a coroutine

// In a MonoBehaviour script
// The return value 'coroutine' is the handler of a coroutine
var coroutine = this.StartSpoon("MyCoroutine");
Get status
Debug.Log(coroutine.Status);
/**	This can print one of followings:
*	NotStarted
*	Running
*	Paused
*	Finished
*/
Change status
// start a paused of not started coroutine
coroutine.Start();

// pause a coroutine
coroutine.Pause();

// stop a coroutine
coroutine.Stop();

// inturrupt a coroutine
// this behaves same as Stop(), but do not invoke callback event in the coroutine.
coroutine.Interrupt();

Samples

You can view samples here:

Limitations

As Unity doesn't have full event loop in editor made, some command works differently when coroutine runs in editor:

  • WaitForEndOfFrame: performs as yield return null in editor.

  • WaitForSecondsRealtime: won't work properly, will wait for a random time depends on the frame rate.

Time support in editor coroutines

Please pay attention when using the Time class, it doesn't work and always return a constant value in editor mode. Considering use DinnerTime instead of Time in your coroutines, this class is an alias of Time in playing mode, and can also provide partial time access in editor mode.

DinnerTime provides following functions in editor mode:

  • time, deltaTime, unscaledTime, unscaledDeltaTime, timeScale, fixedTime, fixedDeltaTime, fixedUnscaledTime, fixedUnscaledDeltaTime, inFixedTimeStep: These functions works properly in editor just like what they do in playing.
  • Other functions are currently not accessible in editor mode.