Skip to content

Commit b00acce

Browse files
authored
Update README.md
1 parent 6f3a988 commit b00acce

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed

README.md

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,33 @@
1-
# Unity-Object-Pool
2-
A very simple singleton object pooler for optimization. It can handle all of the object pooling during the game loading and is persistant throughout scenes
1+
# Unity Generic Object Pool
2+
An Updated Object pooler to increase performance when pooling many objects.
33

4-
Currently to get the objects from the pool you need to know the index in the pool, will be changed to use the name.
5-
# What is pooling?
6-
It is computationally expensive to instantiate and destroy objects like bullets that get re-used a lot.
7-
Its a lot more effective to instantiate them all in the beginning and to keep re-using them by setting them active/false
4+
# Why use Object Pooling?
5+
It is computationally expensive to instantiate and destroy objects, such as bullets, that get re-used a lot.
6+
Its a lot more performant to instantiate them all and to keep reference and to recycle them.
87

9-
This script can act as a pooling control hub, it will create all pooled objects you need at the start and objects can be called as and when needed by other scripts.
8+
To use this object pool you will need to create a script for every pool you will need, and only one object is allowed per pool.
109

11-
## How to setup
12-
1) Create a new empty GameObject
13-
2) Create a new script called ObjectPooler and attach it to the GameObject
14-
3) Replace the contents of ObjectPooler with contents of the ObjectPooler script that can be found in this repo.
15-
4) In the inspector, in the script component, enter the number of gameObjects you want pooled and then add their prefabs to the list.
16-
5) Increase Amount to Pool to at least 1. You can also check the 'Load More If None Left' box if you are unsure on how many you will need
17-
6) Access the Pooled Objects by using
10+
# Why use Queue instead of List?
11+
Queue is benificial to use because it acts similar to a LinkedList in C++. With a Queue, when we Dequeue an object it Pops the first element from the Queue. When we Enqueue an object, it is added to the end of the queue, creating a first in, first out system. We will not need to access objects at specific indecies, therefor a Queue is perfect for what we need. [Read More about when to use Lists or Queues here!](https://stackoverflow.com/questions/10380692/queuet-vs-listt)
1812

13+
## The Setup
14+
1) Create a new script, call it what you want your pool to be named, IE a enemy pool could be called EnemyPool.cs and attach it to a GameObject
15+
2) In the script, we will follow the example below. The script will inherite from GenericObjectPool.cs, we will then give it a component type, such as ObjectTobePooled.cs, this could be any script or component that is attachable to a GameObject.
16+
```
17+
public class GenericPool : GenericObjectPool<ObjectTobePooled>
18+
{
19+
public override void AddPoolReference(ObjectTobePooled objectToAddReference) => objectToAddReference.GetComponent<IObjectPool>().pool = this;
20+
}
1921
```
20-
GameObject GO = ObjectPooler.Instance.GetGameObject(/*Index of the Pooled Object*/);
21-
//Make changes to your gameObject here
22-
```
23-
24-
# In the Editor
25-
![image](https://user-images.githubusercontent.com/32739337/102151045-4fa96100-3e48-11eb-9bed-7c8a8cb7eb8f.png)
26-
27-
Here we have the option to enable debugging and to assign new objects to the pool
28-
29-
## Assigning new Objects to the pool
30-
![image](https://user-images.githubusercontent.com/32739337/102151204-b890d900-3e48-11eb-9eb3-d1654fcea0c7.png)
31-
32-
### Assignables
33-
Set the name you want to use for retrieving the GameObject from the pool
34-
35-
Set the initial amount you want to be created.
36-
37-
Set the GameOject to be pooled
38-
39-
### Settings
4022

41-
If you enable Load More If None Left, then if the pool runs out of the object you want, it will create a new one and add it to the pool
23+
3) We then need to override the virtual function AddPoolReference(), on every object that will be pooled, we want to this snipped of code. This creates an Interface that we access to give the pooled object the reference to the pool that it belongs to. This is done so that all we need to do is call pool.Recycle() when the object is no longer needed and it will know the proper pool to Recycle the object into.
4224

43-
## Debugging
44-
When Debug is enabled it gives more information on what the pool is doing
25+
```
26+
internal interface IObjectPool
27+
{
28+
public GenericPool pool { get; set; }
29+
}
30+
```
4531

46-
![image](https://user-images.githubusercontent.com/32739337/102151501-6ef4be00-3e49-11eb-8320-d3e5529cddf4.png)
47-
![image](https://user-images.githubusercontent.com/32739337/102151883-3b666380-3e4a-11eb-85bf-440009247c92.png)
32+
## How to use the Pool?
33+
To access the pool, we want to call our new PoolScript and use the Get() method to return the object we have pooled.

0 commit comments

Comments
 (0)