Skip to content

Commit 68e74b1

Browse files
Adding Object Pooling
1 parent 644e787 commit 68e74b1

File tree

6 files changed

+149
-0
lines changed

6 files changed

+149
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace UnityHelper.Pooling
2+
{
3+
/// <summary>
4+
/// Interface to remind you to add the return to pool function
5+
/// </summary>
6+
public interface IPoolActions
7+
{
8+
void ReturnObjectToPool();
9+
}
10+
}

Assets/Scripts/ObjectPooling/IPoolActions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
namespace UnityHelper.Pooling
8+
{
9+
[Serializable]
10+
public class Pool
11+
{
12+
/// <summary>
13+
/// The objects that you want to add to the pool
14+
/// </summary>
15+
public List<PoolObject> objects_to_pool = new List<PoolObject>();
16+
17+
/// <summary>
18+
/// All the objects that were added to the pool
19+
/// </summary>
20+
private List<GameObject> pool_objects = new List<GameObject>();
21+
22+
/// <summary>
23+
/// Initializes the pool in your scene
24+
/// </summary>
25+
public void Start_Pool()
26+
{
27+
var pool = new GameObject("Pool");
28+
29+
foreach (var item in objects_to_pool)
30+
{
31+
var sub_parent = new GameObject(item.obj_to_pool.name);
32+
33+
for (int i = 0; i < item.amount_to_pool; i++)
34+
{
35+
var _obj = UnityEngine.Object.Instantiate(item.obj_to_pool);
36+
_obj.transform.SetParent(sub_parent.transform, false);
37+
_obj.SetActive(false);
38+
pool_objects.Add(_obj);
39+
}
40+
41+
sub_parent.transform.SetParent(pool.transform, false);
42+
}
43+
}
44+
45+
/// <summary>
46+
/// Gets the item from the pool and spawns it at the required position
47+
/// </summary>
48+
/// <param name="type">The type of script in the pool that you need to spawn</param>
49+
/// <param name="position">The position where you want the item to spawn</param>
50+
/// <returns>A component of the needed type</returns>
51+
public Component Spawn_Item(Type type, Vector3 position, Quaternion direction)
52+
{
53+
foreach (GameObject item in pool_objects)
54+
{
55+
Component comp = null;
56+
item.TryGetComponent(type, out comp);
57+
58+
if (comp == null)
59+
continue;
60+
61+
if (item.activeSelf)
62+
continue;
63+
64+
item.transform.position = position;
65+
item.transform.rotation = direction;
66+
item.SetActive(true);
67+
return comp;
68+
69+
}
70+
71+
return null;
72+
}
73+
74+
/// <summary>
75+
/// Brings the item back to the pool
76+
/// </summary>
77+
/// <param name="item">The item to bring back to the pool</param>
78+
public void Return_Item(GameObject item)
79+
{
80+
item.SetActive(false);
81+
item.transform.position = Vector3.zero;
82+
}
83+
}
84+
}

Assets/Scripts/ObjectPooling/Pool.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using UnityEngine;
7+
8+
namespace UnityHelper.Pooling
9+
{
10+
[Serializable]
11+
public class PoolObject
12+
{
13+
/// <summary>
14+
/// The gameobject that you want to spawn
15+
/// </summary>
16+
public GameObject obj_to_pool;
17+
/// <summary>
18+
/// The amount of objects that you want to spawn
19+
/// </summary>
20+
public int amount_to_pool;
21+
}
22+
}

Assets/Scripts/ObjectPooling/PoolObject.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)