Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- using UnityEngine;
- using Object = UnityEngine.Object;
- namespace Common.Scripts.Helpers.PoolManager
- {
- public static class SimplePool
- {
- /// <summary>
- /// Dictionary with pools.
- /// </summary>
- ///
- /// <remarks> key - game object type </remarks>
- /// <remarks> value - queue that represents pool with objects </remarks>
- private static readonly Dictionary<Type, Queue<MonoBehaviour>> PoolDictionary = new Dictionary<Type, Queue<MonoBehaviour>>();
- private const string PoolsContainerName = "NotDestroyablePools"; // Name of the game object that will be created and contain pools
- private static GameObject _globalPoolContainer;
- /// <summary>
- /// Gets object of specified type from pool. Instantiates it if necessarily.
- /// </summary>
- /// <param name="original">Original object that will be used to instantiate new object in case there is no objects in pool</param>
- /// <param name="parent">Transform where object needs to be placed after getting from pool.</param>
- /// <typeparam name="T">Type of the object that you need.</typeparam>
- ///
- /// <returns>
- /// Object from pool.
- /// </returns>
- ///
- /// <exception cref="KeyNotFoundException">
- /// If pool was not created yet.
- /// </exception>
- public static T Get<T>(T original, Transform parent) where T : Object
- {
- if (PoolDictionary.TryGetValue(typeof(T), out var monoBehaviours))
- {
- if (monoBehaviours.Count == 0)
- {
- T toReturn = Object.Instantiate(original, parent);
- return toReturn;
- }
- return monoBehaviours.Dequeue() as T;
- }
- throw new KeyNotFoundException("There is no such pool exist.");
- }
- /// <summary>
- /// Returns object in pool
- /// </summary>
- /// <param name="obj">Object to return</param>
- /// <exception cref="KeyNotFoundException">
- /// If there is no pool with such objects exist.
- /// </exception>
- public static void Return(MonoBehaviour obj)
- {
- if (PoolDictionary.ContainsKey(obj.gameObject.GetType()))
- {
- PoolDictionary[obj.gameObject.GetType()].Enqueue(obj);
- }
- throw new KeyNotFoundException("There is no pool with such objects exist.");
- }
- /// <summary>
- /// Creates new pool
- /// </summary>
- ///
- /// <param name="poolableObject">
- /// Objects type that needs to be in this pool.
- /// You will be able to get them by <see cref="Get{T}"/> method.
- /// </param>
- ///
- /// <exception cref="DuplicateNameException">
- /// If there is already exist pool with objects of this type.
- /// </exception>
- public static void CreatePool<T>() where T: Object
- {
- if (!PoolDictionary.ContainsKey(typeof(T)))
- {
- PoolDictionary.Add(typeof(T), new Queue<MonoBehaviour>());
- CreatePoolContainer("Pool of " + typeof(T).Name + " ");
- }
- else
- {
- throw new DuplicateNameException("There is already such pool exist. No swimming today :C");
- }
- }
- /// <summary>
- /// Removes existing pool, destroying all objects in it.
- /// </summary>
- ///
- /// <param name="poolObjects">
- /// Objects that needs to be in this pool.
- /// </param>
- public static void RemovePool<T>() where T: Object
- {
- if (PoolDictionary.ContainsKey(typeof(T)))
- {
- foreach (var objectInPool in PoolDictionary[typeof(T)])
- {
- Object.Destroy(objectInPool);
- }
- PoolDictionary.Remove(typeof(T));
- }
- }
- /// <summary>
- /// Checks if pool with specified objects exist
- /// </summary>
- ///
- /// <param name="poolObjects">
- /// Objects that needs to be in this pool.
- /// </param>
- public static bool IsPoolForObjectsExist<T>() where T : Object
- {
- return PoolDictionary.ContainsKey(typeof(T));
- }
- private static void CreatePoolContainer(string poolName)
- {
- if (_globalPoolContainer == null) {
- _globalPoolContainer = new GameObject(PoolsContainerName);
- Object.DontDestroyOnLoad(_globalPoolContainer);
- }
- Object.Instantiate(new GameObject(poolName), _globalPoolContainer.transform);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement