Advertisement
Guest User

brakeys_object_pooling

a guest
Dec 12th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. public class CustomObjectPooling : MonoBehaviour {
  5.  
  6.     [System.Serializable]
  7.     public class ObjPool
  8.     {
  9.         public string objKey;
  10.         public GameObject objPrefab;
  11.         public int size;
  12.     }
  13.     GameManager gameInstance;
  14.     public static CustomObjectPooling instance;
  15.  
  16.     public List<ObjPool> objPools;
  17.     public Dictionary<string, Queue<GameObject>> objectPoolDictionary;
  18.  
  19.     private void Awake()
  20.     {
  21.         if(instance != null)
  22.         {
  23.             Debug.Log("already have objectpool");
  24.             return;
  25.         }
  26.         instance = this;
  27.     }
  28.  
  29.     // Use this for initialization
  30.     void Start () {
  31.         gameInstance = GameManager.gameInstance;
  32.         objectPoolDictionary = new Dictionary<string, Queue<GameObject>>();
  33.  
  34.         foreach(ObjPool objPool in objPools)
  35.         {
  36.             Queue<GameObject> objectPool = new Queue<GameObject>();
  37.  
  38.             for(int i = 0; i < objPool.size; i++)
  39.             {
  40.                 GameObject obj = Instantiate(objPool.objPrefab);
  41.                 obj.SetActive(false);
  42.                 objectPool.Enqueue(obj);
  43.             }
  44.             objectPoolDictionary.Add(objPool.objKey, objectPool);
  45.         }
  46.     }
  47.  
  48.     public GameObject SpawnObjFromPool(string _objKey, Vector3 position, Quaternion rotation)
  49.     {
  50.         if (!objectPoolDictionary.ContainsKey(_objKey))
  51.         {
  52.             Debug.LogWarning("Pool with key" + _objKey + "doesn't exist");
  53.             return null;
  54.         }
  55.  
  56.         GameObject objectSpawned = objectPoolDictionary[_objKey].Dequeue();
  57.  
  58.         objectSpawned.SetActive(true);
  59.         objectSpawned.transform.position = position;
  60.         objectSpawned.transform.rotation = rotation;
  61.  
  62.         IPooledObject pooledObject = objectSpawned.GetComponent<IPooledObject>();
  63.  
  64.         if(pooledObject != null)
  65.         {
  66.             pooledObject.SpawnEnemy();
  67.             gameInstance.enemiesAlive++;
  68.         }
  69.  
  70.         objectPoolDictionary[_objKey].Enqueue(objectSpawned);
  71.  
  72.         return objectSpawned;
  73.     }
  74.    
  75. }
  76.  
  77. WaveSpawner.cs
  78.  
  79. ......
  80.     IEnumerator SpawnWaveFirstLocation()
  81.     {
  82.         GameObject objSpawned;
  83.         Wave wave = waves[waveNumber];
  84.         for(int i = 0; i < wave.count; i++)
  85.         {
  86.             enemiesAlive++;
  87.             objSpawned = objectPooler.SpawnObjFromPool(wave.enemyNameKey, transform.position, Quaternion.identity);
  88.             yield return new WaitForSeconds(1f / wave.rate);
  89.         }
  90.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement