Skip to content

Commit 2224efe

Browse files
authored
Merge pull request UnityCommunity#48 from LesserKnownThings/object_pooling
Object pooling
2 parents 644e787 + 566ae0c commit 2224efe

File tree

12 files changed

+195
-0
lines changed

12 files changed

+195
-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.
Loading
Loading
Loading
Loading

Assets/Scripts/ObjectPooling/Pool.cs

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.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<h1> Object pooling helper class </h1>
2+
<br>
3+
<h3> These scripts help you set up an object pooling system in your game. It's pretty simple to use, let me show you how: </h3>
4+
5+
<br>
6+
7+
8+
<h3> 1. Initializing the pool </h3>
9+
<a> As you can see in the image bellow, you can initialize the pool by creating a new pool and then calling it in the start. </a>
10+
11+
![Init Image](https://github.com/LesserKnownThings/UnityLibrary/blob/object_pooling/Assets/Scripts/ObjectPooling/Images/Initialization.PNG)
12+
13+
You'll also have to add the namespace that the pool class is in **using UnityHelper.Pooling;**
14+
15+
<h3> 2. Before you start the game </h3>
16+
You will have to add the objects that you want the pool to instantiate when it starts. As you can see in the image bellow, the pool object looks like this in the editor:
17+
18+
![Look Image](https://github.com/LesserKnownThings/UnityLibrary/blob/object_pooling/Assets/Scripts/ObjectPooling/Images/PoolObjectLook.PNG)
19+
20+
The pool contains a List of objects that you want to instantiate in the scene. The objects are of type **PoolObject** which you can see here:
21+
22+
![List Image](https://github.com/LesserKnownThings/UnityLibrary/blob/object_pooling/Assets/Scripts/ObjectPooling/Images/PoolObject.PNG)
23+
24+
The **obj_to_pool** is the gameobject that you want to instantiate and the **amount_to_pool** is the amount of objects that you want to instantiate.
25+
26+
<h3> 3. Spawning the objects in the scene </h3>
27+
Spawning an object is very easy, the **Pool** class comes with a function called **Spawn_Item(Type type, Vector3 position, Quaternion direction)** it returns a **Component** so you can get direct reference to the object. Here's how you do it:
28+
29+
![Spawn Image](https://github.com/LesserKnownThings/UnityLibrary/blob/object_pooling/Assets/Scripts/ObjectPooling/Images/SpawnObject.PNG)
30+
31+
In my example I have a custom Object created by me called **CubeObj** it doesn't do much, but it helps with the example. So when you spawn an item in the scene you have to give the function the following parameters:
32+
33+
>**Type** the typeof object that the list needs to look for and spawn it in the scene
34+
35+
>**Position** the position where the item should spawn
36+
37+
>**Rotation** the rotation of the item
38+
39+
One more thing to remember. Pool has a function to bring back the objects to the pool, but it requires the object as a parameter. That's why I made an interface called **IPoolActions** that comes with **ReturnObjectToPool** void and will help you return the items to the pool. It's pretty easy, to return an item you simply have to **gameobject.SetActive(false)** and also reset its position **transform.position = Vector3.zero**.

Assets/Scripts/ObjectPooling/README.md.meta

Lines changed: 7 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)