Class EditorCoroutine
Manages and executes coroutines within the Unity Editor environment.
Inherited Members
Namespace: Unity.EditorCoroutines.Editor
Assembly: Unity.EditorCoroutines.Editor.dll
Syntax
public class EditorCoroutine
Remarks
The `EditorCoroutine` class enables running coroutines without entering Play Mode, providing a way to perform asynchronous tasks within the Unity Editor. This feature is beneficial for developing editor tools that require non-blocking operations such as waiting on web requests, IO operations or for user inputs.
This class interacts with the EditorApplication.update event to advance coroutine execution. Coroutines started with `EditorCoroutine` can be controlled via the EditorCoroutineUtility methods, which offer lifecycle control such as stopping or pausing operations.
**Tip**: `yield return null` is a way to skip a frame in the Editor.
Important Considerations:
- Be aware of editor performance when using `EditorCoroutine` for intensive tasks; aim to optimize long-running operations to avoid impacting the Unity Editor's responsiveness. Remember that coroutines execute synchronously on the main thread in between of yielding.
- Coroutines automatically stop if the owner object is garbage collected, thanks to a `WeakReference` ownership check.
- No explicit update calls are needed as this class handles its integration into the editor's update loop.
Examples
The following example demonstrates how to use `EditorCoroutine` to increment a counter over time:
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections;
using Unity.EditorCoroutines.Editor;
using UnityEditor;
using UnityEngine;
public class EditorCoroutineExample : EditorWindow
{
int m_Counter;
EditorCoroutine m_Coroutine;
[MenuItem("Window/Editor Coroutine Example")]
public static void ShowWindow()
{
GetWindow<EditorCoroutineExample>("Editor Coroutine Example");
}
void OnGUI()
{
if (GUILayout.Button("Start Coroutine"))
m_Coroutine = EditorCoroutineUtility.StartCoroutine(IncrementCounter(), this);
if (GUILayout.Button("Stop Coroutine"))
EditorCoroutineUtility.StopCoroutine(m_Coroutine);
GUILayout.Label("Counter: " + m_Counter);
}
IEnumerator IncrementCounter()
{
for (int i = 0; i < 10; i++)
{
m_Counter++;
Debug.Log("Counter: " + m_Counter);
Repaint();
yield return new EditorWaitForSeconds(1);
}
}
}