[DEPRECATED] EZ Object Pools v1.4.0

Hello all,

I am pleased to announce a major update to my object pooling system, EZ Object Pools.

EZ Object Pools is an easy-to-use object pooling system that is designed to simplify the use of object pools in your projects. It also integrates seamlessly into existing code! Also now included in the package are several extra scripts that make use of the system, and can prove to be quite useful!

*Works in Unity 4.5+ and Unity 5!

Features:

  • Ability to resize pools at runtime to accommodate object requests.

  • Pools that pull objects from a single, shared pool.

  • PooledObjects that can do special tasks when enabled and are automatically put back into the pool when finished.

  • Full access to all source code.

Asset Store Link
https://www.assetstore.unity3d.com/en/#!/content/28002

Guide & Docs Link
http://www.roadturtlegames.com/Downloads/EZ_Object_Pools_Documentation.pdf

Demo Link
http://www.roadturtlegames.com/Webplayers/EZ_Effects_Demo/

Feedback is greatly appreciated! And of course if you find any bugs or issues post them here and I will submit fixes for them promptly!

I just pushed out a fairly major update, here are the changes:

  • Improved internally how objects are retrieved from the pool: When the pool is empty, it no longer has to iterate through the entire list.
  • PooledObject components are now automatically added to objects created by a object pool, unless a PooledObject script or derivative already exists.
  • Objects are automatically made available for use when disabled (Using the PooledObject script’s OnDisable method).
  • The Disable() method of PooledObject is now no longer needed.
  • Fixed bugs with using pre-created object pools.

One of the slowest things about the object pool was that, when no object was available, it had to iterate through the whole list before it realized that no object was available. This is no longer the case. This did require a significant change to the way objects are retrieved and put back into the pool, but nothing that should affect existing code.

Secondly, PooledObjects have been changed to adjust for the change in how objects are retrieved and put back for use in the pool. Basically you should never really need to inherit from the PooledObject script anymore unless you really need access to the parent pool. An example of this might be if you are modifying an object transform’s parent, and want to set its parent to the parent pool when it is disabled, which you can’t do in the OnDisable method that the PooledObject script uses.

Why it is impossible to change pooled object’s parent?

[EDIT: I officially redact this bug report, since it was caused by a change I made to the asset. Because I’m just that clever!]

I found a bug in EZObjectPool.InstantiatePool(). It creates new object instances and adds them to the ObjectList, but it doesn’t add them to the AvailableObjects list. That means they never get used but still consume memory.
You can fix it by adding this line inside the for loop, around line 155:
AddToAvailableObjects(g);

Hey Jason,

Objects are added to the available objects list, just in a slightly weird way, and here’s how:

To create an object, the object pool calls the NewActiveObject() method, which Instantiates an instance of the pool’s template object, adds a PooledObject component to it (if one isn’t already on the template), and sets that object’s parent pool.

Then, it calls g.SetActive(false). That line is important because if we look at PooledObject.cs, we see an OnDisable() method, which is a Unity callback that is called whenever an object gets disabled. Here is where it adds this object to the parent pool’s available objects list.

Hope that clears things up!

ParentPool is a public field of a PooledObject, so I don’t see why it would be impossible for you to change it. It is hidden in the inspector, though. So to see it in the inspector you would need to open PooledObject.cs and delete the [HideInInspector] attribute from above the ParentPool field. May I ask why you want to change an object’s parent pool?

Aahhhh, that makes sense. Then the bug was mine! Because I changed the OnDisabled() to an explicit Recycle() since I don’t want to accidentally recycle an object that might get disabled for other reasons.

I apologize for the buggy bug report! :smile:

This asset has worked very well and is easy to build with. I plan to continue to use it for my current and future games.

Is there any special callback instead of OnEnable()? Because regular OnEnable is first fired once at the very beginning for every instance of pooled objects and then each time it is actually enabled (spawned) in game.

FIXED by adding

AvailableObjects[lastIndex].SendMessage("OnSpawn", SendMessageOptions.RequireReceiver);

in 2 places after

AvailableObjects[lastIndex].SetActive(true);

in EZObjectPool.cs

Hi, i think there maybe an bug in EZ Object Pools.

EZObjectPools.cs line 271

public void AddToAvailableObjects (GameObject obj)
{
    AvailableObjects.Add (obj);
}

i thinks it should change to

public void AddToAvailableObjects (GameObject obj)
{
    if (!AvailableObjects.Contains (obj)) {
        AvailableObjects.Add (obj);
    }
}

the bug happen when you put the obejct like

ParentNoded->Child_EZObject

then you manipulate the parentNode active, the child EZObject(EZObjectPools.cs) OnDisable() function will trigger.

so , it will add multiple time in AvailableObjects.

Hi,

I’ve to create a Texture2D pool.
How would I achieve this please?

I am trying to make a pool that holds all my particle fx. I have a few different particle fx is this possible?

how can i make for gun script?

Are there any videos or examples that I can follow to get this set up? The docs are a bit unclear.
I have an array of 10 different asteroids that I am trying to set up to use pooling.
Do I need a separate pool for each asteroid prefab?
What script goes on the asteroid?
How do I instantiate them using the pool?

Help please.
Mark

1 Like

I guess there is no support for this asset.
Another cold, abandoned asset by the side of the Information Super Highway…

Mark

1 Like