新增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 System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
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]
public class PrefabRootObject : ScriptableObject, IAssetBundleLoader
{
@@ -13,15 +33,26 @@ namespace Demo.Game
return new GameObject().AddComponent<PrefabRootObject>();
}
private readonly List<string> AssetBundles = new();
private readonly List<GameObject> Prefabs = new();
private int LoadingCounter = 0;
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()
{
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="prefab"></param>
[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<GameObject>(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<GameObject>(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--;
}));
}
}
}