Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ==========================
- Copyright (c) Diliupg 2020
- www.soft.diliupg.com
- ==========================
- */
- using UnityEngine;
- using System.Collections.Generic;
- public class PoolManager : MonoBehaviour
- {
- //float scaleAmount = 1f;
- [System.Serializable]
- public class Pool
- {
- public int tag;
- public GameObject prefab;
- public int size;
- }
- public List<Pool> Pools;
- private Pool pool;
- public Dictionary<int, Queue<GameObject>> PoolDictionary;
- void Start ( )
- {
- Init ( );
- }
- void Init ( )
- {
- PoolDictionary = new Dictionary<int, Queue<GameObject>> ( );
- foreach ( Pool pool in Pools )
- {
- Queue<GameObject> objectPool = new Queue<GameObject> ( );
- for ( int i = 0 ; i < pool.size ; i++ )
- {
- GameObject obj = Instantiate ( pool.prefab );
- obj.SetActive ( false );
- objectPool.Enqueue ( obj ); // add to the queue
- }
- PoolDictionary.Add ( pool.tag, objectPool );
- }
- }
- public GameObject SpawnFromPool ( int tag, Vector2 position )
- {
- if ( !PoolDictionary.ContainsKey ( tag ) )
- {
- //Debug.LogWarning ( "Pool with tag " + tag + "doesn't exist" );
- return null;
- }
- GameObject objectToSpawn = PoolDictionary [ tag ].Dequeue ( ); // remove from the queue
- objectToSpawn.SetActive ( true );
- objectToSpawn.transform.position = position;
- PoolDictionary [ tag ].Enqueue ( objectToSpawn ); // put back in the queue
- return objectToSpawn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement