Created
May 14, 2016 13:53
-
-
Save RevenantX/e036fe2838326c00d10194e689a757d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Game.Shared.Helpers | |
{ | |
public class GamePool<T> where T : class | |
{ | |
private readonly Stack<T> _pool; | |
private readonly Func<T> _creator; | |
public GamePool(Func<T> creator) | |
{ | |
_pool = new Stack<T>(); | |
_creator = creator; | |
} | |
public GamePool(Func<T> creator, int capacity) | |
{ | |
_pool = new Stack<T>(capacity); | |
_creator = creator; | |
} | |
public T Get() | |
{ | |
if (_pool.Count > 0) | |
{ | |
return _pool.Pop(); | |
} | |
return _creator(); | |
} | |
public void Put(T gameObject) | |
{ | |
_pool.Push(gameObject); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment