Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
EditorCoroutine: coroutines for Unity editor operations. Usage: EditorCoroutine.start(myIEnumerator)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Swing.Editor
{
public class EditorCoroutine
{
public static EditorCoroutine start( IEnumerator _routine )
{
EditorCoroutine coroutine = new EditorCoroutine(_routine);
coroutine.start();
return coroutine;
}
readonly IEnumerator routine;
EditorCoroutine( IEnumerator _routine )
{
routine = _routine;
}
void start()
{
//Debug.Log("start");
EditorApplication.update += update;
}
public void stop()
{
//Debug.Log("stop");
EditorApplication.update -= update;
}
void update()
{
/* NOTE: no need to try/catch MoveNext,
* if an IEnumerator throws its next iteration returns false.
* Also, Unity probably catches when calling EditorApplication.update.
*/
//Debug.Log("update");
if (!routine.MoveNext())
{
stop();
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
using UnityEditor;
using Random = UnityEngine.Random;
namespace Swing.Editor.Test
{
public class TestEditorCoroutine
{
[MenuItem("Swing/Test/Test Editor Coroutine")]
static void testEditorCoroutine()
{
EditorCoroutine.start(testRoutine());
}
static IEnumerator testRoutine()
{
Debug.Log("hello " + DateTime.Now.Ticks);
yield return null;
Debug.Log("done " + DateTime.Now.Ticks);
}
[MenuItem("Swing/Test/Test Editor Coroutine With Exception")]
static void testEditorCoroutineWithException()
{
EditorCoroutine.start(testRoutineWithException());
}
static IEnumerator testRoutineWithException()
{
Debug.Log("hello " + DateTime.Now.Ticks);
yield return null;
for (int i = 0; i < 10; i++)
{
testRandomException();
yield return null;
}
Debug.Log("done " + DateTime.Now.Ticks);
}
static void testRandomException()
{
if (Random.value < 0.3f)
{
throw new Exception("ahah! " + DateTime.Now.Ticks);
}
else
{
Debug.Log("ok " + DateTime.Now.Ticks);
}
}
}
}
@marijnz

This comment has been minimized.

Copy link

@marijnz marijnz commented Jan 9, 2015

I made a plugin that supports all of the functionality of Unity's default coroutines in Editor code:http://forum.unity3d.com/threads/released-editor-coroutines.289703/

Your code will work with the basic things, but stuff like WaitForSeconds won't work. So that's why I decided so make something that really supports it all :)

@pointcache

This comment has been minimized.

Copy link

@pointcache pointcache commented Jun 1, 2016

broken

@ceilwoo

This comment has been minimized.

Copy link

@ceilwoo ceilwoo commented Jun 28, 2016

works perfect, Thanks!

@padmalcom

This comment has been minimized.

Copy link

@padmalcom padmalcom commented Jan 22, 2017

Hi, thanks for the code. How would you add a return type for the enumerator?

@StephanSchue

This comment has been minimized.

Copy link

@StephanSchue StephanSchue commented Jul 10, 2017

Works perfect for me. Thanks! :)

@HelloKitty

This comment has been minimized.

Copy link

@HelloKitty HelloKitty commented Aug 11, 2017

Interesting solution!

@marijnz

This comment has been minimized.

Copy link

@marijnz marijnz commented Oct 28, 2017

As a followup on my previous comment, it's now available on Github as well: https://github.com/marijnz/unity-editor-coroutines

@Pyrozhok4u

This comment has been minimized.

Copy link

@Pyrozhok4u Pyrozhok4u commented Jul 29, 2020

Works for my Assembly builder. MenuItem compiles several DLLs from my project code. I used EditorCoroutine to wait until UnityEditor finishes the compilation process to start a new Assembly build. Thanks mate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.