using Demo.Attr; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace Demo.Game { [Scriptable] public class PrefabRootObject : ScriptableObject, IAssetBundleLoader { public static PrefabRootObject Make() { return new GameObject().AddComponent(); } private readonly List AssetBundles = new(); private readonly List Prefabs = new(); public override IEnumerator UnloadScript() { yield return base.UnloadScript(); foreach (var ab in AssetBundles) { yield return this.UnloadAssetBundle(ab); } } /// /// 加载预制体作为子物体 /// /// /// [Convention.RScript.Variable.Attr.Method] public IEnumerator Load(string ab, string prefab) { 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); } } } }