Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://forum.unity3d.com/threads/155242-What-you%92re-objectively-missing-when-using-UnityScript-instead-than-C/page3
- //2012 (C) NPSF3000
- public class GenericPool<T>
- {
- Stack<T> store = new Stack<T>();
- public int MaxStored = 100;
- public Func<T> CreateLogic;
- public Func<T, T> SaveLogic;
- public Action<T> DestroyLogic;
- public T Get()
- {
- if (store.Count > 0) return store.Pop();
- else if (CreateLogic != null) return CreateLogic();
- return default(T);
- }
- public void Save(T item)
- {
- if (store.Count < MaxStored)
- {
- if (SaveLogic != null) item = SaveLogic(item);
- store.Push(item);
- }
- else if (DestroyLogic != null) DestroyLogic(item);
- }
- }
- ...
- var goPool = new GenericPool<GameObject>()
- {
- MaxStored = 10,
- CreateLogic = () => new GameObject(),
- SaveLogic = x => x,
- DestroyLogic = x => Destroy(x)
- };
- var intPool = new GenericPool<int>()
- {
- MaxStored = 10,
- CreateLogic = () => 0,
- SaveLogic = x => --x,
- };
- /*etc*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement