From 886e8849580abf6efc2d9a27026b8b75d8454601 Mon Sep 17 00:00:00 2001 From: "ALIENJACK\\alien" Date: Mon, 18 Aug 2025 16:24:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=82=E6=AD=A5loadasset=E5=92=8Cinstatiate?= =?UTF-8?q?=E7=8E=B0=E5=9C=A8=E6=94=AF=E6=8C=81async=20await=20=E8=AF=AD?= =?UTF-8?q?=E6=B3=95=20LoadRequest=E5=92=8CInstatiateRequest=E6=94=AF?= =?UTF-8?q?=E6=8C=81yield=20return?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../VersionFlow/Runtime/LoadAssetRequest.cs | 89 ++++++++++--------- 1 file changed, 48 insertions(+), 41 deletions(-) diff --git a/Assets/VersionFlow/Runtime/LoadAssetRequest.cs b/Assets/VersionFlow/Runtime/LoadAssetRequest.cs index 2505810..b49203c 100644 --- a/Assets/VersionFlow/Runtime/LoadAssetRequest.cs +++ b/Assets/VersionFlow/Runtime/LoadAssetRequest.cs @@ -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 : LoadAssetRequest where T : UnityEngine.Object { - public T Asset => asset as T; - - - public LoadAssetRequest(IEnumerator enumerator) : base(enumerator) - { - - } - } - - public class InstantiateRequest : LoadAssetRequest - { - 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 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(); + } + + public Task 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 + { + public InstantiateRequest(IEnumerator enumerator) : base(enumerator) { } } }