Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public static class ObjectPool
- {
- const int DEFAULT_POOL_SIZE = 10;
- private static Dictionary<GameObject, Pool> pools; // = new Dictionary<GameObject, Pool>();
- static void Init(GameObject prefab = null, int qty = DEFAULT_POOL_SIZE)
- {
- if (pools == null)
- pools = new Dictionary<GameObject, Pool>();
- else if (pools.ContainsKey(prefab) == false)
- pools[prefab] = new Pool(prefab, qty);
- }
- static public void Preload(GameObject prefab, int qty = 1)
- {
- Init(prefab, qty);
- GameObject[] objs = new GameObject[qty];
- for (int i = 0; i < qty; i++)
- {
- objs[i] = Spawn(prefab, Vector3.zero, Quaternion.identity);
- Despawn(objs[i]);
- }
- }
- /*public static void MakePool(GameObject prefab, int startAmount)
- {
- pools.Add(prefab, new Pool(prefab, startAmount));
- }*/
- public static GameObject Spawn(GameObject prefab)
- {
- return Spawn(prefab, Vector3.zero, Quaternion.identity);
- }
- public static GameObject Spawn(GameObject prefab, Vector3 pos)
- {
- return Spawn(prefab, pos, Quaternion.identity);
- }
- public static GameObject Spawn(GameObject prefab, Quaternion rot)
- {
- return Spawn(prefab, Vector3.zero, rot);
- }
- public static GameObject Spawn(GameObject prefab, Transform parent)
- {
- return Spawn(prefab, Vector3.zero, Quaternion.identity, parent);
- }
- public static GameObject Spawn(GameObject prefab, Vector3 pos, Transform parent)
- {
- return Spawn(prefab, pos, Quaternion.identity, parent);
- }
- public static GameObject Spawn(GameObject prefab, Quaternion rot, Transform parent)
- {
- return Spawn(prefab, Vector3.zero, rot, parent);
- }
- public static GameObject Spawn(GameObject prefab, Vector3 pos, Quaternion rot, Transform parent = null)
- {
- Init(prefab);
- // if(avail)
- return pools[prefab].Spawn(pos, rot, parent);
- }
- public static void Despawn(GameObject obj)
- {
- PoolMember pm = obj.GetComponent<PoolMember>();
- if (pm == null)
- Object.Destroy(obj);
- else
- pm.myPool.Despawn(obj);
- }
- public static void Despawn(GameObject obj, float delay)
- {
- Wait.instance.Call(obj, delay);
- // Wait wait = new Wait();
- // wait.Amount(obj, delay);
- }
- private class Pool // : MonoBehaviour
- {
- Stack<GameObject> available;
- GameObject prefab;
- // int startAmount;
- public Pool(GameObject prefab, int startAmount)
- {
- this.prefab = prefab;
- // this.startAmount = startAmount;
- available = new Stack<GameObject>(startAmount);
- }
- public GameObject Spawn(Vector3 pos, Quaternion rot, Transform parent)
- {
- GameObject obj;
- if (available.Count == 0)
- obj = Object.Instantiate(prefab, pos, rot);
- else
- {
- obj = available.Pop();
- if (obj == null)
- return Spawn(pos, rot, parent);
- }
- obj.transform.parent = parent;
- obj.transform.position = pos;
- obj.transform.rotation = rot;
- obj.SetActive(true);
- return obj;
- }
- public void Despawn(GameObject obj)
- {
- obj.SetActive(false);
- available.Push(obj);
- }
- }
- private class PoolMember // : MonoBehaviour
- {
- public Pool myPool;
- }
- private class Wait : MonoBehaviour
- {
- public static Wait instance;
- private void Awake()
- {
- instance = this;
- }
- public void Call(GameObject obj, float delay)
- {
- StartCoroutine(DelayDespawn(obj, delay));
- }
- /*public void Amount(GameObject obj, float delay)
- {
- StartCoroutine(DelayDespawn(obj, delay));
- }
- /*public Wait(GameObject obj, float delay)
- {
- StartCoroutine(DelayDespawn(obj, delay));
- }*/
- }
- private static IEnumerator DelayDespawn(GameObject obj, float delay)
- {
- yield return new WaitForSeconds(delay);
- Despawn(obj);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement