5

Spawning an Army of Units

RTS games have a gameplay mechanic that allows you to spawn units and create large armies to battle enemies. But having so many objects in the same scene that can be easily created and destroyed requires good memory and CPU management, to avoid problems such as frame rate drops due to CPU spikes, out-of-memory issues, and memory leak issues due to inefficient memory usage. There is one programming pattern that works well in this scenario, which is called Object Pooling, and we are going to learn how to implement and use it to spawn our units.

In this chapter, we will create the configuration files for our units using the ScriptableObject class from Unity, which will have all the attributes we need to use a unit in ...

Get Creating an RTS Game in Unity 2023 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.