异步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;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace VersionFlow.Runtime
|
namespace VersionFlow.Runtime
|
||||||
{
|
{
|
||||||
public abstract class LoadAssetRequest
|
public abstract class LoadAssetRequest : CustomYieldInstruction
|
||||||
{
|
{
|
||||||
protected IEnumerator enumerator;
|
protected IEnumerator enumerator;
|
||||||
protected Coroutine coroutine;
|
protected Coroutine coroutine;
|
||||||
@ -13,14 +14,12 @@ namespace VersionFlow.Runtime
|
|||||||
public bool Error => IsDone && asset == null;
|
public bool Error => IsDone && asset == null;
|
||||||
|
|
||||||
internal UnityEngine.Object asset;
|
internal UnityEngine.Object asset;
|
||||||
public event Action OnComplete;
|
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
public string StackInfoInEditor;
|
public string StackInfoInEditor;
|
||||||
#endif
|
#endif
|
||||||
public LoadAssetRequest(IEnumerator enumerator)
|
public LoadAssetRequest(IEnumerator enumerator)
|
||||||
{
|
{
|
||||||
this.enumerator = enumerator;
|
this.enumerator = enumerator;
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
StackInfoInEditor = string.Empty;
|
StackInfoInEditor = string.Empty;
|
||||||
var st = new System.Diagnostics.StackTrace(true);
|
var st = new System.Diagnostics.StackTrace(true);
|
||||||
@ -35,11 +34,6 @@ namespace VersionFlow.Runtime
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void RaiseOnComplete()
|
|
||||||
{
|
|
||||||
OnComplete?.Invoke();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void Start()
|
internal void Start()
|
||||||
{
|
{
|
||||||
if (IsDone) return;
|
if (IsDone) return;
|
||||||
@ -58,9 +52,10 @@ namespace VersionFlow.Runtime
|
|||||||
}
|
}
|
||||||
|
|
||||||
asset = null;
|
asset = null;
|
||||||
IsDone = false;
|
IsDone = true;
|
||||||
}
|
|
||||||
|
|
||||||
|
RaiseTaskComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
internal virtual IEnumerator Task()
|
internal virtual IEnumerator Task()
|
||||||
@ -76,41 +71,53 @@ namespace VersionFlow.Runtime
|
|||||||
IsDone = true;
|
IsDone = true;
|
||||||
coroutine = null;
|
coroutine = null;
|
||||||
|
|
||||||
OnComplete?.Invoke();
|
RaiseTaskComplete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected abstract void RaiseTaskComplete();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class LoadAssetRequest<T> : LoadAssetRequest where T : UnityEngine.Object
|
public class LoadAssetRequest<T> : LoadAssetRequest where T : UnityEngine.Object
|
||||||
{
|
{
|
||||||
public T Asset => asset as T;
|
TaskCompletionSource<T> loadTaskSrc;
|
||||||
|
public T Asset
|
||||||
|
{
|
||||||
public LoadAssetRequest(IEnumerator enumerator) : base(enumerator)
|
get
|
||||||
{
|
{
|
||||||
|
if (!IsDone)
|
||||||
}
|
{
|
||||||
}
|
throw new Exception("Asset Load not Finish");
|
||||||
|
}
|
||||||
public class InstantiateRequest : LoadAssetRequest<GameObject>
|
var result = asset as T;
|
||||||
{
|
return result;
|
||||||
public InstantiateRequest(IEnumerator enumerator) : base(enumerator) { }
|
}
|
||||||
|
}
|
||||||
internal override IEnumerator Task()
|
|
||||||
{
|
|
||||||
yield return null;//等待一帧
|
|
||||||
|
public LoadAssetRequest(IEnumerator enumerator) : base(enumerator)
|
||||||
while (enumerator.MoveNext()) yield return enumerator.Current;
|
{
|
||||||
#if UNITY_EDITOR
|
loadTaskSrc = new TaskCompletionSource<T>();
|
||||||
if (enumerator.Current == null)
|
}
|
||||||
{
|
|
||||||
VersionFlowX.Error(StackInfoInEditor);
|
public Task<T> GetAsset()
|
||||||
}
|
{
|
||||||
#endif
|
return loadTaskSrc.Task;
|
||||||
asset = enumerator.Current as UnityEngine.Object;
|
}
|
||||||
IsDone = true;
|
|
||||||
coroutine = null;
|
protected override void RaiseTaskComplete()
|
||||||
|
{
|
||||||
RaiseOnComplete();
|
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