|
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. |
3 | 3 |
|
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. |
8 | 7 |
|
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. |
10 | 9 |
|
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) |
18 | 12 |
|
| 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 | +} |
19 | 21 | ```
|
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 |
| - |
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 |
| - |
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 |
40 | 22 |
|
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. |
42 | 24 |
|
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 | +``` |
45 | 31 |
|
46 |
| - |
47 |
| - |
| 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