新增PrefabRootObjectConfig

This commit is contained in:
2025-12-15 17:59:27 +08:00
parent 0ca799ca34
commit 39a051eacf

View File

@@ -1,10 +1,30 @@
using Convention;
using Demo.Game.Attr; using Demo.Game.Attr;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using UnityEngine; using UnityEngine;
namespace Demo.Game namespace Demo.Game
{ {
namespace ConfigType
{
public class PrefabRootObjectConfig : ScriptLoadableConfig
{
private readonly Dictionary<string, List<string>> LoadedGameObjectNames = new();
public override void Deserialize(BinaryReader reader)
{
base.Deserialize(reader);
}
public override void Serialize(BinaryWriter writer)
{
base.Serialize(writer);
}
}
}
[Scriptable] [Scriptable]
public class PrefabRootObject : ScriptableObject, IAssetBundleLoader public class PrefabRootObject : ScriptableObject, IAssetBundleLoader
{ {
@@ -13,15 +33,26 @@ namespace Demo.Game
return new GameObject().AddComponent<PrefabRootObject>(); return new GameObject().AddComponent<PrefabRootObject>();
} }
private readonly List<string> AssetBundles = new(); private int LoadingCounter = 0;
private readonly List<GameObject> Prefabs = new(); private readonly Dictionary<string, List<string>> LoadedGameObjectNames = new();
private readonly List<GameObject> LoadedGameObjects = new();
protected override IEnumerator DoSomethingDuringApplyScript()
{
yield return base.DoSomethingDuringApplyScript();
yield return new WaitUntil(() => LoadingCounter == 0);
}
public override IEnumerator UnloadScript() public override IEnumerator UnloadScript()
{ {
yield return base.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
/// <param name="ab"></param> /// <param name="ab"></param>
/// <param name="prefab"></param> /// <param name="prefab"></param>
[Convention.RScript.Variable.Attr.Method] [Convention.RScript.Variable.Attr.Method]
public IEnumerator Load(string ab, string prefab) public void Load(string ab, string prefab)
{ {
AssetBundle assetBundle = null; LoadingCounter++;
yield return this.LoadAssetBundle(ab, x => assetBundle = x); ConventionUtility.StartCoroutine(this.LoadAssetBundle(ab, assetBundle =>
GameObject prefabObject = null;
if (assetBundle != null)
{ {
var ir = assetBundle.LoadAssetAsync<GameObject>(prefab); GameObject prefabObject = null;
yield return ir; if (assetBundle != null)
if (ir.asset != null)
{ {
prefabObject = Instantiate(ir.asset as GameObject); prefabObject = Instantiate(assetBundle.LoadAsset<GameObject>(prefab));
Prefabs.Add(prefabObject); LoadedGameObjects.Add(prefabObject);
prefabObject.transform.SetParent(transform); prefabObject.transform.SetParent(transform);
AssetBundles.Add(ab); if (LoadedGameObjectNames.ContainsKey(ab) == false)
LoadedGameObjectNames.Add(ab, new());
LoadedGameObjectNames[ab].Add(prefab);
} }
else else
{ {
Debug.LogError($"Load Prefab failed", this); Debug.LogError($"Load AssetBundle failed", this);
} }
} LoadingCounter--;
else }));
{
Debug.LogError($"Load AssetBundle failed", this);
}
} }
} }
} }