Some ways you can optimize your unity project

If there is one thing to keep in mind when developing a video game, and which, above all, unfortunately, is only taken into account in the final stages of development, it is its optimization. Many titles that neglect this aspect receive a lot of complaints and criticism. One of the key points that can produce spikes in the performance of our game can be the user interface (or UI).

Understanding how Unity UI works

First of all, it must be clear that when the UI unity is rendered it creates a mesh to be able to represent each of the elements that are on the canvas. When there is a change in the canvas it is marked as dirty and the system is informed that it has to be recalculated and redrawn.

Another thing to keep in mind is that the canvas elements are the last to be rendered, so even though our canvas is configured as World Space and there are other elements in the scene that cover it, the pixels corresponding to the canvas will be calculated anyway.

Practical tips to optimize our UI

Another point to take into account is the interactive elements of our canvas. The UI elements that we create in Unity have the Raycast Target property. This property makes the elements of the canvas susceptible to being clicked or touched, that is, they can receive an input. This means that every time we click or touch on the Unity screen, it checks if there is an element of the UI that has this property checked and that remains in the projection line of that interaction with the screen. If all the elements of our canvas have the raycast property activated, we will be making Unity perform more calculations than necessary, slowing down the system. That is why it is important to make sure that all the UI elements with which it will not be possible to interact have the Raycast Target option unchecked.

The use of cameras on canvases defined with a Render Mode of Screen Space or World Space is also important. If in these two modes we do not specify which camera we are going to use, Unity by default will search for the main Camera of our scene. But to carry out this search automatically, it will be forced to carry out a search using the Find method, that is, it will have to go through all the objects in our scene until it finds the one that responds to the MainCamera. The use of Find-type methods is totally discouraged at the performance level, so it is highly recommended that if we use any of these types of render mode we always specify the camera manually.

When we want to hide a canvas the best solution is to deactivate the canvas Component of the object. Disabling the component will stop the object from making GPU calls and the canvas will stop rendering. In addition, as we do not hide the object, we simply deactivate a component, certain Garbage Collection tasks that could be called automatically if we were to act on the object, such as the OnDisable functions, will not be executed.

One of the key points of UI optimization is to reduce the use of animations within it, animations cause the canvas to be marked as dirty in each frame and have to be recalculated, if we have to make very sporadic animations it is better to try face a solution that makes the same change through code being this much less costly at the performance level.

We have seen some tips to optimize the use of our UI in our games. We must remember again that although the optimization task can be somewhat heavy since we have to take many things into account, and that if we do not plan it well from the beginning it can lead us to make many changes later, it is a key task when it comes to making a successful title.

Layers & Collision Matrix

Edit > Project Settings > Physics -> Layer Collision Matrix

Leaving all our GameObjects with the default layer, unless our game requires it, is not very efficient since, for each object, Unity has to calculate its collisions with the rest of the existing ones in the scene. Therefore, it is our responsibility to create our own layers, assign them to our objects and make a good configuration of the “Layer Collision Matrix” in order to achieve better optimization.

Avoid calls to Find … () methods

Although these types of methods are very powerful, they are quite expensive since they force Unity to go through all the GameObjects in memory until it finds the desired object. This is a little less worrisome in small projects, where there are a very small number of objects, but as the complexity of the project increases, the less advisable these calls to these functions become.

As a recommendation, use these methods as little as possible and try to access the objects you are looking for in other ways such as caching their references in code, through a reference in the inspector.

Level of Detail (LOD)

Using the “Profiler”

Window -> Profiler

Thanks to this tool we can take a look at the performance of our game. Details such as the memory usage we are using, how much CPU time is being used for a certain task, when physical calculations are being carried out, etc … we can analyze all of this from this window.

There is no need to talk about the importance of making use of this useful tool throughout the development process of our game, but… use it!

Systems Engineer with a huge experience working on IT infrastructure projects and passionate about software development, AR/VR, Big Data, and Machine Learning.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store