Optimizing with the Object Pool Pattern

In most video games, there are a lot of things happening on the screen. Bullets are flying around, enemies are spawning around the map, particles are popping up around the player, and these various objects are loaded and rendered on the screen in the blink of an eye. So, to avoid putting strain on the Central Processing Unit (CPU) while maintaining a consistent frame rate, it's a good practice to reserve some memory for our frequently spawned entities. So, instead of releasing recently destroyed enemies from memory, we add them to an object pool to recycle them for later use. With this technique, we avoid the initial initialization cost of loading a new instance of an entity. In addition, because we ...

Get Game Development Patterns with Unity 2021 - Second 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.