12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- namespace YooAsset
- {
- public abstract class AsyncOperationBase : IEnumerator
- {
-
- private Action<AsyncOperationBase> _callback;
-
-
-
- public EOperationStatus Status { get; protected set; } = EOperationStatus.None;
-
-
-
- public string Error { get; protected set; }
-
-
-
- public float Progress { get; protected set; }
-
-
-
- public bool IsDone
- {
- get
- {
- return Status == EOperationStatus.Failed || Status == EOperationStatus.Succeed;
- }
- }
-
-
-
- public event Action<AsyncOperationBase> Completed
- {
- add
- {
- if (IsDone)
- value.Invoke(this);
- else
- _callback += value;
- }
- remove
- {
- _callback -= value;
- }
- }
-
-
-
- public Task Task
- {
- get
- {
- if (_taskCompletionSource == null)
- {
- _taskCompletionSource = new TaskCompletionSource<object>();
- if (IsDone)
- _taskCompletionSource.SetResult(null);
- }
- return _taskCompletionSource.Task;
- }
- }
- internal abstract void Start();
- internal abstract void Update();
- internal void Finish()
- {
- Progress = 1f;
- _callback?.Invoke(this);
- if (_taskCompletionSource != null)
- _taskCompletionSource.TrySetResult(null);
- }
- #region 异步编程相关
- bool IEnumerator.MoveNext()
- {
- return !IsDone;
- }
- void IEnumerator.Reset()
- {
- }
- object IEnumerator.Current => null;
- private TaskCompletionSource<object> _taskCompletionSource;
- #endregion
- }
- }
|