Simple Pooling for Unity

Posted by
April 16th, 2015 1:27 am

I made a thing.  It might be helpful for people.

https://gist.github.com/quill18/5a7cfffae68892621267

Instantiating & Destroying large numbers of objects (bullets or large hordes of enemies) can sometimes cause a game to stutter during their creation or later during garbage collection.  Object Pooling counters this by simply deactivating objects instead of destroying them, then re-activating them when you need a new copy.

All the pooling solutions for Unity I could find seemed overwrought and often required a bunch of advanced setup on a prefab-by-prefab basis.  The solution I’ve come up with simply requires you to use SimplePool.Spawn() and SimplePool.Despawn() instead of Instantiate() and Destroy().  Pool sizes automatically grow to meet demand.  There is an option to preload objects if you know you’re going to need to spam out a bunch of something very quickly (for example, projectiles for a bullet-hell shmup).

WARNING: NOT YET TESTED IN A FULL PRODUCTION APPLICATION.


4 Responses to “Simple Pooling for Unity”

  1. SelfTitled says:

    I’m new to LD, but I thought you had to write everything from scratch?

    • sydan says:

      For the Jam (not the Compo) you may use third party assets. For the Compo you may start with your own code base as long as you declare it before hand and make if available to others. :)

    • quill18 says:

      Obviously not, unless you’re going to be writing your program directly in binary.

      The rules very clearly explain that you can use existing code libraries assuming they are available to other people. This includes all the classes and functions within Unity, for example, as well as **code** assets that can be found online for free or for pay. That additionally includes your own code libraries, but again they have to be available to other participants in some form.

      You CANNOT, however, use pre-existing non-code assets in the compo.

    • SelfTitled says:

      Thanks for clarifying :)

Leave a Reply

You must be logged in to post a comment.

[cache: storing page]