Booniverse
8Nov/112

Object Pooling #2

A while ago, I made a post about a simple object pooling system I had made. Well, I decided to improve upon it. I was looking around online for some editor scripting bits and pieces to build a custom inspector for my pathing system - it was a bit fiddly to build paths, and I know from some videos and articles that I could make it much easier to use (and more error proof) by writing a custom inspector. Long story short: I built a custom inspector for my pathing system, and it works really nicely (I'll probably make a post about it at some point).

Not A.D.H.D.: Just an awkward segue.

While searching, I came across a commercial plugin for Unity on the asset store called PoolManager, by a company called Path-o-logical, and I was enamored by their simple approach to creating object pools. So, I decided to rebuild my object pooling system to be less fiddly, and to take some of the things I liked about the Path-o-logical plugin such as situational display of properties (i.e. hiding properties when they have a disabled dependancy). This was made easier by the fact that my object pooling system already successfully handled most of the control of the Path-o-logical plugin, such as being able to declare cull limits and delays.

Something I really like about my system over the Path-o-logical one is the way in which pooling is managed by a clearly specified PoolManager object. I find Path-o-logical's approach to be a little round-about, and probably not quite as clear as it should be. I suspect this is due to Path-o-logical's PoolManager being incredibly robust with a whole bunch of functionality that I don't intend to include in my own PoolManager, but which invariably resulted in their chosen workflow. For me, I just want a PoolManager GameObject I can drag and drop to the scene, and then the ability to drag-and-drop prefabs to the PoolManager in order to create a pool.

It was a little bit more complicated than I expected. When isn't it?

I ran into a few problems early on with the inspector not showing properties for a custom PrefabPool class. This class handles a pool of a single prefab type: Pre-loading, spawning, despawning and culling. Eventually I figured out I needed to mark the custom class as [Serializable] in order to have Unity display it in the inspector. I think it's worth documenting that to help me remember, because I'm sure I'm going to run into that tiny hurdle again.

After I got that working, I wanted to create a custom editor script for the PoolManager which would display object pools in a more user-friendly manner than the default inspector. This is what I came up with:

It bares some similarities to the Path-o-logical plugin: I've renamed my variables to be more readable, and I only show properties such as CullAbove and CullDelay when their dependancy flag is checked. What I think it does better (for my own purposes) is give me quick, easy access to pre-allocation count, which I will expand to include allocation block overrides - meaning, when the pool is empty and a new instance is called, allow the user to declare the number of new instances to be generated at this point instead of just defaulting to 1. It's also easier to get new instances of an object, by using PoolManager.Pools["name"].Spawn();.

The best bit is the drag-and-drop box at the top of the inspector, which allows me to drag a prefab directly from the library, drop it on that box, and have it automatically added to the PoolManager. I dig that :)

I haven't tested it extensively, so it might still have a flaw or two, but what testing I have done has worked perfectly. Anyone wanna bet I spend more time scripting the editor now than making my damn game?

Filed under: Game Dev, Unity 2 Comments