Skip to content

Instantly share code, notes, and snippets.

@RevenantX
Created May 14, 2016 13:53
Show Gist options
  • Save RevenantX/e036fe2838326c00d10194e689a757d2 to your computer and use it in GitHub Desktop.
Save RevenantX/e036fe2838326c00d10194e689a757d2 to your computer and use it in GitHub Desktop.
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