异步loadasset和instatiate现在支持async await 语法
LoadRequest和InstatiateRequest支持yield return
This commit is contained in:
parent
c040d7fbc9
commit
886e884958
@ -1,10 +1,11 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace VersionFlow.Runtime
|
||||
{
|
||||
public abstract class LoadAssetRequest
|
||||
public abstract class LoadAssetRequest : CustomYieldInstruction
|
||||
{
|
||||
protected IEnumerator enumerator;
|
||||
protected Coroutine coroutine;
|
||||
@ -13,14 +14,12 @@ namespace VersionFlow.Runtime
|
||||
public bool Error => IsDone && asset == null;
|
||||
|
||||
internal UnityEngine.Object asset;
|
||||
public event Action OnComplete;
|
||||
#if UNITY_EDITOR
|
||||
public string StackInfoInEditor;
|
||||
#endif
|
||||
public LoadAssetRequest(IEnumerator enumerator)
|
||||
{
|
||||
this.enumerator = enumerator;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
StackInfoInEditor = string.Empty;
|
||||
var st = new System.Diagnostics.StackTrace(true);
|
||||
@ -35,11 +34,6 @@ namespace VersionFlow.Runtime
|
||||
#endif
|
||||
}
|
||||
|
||||
protected void RaiseOnComplete()
|
||||
{
|
||||
OnComplete?.Invoke();
|
||||
}
|
||||
|
||||
internal void Start()
|
||||
{
|
||||
if (IsDone) return;
|
||||
@ -58,9 +52,10 @@ namespace VersionFlow.Runtime
|
||||
}
|
||||
|
||||
asset = null;
|
||||
IsDone = false;
|
||||
}
|
||||
IsDone = true;
|
||||
|
||||
RaiseTaskComplete();
|
||||
}
|
||||
|
||||
|
||||
internal virtual IEnumerator Task()
|
||||
@ -76,41 +71,53 @@ namespace VersionFlow.Runtime
|
||||
IsDone = true;
|
||||
coroutine = null;
|
||||
|
||||
OnComplete?.Invoke();
|
||||
RaiseTaskComplete();
|
||||
}
|
||||
|
||||
protected abstract void RaiseTaskComplete();
|
||||
}
|
||||
|
||||
public class LoadAssetRequest<T> : LoadAssetRequest where T : UnityEngine.Object
|
||||
{
|
||||
public T Asset => asset as T;
|
||||
|
||||
|
||||
public LoadAssetRequest(IEnumerator enumerator) : base(enumerator)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class InstantiateRequest : LoadAssetRequest<GameObject>
|
||||
{
|
||||
public InstantiateRequest(IEnumerator enumerator) : base(enumerator) { }
|
||||
|
||||
internal override IEnumerator Task()
|
||||
{
|
||||
yield return null;//等待一帧
|
||||
|
||||
while (enumerator.MoveNext()) yield return enumerator.Current;
|
||||
#if UNITY_EDITOR
|
||||
if (enumerator.Current == null)
|
||||
{
|
||||
VersionFlowX.Error(StackInfoInEditor);
|
||||
}
|
||||
#endif
|
||||
asset = enumerator.Current as UnityEngine.Object;
|
||||
IsDone = true;
|
||||
coroutine = null;
|
||||
|
||||
RaiseOnComplete();
|
||||
}
|
||||
TaskCompletionSource<T> loadTaskSrc;
|
||||
public T Asset
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!IsDone)
|
||||
{
|
||||
throw new Exception("Asset Load not Finish");
|
||||
}
|
||||
var result = asset as T;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public LoadAssetRequest(IEnumerator enumerator) : base(enumerator)
|
||||
{
|
||||
loadTaskSrc = new TaskCompletionSource<T>();
|
||||
}
|
||||
|
||||
public Task<T> GetAsset()
|
||||
{
|
||||
return loadTaskSrc.Task;
|
||||
}
|
||||
|
||||
protected override void RaiseTaskComplete()
|
||||
{
|
||||
if (Error)
|
||||
loadTaskSrc.TrySetCanceled();
|
||||
else
|
||||
loadTaskSrc.TrySetResult(asset as T);
|
||||
}
|
||||
|
||||
public override bool keepWaiting => !IsDone;
|
||||
}
|
||||
|
||||
public sealed class InstantiateRequest : LoadAssetRequest<GameObject>
|
||||
{
|
||||
public InstantiateRequest(IEnumerator enumerator) : base(enumerator) { }
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user