Pooling for Unity3D in Undead Zombie Invasion

undead3

In the past year we developed the android game Undead Zombie Invasion, which had quite a lot of content and takes several hours to play through. Since this was a larger project, a lot of groundwork was beeing created too. Some of this i can explain and share in the following posts.

The game is in principle wave-like defense games, which means that reusing existing enemies and objects is an integral part of memory management.

Targets

The idea was to use a generic pooling management based on gameobjects from which objects can be requested, used and disposed for further used without as much hazzle as possible. Since gameobjects may have a specific livespan and won’t be revisited later in the gamecode, an option to dispose of gameobjects after a specific time was also added.

The GameObjectCache is implemented as a MonoBehaviour, as it depends on the time-managenent of Unity for the time-based objects.

Usage

Thus using the pooling mechanism consists of the following steps:

  • Drag the GameObjectCache Monobehaviour on a GameObject
  • Call the appropriate Methods in your Code

The idea behind it is actually quite simple.

  • Store the entries by their prototype object to be able to find them again
  • If not needed anymore, the object is set inactive instead of beeing destroyed
  • Run a time-ticker for the timed objects and set them inactive when reached
  • If you want to get an object, the dictionary is searched for the first inactive one
    • if found, this one is activated and returned
    • if not found, a new one is created, stored and returned

Leave a Reply

Your email address will not be published. Required fields are marked *