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

Instantiate a custom amount of objects on startup to save even more performance #1

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
4 changes: 2 additions & 2 deletions Assets/ObjectPool/Demo/Scripts/Turret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class Turret : MonoBehaviour

void Start()
{
bulletPrefab.CreatePool();
bulletPrefab.explosionPrefab.CreatePool();
bulletPrefab.CreatePool(10);
bulletPrefab.explosionPrefab.CreatePool(10);
}

void Update()
Expand Down
75 changes: 72 additions & 3 deletions Assets/ObjectPool/Scripts/ObjectPool.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public sealed class ObjectPool : MonoBehaviour
{
Expand All @@ -16,9 +17,18 @@ public static void Clear()
}

public static void CreatePool<T>(T prefab) where T : Component
{
CreatePool(prefab, 0);
}

public static void CreatePool<T>(T prefab, int initialAmount) where T : Component
{
if (!instance.objectLookup.ContainsKey(prefab))
instance.objectLookup.Add(prefab, new List<Component>());

if(initialAmount > 0)
for(int i = 0; i < initialAmount; i++)
SpawnInactive(prefab);
}

public static T Spawn<T>(T prefab, Vector3 position, Quaternion rotation) where T : Component
Expand All @@ -44,6 +54,7 @@ public static T Spawn<T>(T prefab, Vector3 position, Quaternion rotation) where
return (T)obj;
}
}

obj = (T)Object.Instantiate(prefab, position, rotation);
instance.prefabLookup.Add(obj, prefab);
return (T)obj;
Expand All @@ -60,20 +71,58 @@ public static T Spawn<T>(T prefab) where T : Component
return Spawn(prefab, Vector3.zero, Quaternion.identity);
}

public static T SpawnInactive<T>(T prefab) where T : Component
{
if (!instance.objectLookup.ContainsKey(prefab))
instance.objectLookup.Add(prefab, new List<Component>());

var obj = (T)Object.Instantiate(prefab, Vector3.zero, Quaternion.identity);
obj.transform.parent = instance.transform;
obj.gameObject.SetActive(false);

instance.objectLookup[prefab].Add(obj);

return (T)obj;
}

public static void Recycle<T>(T obj) where T : Component
{
if (instance.prefabLookup.ContainsKey(obj))
if(instance.prefabLookup.ContainsKey(obj))
{
instance.objectLookup[instance.prefabLookup[obj]].Add(obj);
instance.prefabLookup.Remove(obj);
obj.transform.parent = instance.transform;
obj.gameObject.SetActive(false);

}
else
Object.Destroy(obj.gameObject);
}

public static void RecycleAll<T>(T obj) where T : Component
{
var active = instance.prefabLookup.Keys.Where(p => p.GetType() == typeof(T)).ToList();

if(active.Count > 0)
for(int i = 0; i < active.Count; i++)
Recycle(active[i]);
}

public static List<T> GetAllOfType<T>() where T : Component
{
var keys = instance.objectLookup.Keys.Where(p => p.GetType() == typeof(T)).ToList();

List<T> objects = new List<T>();

if(keys.Count > 0)
foreach(var key in keys)
if(instance.objectLookup[key].Count > 0)
foreach(var obj in instance.objectLookup[key])
objects.Add(obj as T);


return objects;
}

public static int Count<T>(T prefab) where T : Component
{
if (instance.objectLookup.ContainsKey(prefab))
Expand Down Expand Up @@ -102,6 +151,11 @@ public static void CreatePool<T>(this T prefab) where T : Component
{
ObjectPool.CreatePool(prefab);
}

public static void CreatePool<T>(this T prefab, int initialAmount) where T : Component
{
ObjectPool.CreatePool(prefab, initialAmount);
}

public static T Spawn<T>(this T prefab, Vector3 position, Quaternion rotation) where T : Component
{
Expand All @@ -115,14 +169,29 @@ public static T Spawn<T>(this T prefab) where T : Component
{
return ObjectPool.Spawn(prefab, Vector3.zero, Quaternion.identity);
}

public static T SpawnInactive<T>(this T prefab) where T : Component
{
return ObjectPool.SpawnInactive(prefab);
}

public static void Recycle<T>(this T obj) where T : Component
{
ObjectPool.Recycle(obj);
}

public static void RecycleAll<T>(this T obj) where T : Component
{
ObjectPool.RecycleAll(obj);
}

public static List<T> GetAllOfType<T>(this T prefab) where T : Component
{
return ObjectPool.GetAllOfType<T>();
}

public static int Count<T>(T prefab) where T : Component
{
return ObjectPool.Count(prefab);
}
}
}