예제 #1
0
 public void Complete(Exception exception)
 {
     _exception  = exception;
     isCompleted = true;
     if (_continuation != null)
     {
         UnityTaskUtil.RunOnUnityThread(_continuation);
     }
 }
예제 #2
0
        private static async Task <bool> TestAwaitCoroutineAsync()
        {
            var  hostBehaviour = new GameObject("Test").AddComponent <TestHostBehaviour>();
            bool result        = false;
            await UnityTaskUtil.StartCoroutineAsync(LongRunningCoroutine(() => { result = true; }), hostBehaviour);

            //If we don't await till the coroutine is fully complete result will still be false here
            return(result);
        }
예제 #3
0
        public IEnumerator CanStartCoroutineFromBackgroundThread()
        {
            var hostBehaviour = new GameObject("Test").AddComponent <TestHostBehaviour>();
            var result        = false;
            var task          = Task.Run(() =>
            {
                //Start coroutine and block task thread until coroutine is complete
                Assert.IsFalse(UnityTaskUtil.CurrentThreadIsUnityThread);
                UnityTaskUtil.StartCoroutineAsync(LongRunningCoroutine(() => { result = true; }), hostBehaviour).GetAwaiter().GetResult();
            });

            yield return(new WaitUntil(() => task.IsCompleted));

            Assert.IsTrue(result);
        }
예제 #4
0
        public IEnumerator InstantiateAsync()
        {
            var        prefab = new GameObject();
            GameObject clone  = null;

            //Call instantiate from background thread
            var task = Task.Run(() =>
            {
                clone = UnityTaskUtil.InstantiateAsync(prefab).Result;
            });

            yield return(new WaitUntil(() => task.IsCompleted));

            Assert.IsTrue(clone != null);
        }
예제 #5
0
        public IEnumerator InstantiateAsync_WithParent()
        {
            var        prefab = new GameObject();
            var        parent = new GameObject().transform;
            GameObject clone  = null;

            //Call instantiate from background thread
            var task = Task.Run(() =>
            {
                clone = UnityTaskUtil.InstantiateAsync(prefab, parent).Result;
            });

            yield return(new WaitUntil(() => task.IsCompleted));

            Assert.IsTrue(clone != null && clone.transform.parent == parent);
        }
예제 #6
0
        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);
        }
예제 #7
0
        public IEnumerator StartCoroutineAsync()
        {
            var testHost = new GameObject().AddComponent <TestHostBehaviour>();
            int count    = 0;
            int countTo  = 3;

            var task = Task.Run(() =>
            {
                var coroutine = UnityTaskUtil.StartCoroutineAsync(TestCoroutine(() =>
                {
                    count++;
                    return(count < countTo);
                }), testHost);
                //Wait for coroutine to finish
                coroutine.GetAwaiter().GetResult();
            });

            yield return(new WaitUntil(() => task.IsCompleted));

            Assert.IsTrue(count >= countTo);
        }
예제 #8
0
        public IEnumerator StartCancelableCoroutine()
        {
            var testHost          = new GameObject().AddComponent <TestHostBehaviour>();
            var cancelTokenSource = new CancellationTokenSource();
            var token             = cancelTokenSource.Token;

            var task = Task.Run(() =>
            {
                var coroutine = UnityTaskUtil.StartCancelableCoroutineAsync(TestForeverCoroutine(), testHost, token);
                coroutine.GetAwaiter().GetResult();
            });

            yield return(null);

            Assert.IsFalse(task.IsCompleted);

            cancelTokenSource.Cancel();

            yield return(null);

            Assert.IsTrue(task.IsCompleted);
        }
예제 #9
0
        public IEnumerator RunOnUnityThread()
        {
            bool checkOne = false;
            bool checkTwo = false;

            //Post the first check to the context
            UnityTaskUtil.RunOnUnityThread(() => { checkOne = UnityTaskUtil.CurrentThreadIsUnityThread; });
            //Wait a frame so it has a chance to run
            yield return(null);

            var task = Task.Run(() =>
            {
                UnityTaskUtil.RunOnUnityThread(() => { checkTwo = UnityTaskUtil.CurrentThreadIsUnityThread; });
            });

            //Wait for the task to run and post the function to the context
            task.Wait();
            //Wait a frame for the posted function to get called on the unity context
            yield return(null);

            Assert.IsTrue(checkOne);
            Assert.IsTrue(checkTwo);
        }
예제 #10
0
 /// <summary>
 /// Use this method to start a coroutine from any thread.
 /// Coroutines themselves always run on the main thread
 /// </summary>
 /// <param name="enumerator">coroutine to run</param>
 public static void Start(IEnumerator enumerator)
 {
     UnityTaskUtil.RunOnUnityThread(() => Run(enumerator, cancellationTokenSource.Token));
 }
예제 #11
0
 /// <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));
 }
예제 #12
0
 /// <summary>
 /// Use this method to start a coroutine from any thread.
 /// Coroutines themselves always run on the main thread
 /// </summary>
 /// <param name="enumerator">coroutine to run</param>
 public static void Start(IEnumerator enumerator)
 {
     UnityTaskUtil.RunOnUnityThread(async() => await RunAsync(enumerator, cancellationTokenSource.Token).ConfigureAwait(false));
 }
예제 #13
0
 /// <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));
 }