Poolable components

Let's first define an interface class for a component that can be used in the pooling system:

public interface IPoolableComponent {  void Spawned();  void Despawned();}

The approach for IPoolableComponent will be very different from the approach taken for IPoolableObject. The objects being created this time are GameObjects, which are a lot trickier to work with than standard objects because of how much of their runtime behavior is already handled through the Unity engine and how little low-level access we have to it.

GameObjects do not give us access to an equivalent New() method that we can invoke any time the object is created, and we cannot derive from the GameObject class to implement one. GameObjects are created either ...

Get Unity Game Optimization - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.