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(); } private List AssetBundleLoadings = new(); private List AssetBundles = new(); private List 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); } } /// /// 加载预制体作为子物体 /// /// /// [ScriptableCall(@" 加载预制体作为子物体 ")] 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(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()); } } }