Advertisement
NPSF3000

Simple Pool Manager

Dec 2nd, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. //http://forum.unity3d.com/threads/155242-What-you%92re-objectively-missing-when-using-UnityScript-instead-than-C/page3
  2. //2012 (C) NPSF3000
  3.  
  4. public class GenericPool<T>
  5. {
  6.     Stack<T> store = new Stack<T>();
  7.  
  8.     public int MaxStored = 100;
  9.     public Func<T> CreateLogic;
  10.     public Func<T, T> SaveLogic;
  11.     public Action<T> DestroyLogic;
  12.  
  13.     public T Get()
  14.     {
  15.         if (store.Count > 0) return store.Pop();
  16.         else if (CreateLogic != null) return CreateLogic();
  17.         return default(T);
  18.     }
  19.  
  20.     public void Save(T item)
  21.     {
  22.         if (store.Count < MaxStored)
  23.         {
  24.             if (SaveLogic != null) item = SaveLogic(item);
  25.             store.Push(item);
  26.         }
  27.         else if (DestroyLogic != null) DestroyLogic(item);
  28.     }
  29. }
  30.  
  31. ...
  32.  
  33. var goPool = new GenericPool<GameObject>()
  34. {
  35.     MaxStored = 10,
  36.     CreateLogic = () => new GameObject(),
  37.     SaveLogic = x => x,
  38.     DestroyLogic = x => Destroy(x)
  39. };
  40.  
  41. var intPool = new GenericPool<int>()
  42. {
  43.     MaxStored = 10,
  44.     CreateLogic = () => 0,
  45.     SaveLogic = x => --x,
  46. };
  47.  
  48. /*etc*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement