Skip to content

Object pooling #48

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

Merged
merged 4 commits into from
Sep 11, 2020
Merged
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
10 changes: 10 additions & 0 deletions Assets/Scripts/ObjectPooling/IPoolActions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace UnityHelper.Pooling
{
/// <summary>
/// Interface to remind you to add the return to pool function
/// </summary>
public interface IPoolActions
{
void ReturnObjectToPool();
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/ObjectPooling/IPoolActions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 84 additions & 0 deletions Assets/Scripts/ObjectPooling/Pool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace UnityHelper.Pooling
{
[Serializable]
public class Pool
{
/// <summary>
/// The objects that you want to add to the pool
/// </summary>
public List<PoolObject> objects_to_pool = new List<PoolObject>();

/// <summary>
/// All the objects that were added to the pool
/// </summary>
private List<GameObject> pool_objects = new List<GameObject>();

/// <summary>
/// Initializes the pool in your scene
/// </summary>
public void Start_Pool()
{
var pool = new GameObject("Pool");

foreach (var item in objects_to_pool)
{
var sub_parent = new GameObject(item.obj_to_pool.name);

for (int i = 0; i < item.amount_to_pool; i++)
{
var _obj = UnityEngine.Object.Instantiate(item.obj_to_pool);
_obj.transform.SetParent(sub_parent.transform, false);
_obj.SetActive(false);
pool_objects.Add(_obj);
}

sub_parent.transform.SetParent(pool.transform, false);
}
}

/// <summary>
/// Gets the item from the pool and spawns it at the required position
/// </summary>
/// <param name="type">The type of script in the pool that you need to spawn</param>
/// <param name="position">The position where you want the item to spawn</param>
/// <returns>A component of the needed type</returns>
public Component Spawn_Item(Type type, Vector3 position, Quaternion direction)
{
foreach (GameObject item in pool_objects)
{
Component comp = null;
item.TryGetComponent(type, out comp);

if (comp == null)
continue;

if (item.activeSelf)
continue;

item.transform.position = position;
item.transform.rotation = direction;
item.SetActive(true);
return comp;

}

return null;
}

/// <summary>
/// Brings the item back to the pool
/// </summary>
/// <param name="item">The item to bring back to the pool</param>
public void Return_Item(GameObject item)
{
item.SetActive(false);
item.transform.position = Vector3.zero;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/ObjectPooling/Pool.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Assets/Scripts/ObjectPooling/PoolObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace UnityHelper.Pooling
{
[Serializable]
public class PoolObject
{
/// <summary>
/// The gameobject that you want to spawn
/// </summary>
public GameObject obj_to_pool;
/// <summary>
/// The amount of objects that you want to spawn
/// </summary>
public int amount_to_pool;
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/ObjectPooling/PoolObject.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions Assets/Scripts/ObjectPooling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<h1> Object pooling helper class </h1>
<br>
<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>

<br>


<h3> 1. Initializing the pool </h3>
<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>

![Init Image](https://github.com/LesserKnownThings/UnityLibrary/blob/object_pooling/Assets/Scripts/ObjectPooling/Images/Initialization.PNG)

You'll also have to add the namespace that the pool class is in **using UnityHelper.Pooling;**

<h3> 2. Before you start the game </h3>
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:

![Look Image](https://github.com/LesserKnownThings/UnityLibrary/blob/object_pooling/Assets/Scripts/ObjectPooling/Images/PoolObjectLook.PNG)

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:

![List Image](https://github.com/LesserKnownThings/UnityLibrary/blob/object_pooling/Assets/Scripts/ObjectPooling/Images/PoolObject.PNG)

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.

<h3> 3. Spawning the objects in the scene </h3>
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:

![Spawn Image](https://github.com/LesserKnownThings/UnityLibrary/blob/object_pooling/Assets/Scripts/ObjectPooling/Images/SpawnObject.PNG)

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:

>**Type** the typeof object that the list needs to look for and spawn it in the scene

>**Position** the position where the item should spawn

>**Rotation** the rotation of the item

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**.
7 changes: 7 additions & 0 deletions Assets/Scripts/ObjectPooling/README.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.