using System; using UnityEngine; using UnityEngine.SceneManagement; namespace asap.core.common { public class GameObjectPool : AObjectPool where T : Component { private T prefab; public GameObjectPool(T prefab, int preloadNum, int maxSize) : base(typeof(T), preloadNum, maxSize) { this.prefab = prefab; } protected override object newObject() { var obj = GameObject.Instantiate(prefab); GameObject.DontDestroyOnLoad(obj); onCreateObject(obj); return obj; } protected override void destroyObject(object obj) { if (obj.Equals(null)) return; onDestroyObject(obj); GameObject gameObject = (obj as T).gameObject; GameObject.Destroy(gameObject); } protected override void onCreateObject(object obj) { GameObject gameObject = (obj as T).gameObject; gameObject.SetActive(false); var comps = gameObject.GetComponents(); for (int i = 0; i < comps.Length; i++) { comps[i].OnPoolCreate(this); } } protected override void onDestroyObject(object obj) { GameObject gameObject = (obj as T).gameObject; if (gameObject.Equals(null)) return; var comps = gameObject.GetComponents(); for (int i = 0; i < comps.Length; i++) { comps[i].OnPoolDestroy(); } } protected override void onSpawnObject(object obj) { GameObject gameObject = (obj as T).gameObject; if (!gameObject.activeSelf) gameObject.SetActive(true); var comps = gameObject.GetComponents(); for(int i =0;i< comps.Length; i++) { comps[i].OnSpawn(); } } protected override void onDespawnObject(object obj) { GameObject gameObject = (obj as T).gameObject; //var ps = gameObject.GetComponent(); //if (ps != null) //{ // ps.Stop(true); //} //var audioSource = gameObject.GetComponent(); //if (audioSource != null) //{ // audioSource.Stop(); // audioSource.volume = 1f; // audioSource.priority = 128; // audioSource.loop = false; // audioSource.playOnAwake = false; // audioSource.clip = null; // audioSource.time = 0f; // audioSource.outputAudioMixerGroup = null; //} var comps = gameObject.GetComponents(); for (int i = 0; i < comps.Length; i++) { comps[i].OnDespawn(); } gameObject.transform.SetParent(null, false); if (gameObject.activeSelf) gameObject.SetActive(false); } public override void Release() { base.Release(); prefab = null; } } }