public PoolableGameObject Get()
{
Initialize();
if (_pooledObjs.Count == 0)
{
if (useCap && _numOfActiveObjs >= capAmount)
{
// We're at cap
return ReuseObject();
}
IncrementPool();
}
PoolableGameObject obj;
do
{
obj = _pooledObjs.Pop();
}
while (obj == null || obj.gameObject == null);
obj._isActive = true;
obj.gameObject.SetActive(true);
_numOfActiveObjs++;
if (useCap || persistAcrossScenes)
{
// Should always have a node available
LinkedListNode<PoolableGameObject> liveNode = _pooledNodes.Pop();
liveNode.Value = obj;
obj._liveNode = liveNode;
_liveObjs.AddLast(liveNode);
}
return obj;
}
/// <summary> /// Receives a GameObject from objToCreateFrom's object /// pool. /// </summary> /// <param name="objToCreateFrom">The prefab or GameObject that we /// want a duplicated object of.</param> /// <returns>A GameObject from the object pool.</returns> public static GameObject GetObj(GameObject objToCreateFrom) { PoolableGameObject poolable = objToCreateFrom.GetComponent <PoolableGameObject>(); if (poolable == null) { Debug.LogErrorFormat(REQUIRES_COMP, objToCreateFrom.name, "Get"); return(null); } return(poolable.Get().gameObject); }