public IEnumerator RunOnUnityThreadAsync() { var task1 = UnityTaskUtil.RunOnUnityThreadAsync(() => UnityTaskUtil.CurrentThreadIsUnityThread); Assert.IsTrue(task1.Result); var task2 = Task.Run(() => { return(UnityTaskUtil.RunOnUnityThreadAsync(() => UnityTaskUtil.CurrentThreadIsUnityThread).Result); }); //Need to do a coroutine here because task2 waits on the result of the thing running on the main thread //so we can't block the main thread till it's done yield return(new WaitUntil(() => task2.IsCompleted)); Assert.IsTrue(task2.Result); }
/// <summary> /// Use this method when you want to await coroutine completion. /// Coroutine itself will always run on the main thread. /// </summary> /// <param name="routine">coroutine to run</param> /// <returns>awaitable task</returns> public static async Task RunAsync(IEnumerator routine) { await UnityTaskUtil.RunOnUnityThreadAsync(() => Run(routine, cancellationTokenSource.Token)); }
/// <summary> /// Use this method when you want to await coroutine completion. /// Coroutine itself will always run on the main thread. /// </summary> /// <param name="routine">coroutine to run</param> /// <returns>awaitable task</returns> public static async Task RunAsync(IEnumerator routine) { await UnityTaskUtil.RunOnUnityThreadAsync(async() => await RunAsync(routine, cancellationTokenSource.Token).ConfigureAwait(false)); }