Guest User

Untitled

a guest
Apr 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Threading.Tasks;
  4. using UnityEngine;
  5.  
  6. namespace Hoge
  7. {
  8. public class CoroutineRunner : MonoBehaviour
  9. {
  10. private static MonoBehaviour instance;
  11.  
  12. public static Task<T> Run<T>(Func<TaskCompletionSource<T>, IEnumerator> coroutine)
  13. {
  14. if (instance == null)
  15. {
  16. var go = new GameObject("CoroutineRunner");
  17. instance = go.AddComponent<CoroutineRunner>();
  18. DontDestroyOnLoad(go);
  19. }
  20.  
  21. var taskCompletionSource = new TaskCompletionSource<T>();
  22. instance.StartCoroutine(coroutine(taskCompletionSource));
  23. return taskCompletionSource.Task;
  24. }
  25. }
  26. }
Add Comment
Please, Sign In to add comment