From 39a051eacf98286923878eec5c9b7c4adfa102fa Mon Sep 17 00:00:00 2001 From: ninemine <1371605831@qq.com> Date: Mon, 15 Dec 2025 17:59:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EPrefabRootObjectConfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scripts/Environment/PrefabRootObject.cs | 69 +++++++++++++------ 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/Assets/Scripts/Environment/PrefabRootObject.cs b/Assets/Scripts/Environment/PrefabRootObject.cs index 1109dbb..7361643 100644 --- a/Assets/Scripts/Environment/PrefabRootObject.cs +++ b/Assets/Scripts/Environment/PrefabRootObject.cs @@ -1,10 +1,30 @@ +using Convention; using Demo.Game.Attr; using System.Collections; using System.Collections.Generic; +using System.IO; using UnityEngine; namespace Demo.Game { + namespace ConfigType + { + public class PrefabRootObjectConfig : ScriptLoadableConfig + { + private readonly Dictionary> LoadedGameObjectNames = new(); + + public override void Deserialize(BinaryReader reader) + { + base.Deserialize(reader); + } + + public override void Serialize(BinaryWriter writer) + { + base.Serialize(writer); + } + } + } + [Scriptable] public class PrefabRootObject : ScriptableObject, IAssetBundleLoader { @@ -13,15 +33,26 @@ namespace Demo.Game return new GameObject().AddComponent(); } - private readonly List AssetBundles = new(); - private readonly List Prefabs = new(); + private int LoadingCounter = 0; + private readonly Dictionary> LoadedGameObjectNames = new(); + private readonly List LoadedGameObjects = new(); + + protected override IEnumerator DoSomethingDuringApplyScript() + { + yield return base.DoSomethingDuringApplyScript(); + yield return new WaitUntil(() => LoadingCounter == 0); + } public override IEnumerator UnloadScript() { yield return base.UnloadScript(); - foreach (var ab in AssetBundles) + foreach (var obj in LoadedGameObjects) { - yield return this.UnloadAssetBundle(ab); + Destroy(obj); + } + foreach (var item in LoadedGameObjectNames) + { + yield return this.UnloadAssetBundle(item.Key); } } @@ -31,31 +62,27 @@ namespace Demo.Game /// /// [Convention.RScript.Variable.Attr.Method] - public IEnumerator Load(string ab, string prefab) + public void Load(string ab, string prefab) { - AssetBundle assetBundle = null; - yield return this.LoadAssetBundle(ab, x => assetBundle = x); - GameObject prefabObject = null; - if (assetBundle != null) + LoadingCounter++; + ConventionUtility.StartCoroutine(this.LoadAssetBundle(ab, assetBundle => { - var ir = assetBundle.LoadAssetAsync(prefab); - yield return ir; - if (ir.asset != null) + GameObject prefabObject = null; + if (assetBundle != null) { - prefabObject = Instantiate(ir.asset as GameObject); - Prefabs.Add(prefabObject); + prefabObject = Instantiate(assetBundle.LoadAsset(prefab)); + LoadedGameObjects.Add(prefabObject); prefabObject.transform.SetParent(transform); - AssetBundles.Add(ab); + if (LoadedGameObjectNames.ContainsKey(ab) == false) + LoadedGameObjectNames.Add(ab, new()); + LoadedGameObjectNames[ab].Add(prefab); } else { - Debug.LogError($"Load Prefab failed", this); + Debug.LogError($"Load AssetBundle failed", this); } - } - else - { - Debug.LogError($"Load AssetBundle failed", this); - } + LoadingCounter--; + })); } } }