Thoughts on optimizing the battle scene -- lasers & projectiles


We want to do our best to portrait epic space battle scenes in our game. If those battles happen in the real life, there will be countless number of lasers and projectiles, and we want to show them in our game. Although we decided to calculate the damage of each command solely on the server to reduce the performance impact, we still need to  spawn them on the client's side. The most basic way of spawning lasers in Unity is to make a laser prefab, and instantiate a laser game object whenever in need. It is ok to do that if there is limited requirement of the quantity of game object. However, when there are hundreds or thousands game objects instantiating at the same time, the FPS will drop, because the CPU need time to allocate memory for every instantiated game object. The stacked calls will substantially impact the performance, and we need to solve that.

1: Object Pooling

Object Pooling is the easiest way to optimize performance for instantiating large quantity of the same game object. The concept is same as the name, use a "pool" of game objects.  Basically, we instantiate a large amount of disabled laser game objects beforehand, like when the game is in loading, and store their references in a "pool". Whenever we need to spawn a laser, we grab a laser instance from the "pool" and activate it. When the laser instance is supposed to be deleted, we disable it and put it back to the "pool". In this way, we reduced the need of allocating memory for new laser game objects, and the performance is improved.


Object pooling in Unity (Part 1) | Tripple Hill


2: Unity Job System & Entity Component System

Whereas Object Pooling is a good method to optimize the performance, the game will still slow down if there's really a large quantity of game objects instantiating and updating on the map. Unity only utilize one CPU core in default, and other CPU cores aren't in use if nothing is configured. Utilizing mutiple CPU cores will allow the game to process more events in each frame, and we can achieve that by using the Unity Job System. More info about the Unity Job System can be found here: https://docs.unity3d.com/Manual/JobSystem.html

Object-Oriented programming (OOP) is the traditional methodology in video game programming. Instances in the game are treated as objects, and have their own logics. Entity Component System (ECS) belongs to Data-Oriented programming. Instances in the game are treated as data, and they don't have their own logics. It consists Entity - The instance, Component - The data of the instance, and System - The logics of manipulating data. In the field of managing large quantity of instances, ECS performs much better than the traditional OOP ways. More info about ECS can be found here: https://docs.unity3d.com/Packages/com.unity.entities@0.17/manual/index.html


3: Unity DOTS

Unity DOTS is Unity's newest Data-Oriented Tech Stack. It is a improved structure of the above combination of multithreading and ECS, and Unity makes it easy to implement. It is still experimental and more about it can be found here: https://learn.unity.com/tutorial/what-is-dots-and-why-is-it-important#5f571864edbc2a011d1ad21a

Get Hoag's End

Leave a comment

Log in with itch.io to leave a comment.