From d8acbe6a52fe95ae164707990063d9120f14c35f Mon Sep 17 00:00:00 2001 From: ninemine <1371605831@qq.com> Date: Thu, 4 Dec 2025 16:53:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=A9=E7=A9=BA=E7=9B=92?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=B8=AD=E7=9A=84=E9=94=99=E8=AF=AF=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Environment/SkyUpdatement.cs | 61 +++++++++++++-------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/Assets/Scripts/Environment/SkyUpdatement.cs b/Assets/Scripts/Environment/SkyUpdatement.cs index e98aaac..e751706 100644 --- a/Assets/Scripts/Environment/SkyUpdatement.cs +++ b/Assets/Scripts/Environment/SkyUpdatement.cs @@ -12,6 +12,7 @@ namespace Demo.Game return new GameObject().AddComponent(); } + private int SkyAssetBundleLoaderStatus = 0; private readonly Dictionary NameCache = new(); private readonly Dictionary MaterialCache = new(); @@ -23,17 +24,24 @@ namespace Demo.Game return begin; } - [Content, SerializeField] private int Cache; + [Content, SerializeField] private int Cache = -1; + + protected override IEnumerator DoSomethingDuringApplyScript() + { + yield return base.DoSomethingDuringApplyScript(); + yield return new WaitUntil(() => SkyAssetBundleLoaderStatus == 0); + } protected override void UpdateData(int data) { if (string.IsNullOrEmpty(SkyAssetBundlePath)) return; - if (Cache < 0) - return; if (Cache != data) { - RenderSettings.skybox = MaterialCache[data]; + if (data < 0) + RenderSettings.skybox = null; + else + RenderSettings.skybox = MaterialCache[data]; Cache = data; } } @@ -52,13 +60,15 @@ namespace Demo.Game /// /// [Convention.RScript.Variable.Attr.Method] - public IEnumerator Load(string ab) + public void Load(string ab) { - yield return this.LoadAssetBundle(ab, x => - { - SkyAssetBundlePath = ab; - SkyAssetBundle = x; - }); + SkyAssetBundleLoaderStatus++; + ConventionUtility.StartCoroutine(this.LoadAssetBundle(ab, x => + { + SkyAssetBundlePath = ab; + SkyAssetBundle = x; + SkyAssetBundleLoaderStatus--; + })); } /// @@ -67,21 +77,28 @@ namespace Demo.Game /// /// [Convention.RScript.Variable.Attr.Method] - public IEnumerator Add(float time, string sky) + public void Add(float time, string sky) { - var ir = SkyAssetBundle.LoadAssetAsync(sky); - ir.completed += delegate + IEnumerator Foo() { - var mat = ir.asset as Material; - if (NameCache.TryGetValue(sky, out int id) == false) + yield return new WaitUntil(() => SkyAssetBundle != null); + var ir = SkyAssetBundle.LoadAssetAsync(sky); + ir.completed += delegate { - id = NameCache.Count; - NameCache[sky] = id; - } - MaterialCache[id] = mat; - ManualAddEntry(time, id, default); - }; - yield return ir; + var mat = ir.asset as Material; + if (NameCache.TryGetValue(sky, out int id) == false) + { + id = NameCache.Count; + NameCache[sky] = id; + } + MaterialCache[id] = mat; + ManualAddEntry(time, id, default); + SkyAssetBundleLoaderStatus--; + }; + yield return ir; + } + SkyAssetBundleLoaderStatus++; + ConventionUtility.StartCoroutine(Foo()); } } }