EditorCoroutines
allow you to start and stop Unity coroutines in Editor scripts.
This library is a modernisation of @marijnz's unity-editor-coroutines, built from the ground up keeping newer C# features, API architecture, and Unity performance in mind.
An example:
// In Editor script
private void RunLoop ()
{
EditorCoroutineService.StartCoroutine (LoopEveryTwoSeconds ());
}
private IEnumerator LoopEveryTwoSeconds ()
{
while (true)
{
Debug.Log ("EditorCoroutine Demo. Logging again in 2 seconds...");
yield return new WaitForSeconds (2f);
}
}
results in this...
Note that the Editor does not need to be playing (as can be seen in the image above).
EditorCoroutines
currently supports starting and stopping coroutines via the following methods:
EditorCoroutine StartCoroutine (IEnumerator routine);
EditorCoroutine StartCoroutine (object owner, string methodName, object[] methodArgs = null);
void StopCoroutine (IEnumerator routine);
void StopCoroutine (EditorCoroutine routine);
void StopCoroutine (object owner, string methodName);
void StopAllCoroutines ();
void StopAllCoroutines (object owner);
WaitForSeconds
WaitForFixedUpdate
WaitForEndOfFrame
null
(behaves similarly toWaitForFixedUpdate
)AsyncOperation
:WWW
WWW
is now obsolete from Unity. UseUnityWebRequest
instead, which is supported as anAsyncOperation
.CustomYieldInstruction
- Nested
EditorCoroutines
The base EditorCoroutines
is simply a special iterator methodology. Additionally, EditorCoroutines.Unity
is purely a Unity Editor concept (and implementation).
Unfortunately, a lot of UnityEngine
/UnityEditor
methods are implemented by the supplied CLR (via attributes that identify them), and not available in their provided assemblies, even via Reflection.
This prevented me from writing unit tests that could directly be triggered via dotnet
. Also confirmed on a different thread here.
Finally, I could mock the Unity dependencies, but that would add an additional layer of interfaces, which I decided to avoid.
As a result, all the viable tests for this project are written in Unity Editor's TestRunner. You can find the currently implemented tests in the Tests.cs file.
Andeart.EditorCoroutines.dll
: Base class lib, for extensibility in other environments.
Andeart.EditorCoroutines.Unity.dll
: Unity implementation, for use in the Editor.
- Download the files from their respective NuGet pages (Base/Unity). If you want to use the Unity implementation, you need both the files.
- Optionally, you can instead download from the Github Releases page, which contains both files.
- Drop both the files anywhere in your Unity project. Any sub-directory under
Assets
will work- it does not need to be under anEditor
folder. - You can now use
EditorCoroutines
in your Editor scripts. - Refer to the Demo C# file for more examples.
Please feel free to send in a pull request, or drop me an email. Cheers!