Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using UnityEngine;
  5. using Object = UnityEngine.Object;
  6.  
  7. namespace Common.Scripts.Helpers.PoolManager
  8. {
  9.     public static class SimplePool
  10.     {
  11.         /// <summary>
  12.         /// Dictionary with pools.
  13.         /// </summary>
  14.         ///
  15.         /// <remarks> key - game object type </remarks>
  16.         /// <remarks> value - queue that represents pool with objects </remarks>
  17.         private static readonly Dictionary<Type, Queue<MonoBehaviour>> PoolDictionary = new Dictionary<Type, Queue<MonoBehaviour>>();
  18.        
  19.         private const string PoolsContainerName = "NotDestroyablePools"; // Name of the game object that will be created and contain pools
  20.         private static GameObject _globalPoolContainer;
  21.  
  22.         /// <summary>
  23.         /// Gets object of specified type from pool. Instantiates it if necessarily.
  24.         /// </summary>
  25.         /// <param name="original">Original object that will be used to instantiate new object in case there is no objects in pool</param>
  26.         /// <param name="parent">Transform where object needs to be placed after getting from pool.</param>
  27.         /// <typeparam name="T">Type of the object that you need.</typeparam>
  28.         ///
  29.         /// <returns>
  30.         /// Object from pool.
  31.         /// </returns>
  32.         ///
  33.         /// <exception cref="KeyNotFoundException">
  34.         /// If pool was not created yet.
  35.         /// </exception>
  36.         public static T Get<T>(T original, Transform parent) where T : Object
  37.         {
  38.             if (PoolDictionary.TryGetValue(typeof(T), out var monoBehaviours))
  39.             {
  40.                 if (monoBehaviours.Count == 0)
  41.                 {
  42.                     T toReturn = Object.Instantiate(original, parent);
  43.                     return toReturn;
  44.                 }
  45.                
  46.                 return monoBehaviours.Dequeue() as T;
  47.             }
  48.  
  49.             throw new KeyNotFoundException("There is no such pool exist.");
  50.         }
  51.  
  52.         /// <summary>
  53.         /// Returns object in pool
  54.         /// </summary>
  55.         /// <param name="obj">Object to return</param>
  56.         /// <exception cref="KeyNotFoundException">
  57.         /// If there is no pool with such objects exist.
  58.         /// </exception>
  59.         public static void Return(MonoBehaviour obj)
  60.         {
  61.             if (PoolDictionary.ContainsKey(obj.gameObject.GetType()))
  62.             {
  63.                 PoolDictionary[obj.gameObject.GetType()].Enqueue(obj);
  64.             }
  65.            
  66.             throw new KeyNotFoundException("There is no pool with such objects exist.");
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Creates new pool
  71.         /// </summary>
  72.         ///
  73.         /// <param name="poolableObject">
  74.         /// Objects type that needs to be in this pool.
  75.         /// You will be able to get them by <see cref="Get{T}"/> method.
  76.         /// </param>
  77.         ///
  78.         /// <exception cref="DuplicateNameException">
  79.         /// If there is already exist pool with objects of this type.
  80.         /// </exception>
  81.         public static void CreatePool<T>() where T: Object
  82.         {
  83.             if (!PoolDictionary.ContainsKey(typeof(T)))
  84.             {
  85.                 PoolDictionary.Add(typeof(T), new Queue<MonoBehaviour>());
  86.                 CreatePoolContainer("Pool of " + typeof(T).Name + " ");
  87.             }
  88.             else
  89.             {
  90.                 throw new DuplicateNameException("There is already such pool exist. No swimming today :C");
  91.             }
  92.         }
  93.  
  94.         /// <summary>
  95.         /// Removes existing pool, destroying all objects in it.
  96.         /// </summary>
  97.         ///
  98.         /// <param name="poolObjects">
  99.         /// Objects that needs to be in this pool.
  100.         /// </param>
  101.         public static void RemovePool<T>() where T: Object
  102.         {
  103.             if (PoolDictionary.ContainsKey(typeof(T)))
  104.             {
  105.                 foreach (var objectInPool in PoolDictionary[typeof(T)])
  106.                 {
  107.                     Object.Destroy(objectInPool);
  108.                 }
  109.                 PoolDictionary.Remove(typeof(T));
  110.             }
  111.         }
  112.  
  113.         /// <summary>
  114.         /// Checks if pool with specified objects exist
  115.         /// </summary>
  116.         ///
  117.         /// <param name="poolObjects">
  118.         /// Objects that needs to be in this pool.
  119.         /// </param>
  120.         public static bool IsPoolForObjectsExist<T>() where T : Object
  121.         {
  122.             return PoolDictionary.ContainsKey(typeof(T));
  123.         }
  124.        
  125.         private static void CreatePoolContainer(string poolName)
  126.         {
  127.             if (_globalPoolContainer == null) {
  128.                 _globalPoolContainer = new GameObject(PoolsContainerName);
  129.                 Object.DontDestroyOnLoad(_globalPoolContainer);
  130.             }
  131.            
  132.             Object.Instantiate(new GameObject(poolName), _globalPoolContainer.transform);
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement