UPDATE: Okay, so I have to apologise to @gl33mer, as I made a mistake in my instructions! The updated instructions are below. The correct call to Spawn/Despawn does not use the PoolManager.Instance object reference, as both Spawn and Despawn are static methods, not instance methods. Sorry! Also, I've uploaded a small demo package where a sphere/cube are spawned every other second (intermittently). See blow for the link.
At the request of fellow tweeterer @gl33mer, I've uploaded my Unity pool manager component, Prefactory v1.0. I have to confess that I haven't done a great deal of stability testing with it, but I haven't had any problems with it either (and I've used it quite extensively in tests and in the few games I've published to the blog). It's fast and flexible, and hopefully of use to others. I'm working on adding extra functionality, such as string-based construction of game entities (something which would be super handy for my next little adventure) and I'll be sure to post an update when and if one is completed.
A better set of instructions than this blathering mess of a blog post would help, too. I'll see what I can do...
For the record, Prefactory v1.0 is completely free to use however you like. You don't need to credit me or anything. I don't care if you modify it, use it commercially, or get down and dirty with it (although I would be morbidly curious about how one does that with code, exactly).
A short message letting me know you're using it in something awesome would totally make my egotistical day, but that's totally optional
To use Prefactory v1.0:
1. Assign the PoolObject MonoBehaviour to any GameObject/Prefab you wish to pool. For PoolObjects, the following options are available:
Prefab Name: Must be filled in, and must be unique per poolable object. This is the name that the PoolManager uses to manage the object.
Is Dynamic: If true, the PoolObject's children will NOT be cached for enabling/disabling of GameObjects and renderers. Set this to true if the PoolObject's hierarchy will change.
You can trigger a PoolObject to auto-despawn with the PoolObject.DespawnAfterSeconds(float seconds) method, or a coroutine method CRDespawnAfterSeconds(float seconds) which will yield after the object is despawned.
PoolObject.Age is a float property with the PoolObject's age in seconds since last being spawned, while PoolObject.AgeAsScalar will return a normalized 0 - 1 float for any PoolObject which has had an auto-despawn triggered.
2. Create a new GameObject and assign it the PoolManager MonoBehaviour. Drag and drop PoolObjects onto the PoolManager's inspector as shown below:
The pool settings for each PoolObject appear as a collapsible roll-out, and are as follows:
Pre-Allocate: The number of PoolObject instances to create at level load.
Allocate Block: The number of new PoolObject instances to create when the pool becomes empty.
Hard Limit: If true, the PoolManager will return null once the spawn limit has been reached.
Limit: The spawn limit when Hard Limit is on.
Cull: If true, excess PoolObjects will be destroyed.
Cull Above: Any excess PoolObjects exceeding this count when Cull is on, will be destroyed.
Cull Delay: The time between culls, in seconds.
3. To get an instance from the PoolManager:
PoolManager.Spawn("prefabName"); // or PoolManager.Spawn(GameObject instanceOfPrefabToSpawn);
4. To despawn an instance back to the PoolManager:
That's all there is to it (LOL). It's not the world's most intuitive system, but it works! So there you go @gl33mer, I hope it comes in useful. And make sure you send me a link to your game when it's done!