Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for pooling Unity UI, and automatic pool creation. #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
48 changes: 25 additions & 23 deletions Assets/ObjectPool/Scripts/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public class StartupPool

static ObjectPool _instance;
static List<GameObject> tempList = new List<GameObject>();

Dictionary<GameObject, List<GameObject>> pooledObjects = new Dictionary<GameObject, List<GameObject>>();
Dictionary<GameObject, GameObject> spawnedObjects = new Dictionary<GameObject, GameObject>();

public StartupPoolMode startupPoolMode;
public StartupPool[] startupPools;

Expand Down Expand Up @@ -64,18 +64,17 @@ public static void CreatePool(GameObject prefab, int initialPoolSize)
{
bool active = prefab.activeSelf;
prefab.SetActive(false);
Transform parent = instance.transform;
while (list.Count < initialPoolSize)
{
var obj = (GameObject)Object.Instantiate(prefab);
obj.transform.parent = parent;
obj.transform.SetParent(instance.transform, false); // worldPositionStays=false to keep UI objects spawning consistently
list.Add(obj);
}
prefab.SetActive(active);
}
}
}

public static T Spawn<T>(T prefab, Transform parent, Vector3 position, Quaternion rotation) where T : Component
{
return Spawn(prefab.gameObject, parent, position, rotation).GetComponent<T>();
Expand Down Expand Up @@ -104,7 +103,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 All @@ -118,7 +118,10 @@ public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 posi
if (obj != null)
{
trans = obj.transform;
trans.parent = parent;
if (parent != null)
{
trans.SetParent(parent, false); // worldPositionStays=false to keep UI objects spawning consistently
}
trans.localPosition = position;
trans.localRotation = rotation;
obj.SetActive(true);
Expand All @@ -128,21 +131,15 @@ public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 posi
}
obj = (GameObject)Object.Instantiate(prefab);
trans = obj.transform;
trans.parent = parent;
if (parent != null)
{
trans.SetParent(parent, false); // worldPositionStays=false to keep UI objects spawning consistently
}
trans.localPosition = position;
trans.localRotation = rotation;
instance.spawnedObjects.Add(obj, prefab);
return obj;
}
else
{
obj = (GameObject)Object.Instantiate(prefab);
trans = obj.GetComponent<Transform>();
trans.parent = parent;
trans.localPosition = position;
trans.localRotation = rotation;
return obj;
}
return obj;
}
public static GameObject Spawn(GameObject prefab, Transform parent, Vector3 position)
{
Expand Down Expand Up @@ -173,15 +170,20 @@ 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)
{
instance.pooledObjects[prefab].Add(obj);
instance.spawnedObjects.Remove(obj);
obj.transform.parent = instance.transform;
obj.transform.SetParent(instance.transform, false); // worldPositionStays=false to keep UI objects spawning consistently
obj.SetActive(false);
}

Expand All @@ -205,7 +207,7 @@ public static void RecycleAll()
Recycle(tempList[i]);
tempList.Clear();
}

public static bool IsSpawned(GameObject obj)
{
return instance.spawnedObjects.ContainsKey(obj);
Expand Down Expand Up @@ -356,7 +358,7 @@ public static void CreatePool(this GameObject prefab, int initialPoolSize)
{
ObjectPool.CreatePool(prefab, initialPoolSize);
}

public static T Spawn<T>(this T prefab, Transform parent, Vector3 position, Quaternion rotation) where T : Component
{
return ObjectPool.Spawn(prefab, parent, position, rotation);
Expand Down Expand Up @@ -405,7 +407,7 @@ public static GameObject Spawn(this GameObject prefab)
{
return ObjectPool.Spawn(prefab, null, Vector3.zero, Quaternion.identity);
}

public static void Recycle<T>(this T obj) where T : Component
{
ObjectPool.Recycle(obj);
Expand Down Expand Up @@ -509,4 +511,4 @@ public static void DestroyAll<T>(this T prefab) where T : Component
{
ObjectPool.DestroyAll(prefab.gameObject);
}
}
}
10 changes: 3 additions & 7 deletions ObjectPool.userprefs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" PreferredExecutionTarget="MonoDevelop.Default" />
<MonoDevelop.Ide.Workbench ActiveDocument="Assets/ObjectPool/Scripts/ObjectPool.cs">
<Files>
<File FileName="Assets/ObjectPool/Scripts/ObjectPool.cs" Line="4" Column="1" />
</Files>
</MonoDevelop.Ide.Workbench>
<Properties StartupItem="Assembly-CSharp.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" PreferredExecutionTarget="Unity.Instance.Unity Editor" />
<MonoDevelop.Ide.Workbench />
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>
Expand Down
Binary file added ProjectSettings/ClusterInputManager.asset
Binary file not shown.
Binary file modified ProjectSettings/GraphicsSettings.asset
Binary file not shown.
Binary file added ProjectSettings/NavMeshAreas.asset
Binary file not shown.
Binary file modified ProjectSettings/ProjectSettings.asset
Binary file not shown.
2 changes: 2 additions & 0 deletions ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
m_EditorVersion: 5.3.5f1
m_StandardAssetsVersion: 0
Binary file added ProjectSettings/UnityAdsSettings.asset
Binary file not shown.
Binary file added ProjectSettings/UnityConnectSettings.asset
Binary file not shown.