Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Spawn now creates a pool if one is not found. As a result, CreatePool…
… isn't required for initial setup. And the fallback to normal instantiation is no longer needed and has been removed.

Added a warning for when Recycle gets called but no pool is found.
  • Loading branch information
Brett Johnson authored and Brett Johnson committed Jun 18, 2016
commit cbcb74a45a59e640a9bcdcae0e660f67a8e159e5
23 changes: 8 additions & 15 deletions Assets/ObjectPool/Scripts/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 posi
{
List<GameObject> list;
Transform trans;
GameObject obj;
GameObject obj = null;
CreatePool(prefab.gameObject, 0); // will create pool if none exists
if (instance.pooledObjects.TryGetValue(prefab, out list))
{
obj = null;
Expand Down Expand Up @@ -140,21 +141,8 @@ public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 posi
trans.localPosition = position;
trans.localRotation = rotation;
instance.spawnedObjects.Add(obj, prefab);
return obj;
}
else
{
obj = (GameObject)Object.Instantiate(prefab);
trans = obj.GetComponent<Transform>();
if (parent != null)
{
bool worldPositionStays = (parent.GetComponent<RectTransform>() == null);
trans.SetParent(parent, worldPositionStays);
}
trans.localPosition = position;
trans.localRotation = rotation;
return obj;
}
return obj;
}
public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 position)
{
Expand Down Expand Up @@ -185,9 +173,14 @@ public static void Recycle(GameObject obj)
{
GameObject prefab;
if (instance.spawnedObjects.TryGetValue(obj, out prefab))
{
Recycle(obj, prefab);
}
else
{
Debug.LogWarning(obj.name + "can not be recycled because it was never pooled. It will be destroyed instead.");
Object.Destroy(obj);
}
}
static void Recycle(GameObject obj, GameObject prefab)
{
Expand Down