新增PrefabRootObjectConfig
This commit is contained in:
@@ -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)
|
||||||
|
{
|
||||||
|
LoadingCounter++;
|
||||||
|
ConventionUtility.StartCoroutine(this.LoadAssetBundle(ab, assetBundle =>
|
||||||
{
|
{
|
||||||
AssetBundle assetBundle = null;
|
|
||||||
yield return this.LoadAssetBundle(ab, x => assetBundle = x);
|
|
||||||
GameObject prefabObject = null;
|
GameObject prefabObject = null;
|
||||||
if (assetBundle != null)
|
if (assetBundle != null)
|
||||||
{
|
{
|
||||||
var ir = assetBundle.LoadAssetAsync<GameObject>(prefab);
|
prefabObject = Instantiate(assetBundle.LoadAsset<GameObject>(prefab));
|
||||||
yield return ir;
|
LoadedGameObjects.Add(prefabObject);
|
||||||
if (ir.asset != null)
|
|
||||||
{
|
|
||||||
prefabObject = Instantiate(ir.asset as GameObject);
|
|
||||||
Prefabs.Add(prefabObject);
|
|
||||||
prefabObject.transform.SetParent(transform);
|
prefabObject.transform.SetParent(transform);
|
||||||
AssetBundles.Add(ab);
|
if (LoadedGameObjectNames.ContainsKey(ab) == false)
|
||||||
}
|
LoadedGameObjectNames.Add(ab, new());
|
||||||
else
|
LoadedGameObjectNames[ab].Add(prefab);
|
||||||
{
|
|
||||||
Debug.LogError($"Load Prefab failed", this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.LogError($"Load AssetBundle failed", this);
|
Debug.LogError($"Load AssetBundle failed", this);
|
||||||
}
|
}
|
||||||
|
LoadingCounter--;
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user