点击这里查看中文介绍
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.
- 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...
DinnerCoroutine is easy to install, you can use any of following methods to install it.
-
If you are new to OpenUPM, install openupm-cli first.
-
Go to your Unity project root folder (you can find an
Assets
folder under it), run this command:openupm add com.canstudio.dinner-coroutine
-
Open your Unity editor, DinnerCoroutine should be installed successfully.
-
If you haven't installed Git, download and install it here: download Git
-
Open your Unity editor, open
Window -> Package Manager
in the toolbar. -
In Package Manager, click
+ -> add package from git URL
in the top left corner. -
Add following package:
https://github.com/SUSTech-CANStudio/DinnerCoroutine.git#upm
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!");
}
}
// In a MonoBehaviour script
// The return value 'coroutine' is the handler of a coroutine
var coroutine = this.StartSpoon("MyCoroutine");
Debug.Log(coroutine.Status);
/** This can print one of followings:
* NotStarted
* Running
* Paused
* Finished
*/
// 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();
You can view samples here:
As Unity doesn't have full event loop in editor made, some command works differently when coroutine runs in editor:
-
WaitForEndOfFrame
: performs asyield return null
in editor. -
WaitForSecondsRealtime
: won't work properly, will wait for a random time depends on the frame rate.
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.