81 lines
2.4 KiB
C#
81 lines
2.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Demo.Game
|
|
{
|
|
public class PrefabRootObject : ScriptableObject, IAssetBundleLoader
|
|
{
|
|
public static PrefabRootObject Make()
|
|
{
|
|
return new GameObject().AddComponent<PrefabRootObject>();
|
|
}
|
|
|
|
private List<IEnumerator> AssetBundleLoadings = new();
|
|
private List<string> AssetBundles = new();
|
|
private List<GameObject> Prefabs = new();
|
|
|
|
public override IEnumerator LoadScript(string script)
|
|
{
|
|
yield return base.LoadScript(script);
|
|
foreach (var loading in AssetBundleLoadings)
|
|
{
|
|
yield return loading;
|
|
}
|
|
}
|
|
|
|
public override IEnumerator UnloadScript()
|
|
{
|
|
yield return base.UnloadScript();
|
|
foreach (var ab in AssetBundles)
|
|
{
|
|
yield return this.UnloadAssetBundle(ab);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载预制体作为子物体
|
|
/// </summary>
|
|
/// <param name="ab"></param>
|
|
/// <param name="prefab"></param>
|
|
[ScriptableCall(@"
|
|
<summary>
|
|
加载预制体作为子物体
|
|
</summary>
|
|
<param name=""ab""></param>
|
|
<param name=""prefab""></param>
|
|
")]
|
|
public void Load(string ab, string prefab)
|
|
{
|
|
IEnumerator Foo()
|
|
{
|
|
AssetBundle assetBundle = null;
|
|
yield return this.LoadAssetBundle(ab, x => assetBundle = x);
|
|
GameObject prefabObject = null;
|
|
if (assetBundle != null)
|
|
{
|
|
var ir = assetBundle.LoadAssetAsync<GameObject>(prefab);
|
|
yield return ir;
|
|
if (ir.asset != null)
|
|
{
|
|
prefabObject = Instantiate(ir.asset as GameObject);
|
|
Prefabs.Add(prefabObject);
|
|
prefabObject.transform.SetParent(transform);
|
|
AssetBundles.Add(ab);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"Load Prefab failed", this);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"Load AssetBundle failed", this);
|
|
}
|
|
}
|
|
|
|
AssetBundleLoadings.Add(Foo());
|
|
}
|
|
}
|
|
}
|