When objects are repeatedly instantiated and destroyed you will likely notice the occasional performance spike during garbage collection which can take several seconds before the frame rate smooths again. This is very common when firing projectiles and creating explosions, but will also occur when painting and erasing tiles with associated game objects at runtime.
Instead of trashing unwanted objects it is better to recycle them so that they can be reused over and over again. This technique is usually referred to as pooling. Essentially unwanted objects are placed into a pool until they are needed once again; new instances only need to be created when the pool is empty.
Due to popular request the logic for instantiating and destroying tile game objects is now encapsulated within object factory classes. Whilst no pooling solution is included with Rotorz Tile System, pooling can be added by implementing the IObjectFactory interface. This gives you the freedom to implement your own specialized solution or alternatively implement a wrapper for one of the existing solutions.
PoolManager by Path-o-logical Games is a very popular pooling solution for Unity which is very powerful and extremely easy to use. RtsPoolManagerObjectFactory is an open-source script which we developed to make it easy to use PoolManager with our tile system extension.
What sort of tiles can be pooled?
Since the latest release of Rotorz Tile System tiles can take a number of forms. The benefits of pooling can currently only be realized for game objects that are instantiated from prefabs.
Tile game objects that are instantiated from prefabs can be pooled using a custom object factory. Individual game objects are created with a mesh filter and renderer component when tiles are painted using non-procedural tileset brushes. These game objects cannot be pooled, though in such circumstances the procedural variety of tileset brushes is generally better. When the “Add Collider” option is used game objects are created to house box colliders. Like with the previous point, such objects cannot be pooled, though pooling can still be utilized by attaching the collider using a prefab instead!