zrrz111

Untitled

Mar 14th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. //Usage:
  6. //void Awake() {
  7. //  StaticPool staticPool = new StaticPool(); //This could be cleaned up for better singleton creation
  8. //  ...
  9. //}
  10. //
  11. //...
  12. //
  13. //void Shoot() {
  14. //  GameObject bullet = StaticPool.GetObj(bulletPrefab); //Will grab or create a new bullet and return it
  15. //  ...
  16. //}
  17.  
  18. public class StaticPool {
  19.  
  20.     static StaticPool s_instance;
  21.    
  22.     public Dictionary<GameObject, List<GameObject>> objLists;
  23.  
  24.     const int DEFAULT_SIZE = 10;
  25.     const int SIZE_INCREMENT = 5;
  26.  
  27.     [System.NonSerialized]
  28.     public GameObject parent;
  29.  
  30.     public StaticPool () {
  31.         s_instance = this;
  32.         objLists = new Dictionary<GameObject, List<GameObject>>();
  33.         parent = new GameObject("StaticPool");
  34.         GameObject.DontDestroyOnLoad(parent);
  35.     }
  36.  
  37.     /// <summary>
  38.     /// Not necessary. Can be used to preload prefab for the pool for loading screens.
  39.     /// </summary>
  40.     /// <param name="prefab">Prefab.</param>
  41.     public static void InitObj(GameObject prefab) {
  42.         if(s_instance.objLists.ContainsKey(prefab) == false) {
  43.             GameObject holder = new GameObject(prefab.name);
  44.             holder.transform.parent = s_instance.parent.transform;
  45.  
  46.             s_instance.objLists.Add (prefab, new List<GameObject>());
  47.             AddToList(prefab, DEFAULT_SIZE, holder.transform);
  48.         }
  49.     }
  50.  
  51.     /// <summary>
  52.     /// Gets the object of type prefab.
  53.     /// </summary>
  54.     /// <returns>The object.</returns>
  55.     /// <param name="prefab">Prefab.</param>
  56.     public static GameObject GetObj(GameObject prefab) {
  57.         InitObj(prefab);
  58.  
  59.         // Find inactive
  60.         for(int i = 0, n = s_instance.objLists[prefab].Count; i < n; i++) {
  61.             if(s_instance.objLists[prefab][i].activeSelf == false) {
  62.                 s_instance.objLists[prefab][i].SetActive(true);
  63.                 return s_instance.objLists[prefab][i];
  64.             }
  65.         }
  66.  
  67.         // None found
  68.         AddToList(prefab, SIZE_INCREMENT, s_instance.parent.transform.FindChild(prefab.name));
  69.  
  70. //      Debug.Log(s_instance.objLists[prefab].Count - SIZE_INCREMENT);
  71.  
  72.         // Zero indexed and minus what we just added
  73.         s_instance.objLists[prefab][s_instance.objLists[prefab].Count - SIZE_INCREMENT].SetActive(true);
  74.         return s_instance.objLists[prefab][s_instance.objLists[prefab].Count - SIZE_INCREMENT];
  75.  
  76.     }
  77.  
  78.     static void AddToList(GameObject prefab, int count, Transform holder) {
  79.         for(int i = 0; i < count; i++) {
  80.             GameObject obj = (GameObject)GameObject.Instantiate(prefab, Vector3.one * 1000f, Quaternion.identity);
  81.             s_instance.objLists[prefab].Add(obj);
  82.             obj.transform.parent = holder;
  83.             obj.SetActive(false);
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment