Skip to content

Commit

Permalink
Add edit-time support to AsyncCoroutineRunner
Browse files Browse the repository at this point in the history
If the package com.unity.editorcoroutine is installed,
AsyncCoroutineRunner gains the ability to work at edit time.

In Unity 2019.1 and above, no additional setup is needed.
Otherwise, the symbol HAVE_EDITOR_COROUTINES must also be
manually defined.

Includes unit test.
Addresses part of modesttree#9
  • Loading branch information
dubois committed Dec 4, 2019
1 parent 5f948fd commit 5938bc4
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "AsyncAwaitUtil",
"references": [
"GUID:478a2357cc57436488a56e564b08d223"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [
{
"name": "com.unity.editorcoroutines",
"expression": "0.0.2-preview",
"define": "HAVE_EDITOR_COROUTINES"
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

using UnityEngine;
#if UNITY_EDITOR && HAVE_EDITOR_COROUTINES
using Unity.EditorCoroutines.Editor;
#endif

namespace UnityAsyncAwaitUtil
{
public class AsyncCoroutineRunner : MonoBehaviour
public class AsyncCoroutineRunner : MonoBehaviour, AsyncCoroutineRunner.ICoroutineRunner
{
static AsyncCoroutineRunner _instance;
public interface ICoroutineRunner
{
object StartCoroutine(IEnumerator routine);
}

#if UNITY_EDITOR
class EditorAsyncCoroutineRunner : ICoroutineRunner
{
object ICoroutineRunner.StartCoroutine(IEnumerator routine)
{
#if HAVE_EDITOR_COROUTINES
return EditorCoroutineUtility.StartCoroutine(routine, this);
#elif UNITY_2019_1_OR_NEWER
throw new NotImplementedException("Install package com.unity.editorcoroutines");
#else
// asmdef "Version Defines" support doesn't exist yet
throw new NotImplementedException("Install package com.unity.editorcoroutines and define HAVE_EDITOR_COROUTINES");
#endif
}
}
#endif

static ICoroutineRunner _instance;

public static AsyncCoroutineRunner Instance
public static ICoroutineRunner Instance
{
get
{
#if UNITY_EDITOR
if (_instance == null && !Application.isPlaying)
{
_instance = new EditorAsyncCoroutineRunner();
}
#endif
if (_instance == null)
{
_instance = new GameObject("AsyncCoroutineRunner")
Expand All @@ -31,5 +61,10 @@ void Awake()

DontDestroyOnLoad(gameObject);
}

object ICoroutineRunner.StartCoroutine(IEnumerator routine)
{
return StartCoroutine(routine);
}
}
}
}
8 changes: 8 additions & 0 deletions UnityProject/Assets/Plugins/AsyncAwaitUtil/Tests/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections;
using System.Collections.Generic;

using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;

namespace UnityAsyncAwaitUtil
{
class TestMisc
{
[UnityTest]
public IEnumerator TestAsyncCoroutineRunnerInEditor()
{
List<bool> result = new List<bool>();
AsyncCoroutineRunner.Instance.StartCoroutine(AppendCoroutine(result));
for (int i = 0; i < 10; ++i)
{
if (result.Count > 0) { yield break; }
yield return null;
}
Assert.Fail("Coroutine did not complete");
}

static IEnumerator AppendCoroutine(List<bool> result)
{
yield return null;
result.Add(true);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5938bc4

Please sign in to comment.