Skip to content

Instantly share code, notes, and snippets.

@kyubuns
Created April 18, 2018 00:50
Show Gist options
  • Select an option

  • Save kyubuns/a4af575d164dd0d8328b06e4fad4a8f5 to your computer and use it in GitHub Desktop.

Select an option

Save kyubuns/a4af575d164dd0d8328b06e4fad4a8f5 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
namespace Hoge
{
public class CoroutineRunner : MonoBehaviour
{
private static MonoBehaviour instance;
public static Task<T> Run<T>(Func<TaskCompletionSource<T>, IEnumerator> coroutine)
{
if (instance == null)
{
var go = new GameObject("CoroutineRunner");
instance = go.AddComponent<CoroutineRunner>();
DontDestroyOnLoad(go);
}
var taskCompletionSource = new TaskCompletionSource<T>();
instance.StartCoroutine(coroutine(taskCompletionSource));
return taskCompletionSource.Task;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment