/// <summary>This function is called when the MonoBehaviour will be destroyed.</summary>
 public UniTask OnDestroyAsync()
 {
     if (called)
     {
         return(UniTask.CompletedTask);
     }
     return(new UniTask(promise ?? (promise = new UniTaskCompletionSource())));
 }
Exemple #2
0
 public virtual async UniTask <bool> WaitForInputAsync()
 {
     if (onInputTCS is null)
     {
         onInputTCS = new UniTaskCompletionSource <bool>();
     }
     return(await onInputTCS.Task);
 }
Exemple #3
0
 public virtual async UniTask WaitForInputEndAsync()
 {
     if (onInputEndTCS is null)
     {
         onInputEndTCS = new UniTaskCompletionSource();
     }
     await onInputEndTCS.Task;
 }
Exemple #4
0
    /// <summary>
    /// 把回调模式转变成一个UniTask
    /// </summary>
    /// <returns></returns>
    public UniTask <int> ConvertEventCallBackToUniTask()
    {
        var utcs = new UniTaskCompletionSource <int>();

        //register your callback to a event, callback should call TrySetResult/TrySetCancel/TrySetException when appropriate
        utcs.TrySetResult(10);
        return(utcs.Task);
    }
Exemple #5
0
 public void SetException(Exception exception)
 {
     if (promise == null)
     {
         promise = new UniTaskCompletionSource();
     }
     promise.TrySetException(exception);
 }
Exemple #6
0
        // ================================================
        // REMOTE VERSION
        // ================================================
        #region REMOTE_VERSION
        public UniTask <string> GetRemoteVersion(string remoteSubPath)
        {
            var source = new UniTaskCompletionSource <string>();

            GetRemoteVersion(remoteSubPath,
                             result => source.TrySetResult(result)
                             );
            return(source.Task);
        }
Exemple #7
0
			public ToUniTaskObserver(UniTaskCompletionSource<T> promise, SingleAssignmentDisposable disposable, CancellationToken cancellationToken) {
				this.promise = promise;
				this.disposable = disposable;
				this.cancellationToken = cancellationToken;

				if (this.cancellationToken.CanBeCanceled) {
					this.registration = this.cancellationToken.RegisterWithoutCaptureExecutionContext(callback, this);
				}
			}
Exemple #8
0
        public static UniTask <bool> CheckURL(string url)
        {
            var source = new UniTaskCompletionSource <bool>();

            CheckURL(url,
                     result => source.TrySetResult(result)
                     );
            return(source.Task);
        }
		public UniTask StartAsync() {
			if (called) return UniTask.CompletedTask;

			if (!awakeCalled) {
				PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
			}

			return new UniTask(promise ?? (promise = new UniTaskCompletionSource()));
		}
Exemple #10
0
        public async UniTask <string> GetName()
        {
            _root.SetActive(true);
            _taskCompletionSource = new UniTaskCompletionSource <string>();
            var yourName = await _taskCompletionSource.Task;

            _root.SetActive(false);
            return(yourName);
        }
 public UniTask AwakeAsync()
 {
     if (called)
     {
         return(UniTask.CompletedTask);
     }
     PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
     return(new UniTask(promise ?? (promise = new UniTaskCompletionSource())));
 }
Exemple #12
0
    private void OnChoiceSelected(UniTaskCompletionSource <bool> tcs, bool choice)
    {
        yesButton.onClick.RemoveAllListeners();
        noButton.onClick.RemoveAllListeners();
        backgroundButton.onClick.RemoveAllListeners();

        parentObject.SetActive(false);
        tcs.TrySetResult(choice);
    }
Exemple #13
0
        /// <summary>
        /// Initializes the engine behaviour and services.
        /// Services will be initialized in the order in which they were added to the list.
        /// </summary>
        /// <param name="configurationProvider">Configuration provider to use when resolving configuration objects.</param>
        /// <param name="behaviour">Unity's <see cref="MonoBehaviour"/> proxy to use.</param>
        /// <param name="services">List of engine services to initialize (order will be preserved).</param>
        public static async UniTask InitializeAsync(IConfigurationProvider configurationProvider, IEngineBehaviour behaviour, IList <IEngineService> services)
        {
            if (Initialized)
            {
                return;
            }
            if (Initializing)
            {
                await initializeTCS.Task;
                return;
            }

            initializeTCS = new UniTaskCompletionSource();
            OnInitializationStarted?.Invoke();

            for (int i = preInitializationTasks.Count - 1; i >= 0; i--)
            {
                await preInitializationTasks[i]();
                if (!Initializing)
                {
                    return;                // In case initialization process was terminated (eg, exited playmode).
                }
            }

            Engine.configurationProvider = configurationProvider;
            Configuration = GetConfiguration <EngineConfiguration>();

            Behaviour = behaviour;
            Behaviour.OnBehaviourDestroy += Destroy;

            objects.Clear();
            Engine.services.Clear();
            Engine.services.AddRange(services);

            for (var i = 0; i < Engine.services.Count; i++)
            {
                OnInitializationProgress?.Invoke(i / (float)Engine.services.Count);
                await Engine.services[i].InitializeServiceAsync();
                if (!Initializing)
                {
                    return;
                }
            }

            for (int i = postInitializationTasks.Count - 1; i >= 0; i--)
            {
                await postInitializationTasks[i]();
                if (!Initializing)
                {
                    return;
                }
            }

            initializeTCS?.TrySetResult();
            OnInitializationFinished?.Invoke();
        }
Exemple #14
0
        // ================================================
        // INITIALIZATION
        // ================================================
        public UniTask Init(object[] data)
        {
            var source = new UniTaskCompletionSource();

            Init(data,
                 () => source.TrySetResult(),
                 error => source.TrySetException(error)
                 );
            return(source.Task);
        }
Exemple #15
0
		public void SetResult() {
			if (moveNext == null) {
			} else {
				if (promise == null) {
					promise = new UniTaskCompletionSource();
				}

				promise.TrySetResult();
			}
		}
Exemple #16
0
        public UniTask SetLocalVersion(string subPath, string version)
        {
            var source = new UniTaskCompletionSource();

            SetLocalVersion(subPath, version,
                            () => source.TrySetResult(),
                            error => source.TrySetException(error)
                            );
            return(source.Task);
        }
Exemple #17
0
            public UniTask <string> GetVersionFileName(string fileName)
            {
                var source = new UniTaskCompletionSource <string>();

                versionFileNomenclatureMethod(fileName,
                                              result => source.TrySetResult(result),
                                              error => source.TrySetException(error)
                                              );
                return(source.Task);
            }
Exemple #18
0
        // DELETE
        public UniTask <bool> DeleteLocalFile(string subPath)
        {
            var source = new UniTaskCompletionSource <bool>();

            DeleteLocalFile(subPath,
                            result => source.TrySetResult(result),
                            error => source.TrySetException(error)
                            );
            return(source.Task);
        }
Exemple #19
0
        public override UniTask <Sprite> Get(string location, Texture2DCompression compression, Picture instance)
        {
            var source = new UniTaskCompletionSource <Sprite>();

            Get(location, compression, instance,
                result => source.TrySetResult(result),
                exception => source.TrySetException(exception)
                );
            return(source.Task);
        }
Exemple #20
0
        public UniTask <ReadLocalFileResult> ReadLocalFile(string localSubPath)
        {
            var source = new UniTaskCompletionSource <ReadLocalFileResult>();

            ReadLocalFile(localSubPath,
                          result => source.TrySetResult(result),
                          error => source.TrySetException(error)
                          );
            return(source.Task);
        }
Exemple #21
0
    public UniTask <bool> ShowAsync()
    {
        var tcs = new UniTaskCompletionSource <bool>();

        OnOk(() => { tcs.TrySetResult(true); });
        OnCancel(() => { tcs.TrySetResult(false); });
        Show();

        return(tcs.Task);
    }
Exemple #22
0
        public UniTask <byte[]> Download(string url)
        {
            var task = new UniTaskCompletionSource <byte[]>();

            Download(url,
                     response => task.TrySetResult(response),
                     error => task.TrySetException(error)
                     );
            return(task.Task);
        }
Exemple #23
0
        // ================================================
        // LOCAL VERSION
        // ================================================
        #region LOCAL_VERSION
        public UniTask <string> GetLocalVersion(string subPath)
        {
            var source = new UniTaskCompletionSource <string>();

            GetLocalVersion(subPath,
                            result => source.TrySetResult(result),
                            error => source.TrySetException(error)
                            );
            return(source.Task);
        }
Exemple #24
0
        public UniTask <bool> UpdateFile(string localSubPath, string remoteSubPath)
        {
            var source = new UniTaskCompletionSource <bool>();

            UpdateFile(localSubPath, remoteSubPath,
                       result => source.TrySetResult(result),
                       error => source.TrySetException(error)
                       );
            return(source.Task);
        }
Exemple #25
0
        /// <summary>
        /// Downloads an image form the URL and returns the results using UniTask
        /// </summary>
        /// <param name="path"></param>
        /// <returns>A UniTask<Texture2D> instance</Texture2D></returns>
        public UniTask <Texture2D> Download(string path)
        {
            var source = new UniTaskCompletionSource <Texture2D>();

            Download(path,
                     result => source.TrySetResult(result),
                     exception => source.TrySetException(exception)
                     );
            return(source.Task);
        }
Exemple #26
0
        public override UniTask Free(string location, Texture2DCompression compression, Picture instance)
        {
            var source = new UniTaskCompletionSource <bool>();

            Free(location, compression, instance,
                 () => source.TrySetResult(true),
                 exception => source.TrySetException(exception)
                 );
            return(source.Task);
        }
Exemple #27
0
        public UniTask ConnectAsync(Uri uri)
        {
            clients[id]             = this;
            connectCompletionSource = new UniTaskCompletionSource();

            this.uri = uri;

            nativeRef = SocketCreate(uri.ToString(), id, OnOpen, OnData, OnClose);
            return(connectCompletionSource.Task);
        }
    public static UniTask <T> .Awaiter GetAwaiter <T>(this IAsyncOperation <T> asyncOperation)
    {
        var completion = new UniTaskCompletionSource <T>();

        asyncOperation.Completed += (operation) =>
        {
            completion.TrySetResult(operation.Result);
        };
        return(completion.Task.GetAwaiter());
    }
Exemple #29
0
        // WRITE
        public UniTask WriteLocalFile(string subPath, byte[] bytes, string version)
        {
            var source = new UniTaskCompletionSource();

            WriteLocalFile(subPath, bytes, version,
                           () => source.TrySetResult(),
                           error => source.TrySetException(error)
                           );
            return(source.Task);
        }
Exemple #30
0
    private async UniTask MainGameFlow()
    {
        var titleUi  = new TitleUiElements(titleUiCache);
        var quizUi   = new QuizUiElements(quizUiCache);
        var resultUi = new ResultUiElements(resultUiCache);

        while (true)
        {
            titleUi.Root.SetActive(true);
            quizUi.Root.SetActive(false);
            resultUi.Root.SetActive(false);

            // Title

            var origin = titleUi.TitleText.rectTransform.anchoredPosition;
            await Easing.Create <OutBounce>(origin + new Vector2(0f, 600f), origin, 0.5f).ToAnchoredPosition(titleUi.TitleText);

            await titleUi.StartButton.OnClickAsync();

            titleUi.Root.SetActive(false);
            quizUi.Root.SetActive(true);

            // Quiz

            quizUi.QuizText.text = "6 * 4 = ";
            var buttonClick = new UniTaskCompletionSource <int>();
            using (var editor = quizUi.SelectList.Edit())
            {
                for (var i = 0; i < 100; i++)
                {
                    var i1 = i;
                    editor.Contents.Add(new UIFactory <SelectButtonUiElements>(x =>
                    {
                        x.Text.text = $"{i1}";
                        x.Button.onClick.AddListener(() => buttonClick.TrySetResult(i1));
                    }));
                }
            }

            var index = await buttonClick.Task;

            quizUi.Root.SetActive(false);
            resultUi.Root.SetActive(true);

            // Result

            resultUi.Circle.SetActive(index == 24);
            resultUi.Cross.SetActive(index != 24);

            var animator = Easing.Create <OutBounce>(Vector3.one * 5f, Vector3.one, 0.5f);
            await UniTask.WhenAll(animator.ToLocalScale(resultUi.Circle), animator.ToLocalScale(resultUi.Cross));

            await resultUi.ReturnButton.OnClickAsync();
        }
    }