Skip to content

Instantly share code, notes, and snippets.

@daemon3000
Last active August 29, 2015 14:24
Show Gist options
  • Save daemon3000/02d193f5e2009c095598 to your computer and use it in GitHub Desktop.
Save daemon3000/02d193f5e2009c095598 to your computer and use it in GitHub Desktop.
GameObjectPool
#region [Copyright (c) 2015 Cristian Alexandru Geambasu]
// Distributed under the terms of an MIT-style license:
//
// The MIT License
//
// Copyright (c) 2015 Cristian Alexandru Geambasu
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#endregion
using UnityEngine;
using System.Collections.Generic;
namespace TeamUtility
{
public class GameObjectPool : System.IDisposable
{
private const string ON_POOLED_INSTANCE_RESET = "OnPooledInstanceReset";
private const string ON_POOLED_INSTANCE_INITIALIZE = "OnPooledInstanceInitialize";
private Queue<GameObject> m_freeInstances;
private HashSet<GameObject> m_activeInstances;
private GameObject m_prefab;
private int m_maxCapacity;
private int m_currentCapacity;
private bool m_isDisposed;
public int Alive { get { return m_activeInstances.Count; } }
public GameObjectPool(GameObject prefab, int maxCapacity = 100)
{
m_prefab = prefab;
m_freeInstances = new Queue<GameObject>();
m_activeInstances = new HashSet<GameObject>();
m_maxCapacity = maxCapacity;
m_currentCapacity = 0;
m_isDisposed = false;
}
public GameObject Create()
{
if(m_isDisposed)
return null;
GameObject instance = null;
if(m_freeInstances.Count > 0)
{
instance = m_freeInstances.Dequeue();
instance.SetActive(true);
instance.SendMessage(ON_POOLED_INSTANCE_INITIALIZE, SendMessageOptions.RequireReceiver);
}
else
{
if(m_currentCapacity < m_maxCapacity)
{
instance = GameObject.Instantiate<GameObject>(m_prefab);
if(instance != null)
instance.SendMessage(ON_POOLED_INSTANCE_INITIALIZE, SendMessageOptions.RequireReceiver);
m_currentCapacity++;
}
}
if(instance != null)
m_activeInstances.Add(instance);
return instance;
}
public void Free(GameObject gameObject)
{
if(m_isDisposed)
return;
if(m_activeInstances.Contains(gameObject))
{
gameObject.SendMessage(ON_POOLED_INSTANCE_RESET, SendMessageOptions.RequireReceiver);
gameObject.SetActive(false);
m_freeInstances.Enqueue(gameObject);
m_activeInstances.Remove(gameObject);
}
}
public void FreeAll()
{
if(m_isDisposed)
return;
foreach(var gameObject in m_activeInstances)
{
if(gameObject != null)
{
gameObject.SendMessage(ON_POOLED_INSTANCE_RESET, SendMessageOptions.RequireReceiver);
gameObject.SetActive(false);
m_freeInstances.Enqueue(gameObject);
}
}
m_activeInstances.Clear();
}
public void Dispose()
{
if(!m_isDisposed)
{
FreeAll();
while(m_freeInstances.Count > 0)
{
GameObject gameObject = m_freeInstances.Dequeue();
GameObject.Destroy(gameObject);
}
m_isDisposed = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment