Add support for unit-testing async code in the editor by dubois · Pull Request #20 · modesttree/Unity3dAsyncAwaitUtil · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace UnityAsyncAwaitUtil
{
public static class SyncContextUtil
{
#if UNITY_EDITOR
[UnityEditor.InitializeOnLoadMethod]
#endif
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void Install()
{
Expand Down
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.

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