Tips on Improving the performance (fps) for your Unity game. (General topics)

There are several ways to improve the performance of a game when using Unity. Some tip and tricks are listed blow. I will go into more detail on each topic listing pros and cons and even alternatives in the future. Some of these tips will improve performance dramatically and some may only add 1 or 2 fps. However if all these tips are used, your game should see a performance boost!


Through the Unity UI system:

Almost every UI element has an Image(Script) component and within this component is a Raycast Target toggle. If this is checked it will hold Raycast information that you may not intend on using and un-checking this can improve performance when building a large UI system. When I created an inventory UI system the unchecked vs checked performance boost for around 50 UI elements was 5- 15fps on Android.

An example: With Raycast Target checked I can use the code below to get or set information about a click/touch from a GameObject. I will go into more detail about AddListener in a future post.
theButton.onClick.AddListener(delegate { SomeMethod(someData, otherData); });

However, if Raycast Target is checked and you are not intending on doing something like the above then unchecking Raycast Target on every GameObject's Image(Script) component is recommended.

Note: When creating certain UI elements such as touch scrolling this MUST be checked or touch/click information can behave incorrectly.

In your code:

It is best to not use foreach loops and go with a for loop or while loop instead. Foreach loops are meant for use with non-generic types (IList or IEnumerable) and will be much slower in the majority of uses. There are a few exceptions for this but I'll go over those in a future post.

Insuring your loops are set up correctly can also make a big difference in performance.
A standard for loop:
for(int i = 0; i < myList.Count; i++)
{
     myList[i].SetActive(true);  //Do something using that list
}
Using a loop this way is fine most of the time but when loops get more complex and when using nested loops, improvements can be made. The code below can improve the speed of the loop because it does not have to go into myList and access myList.Count and compare it to an int. Instead it only has to check an int to another int each iteration.

int count = myList.Count;

for(int i = 0; i < count; i++)
{
     myList[i].SetActive(true);  //Do something using that list
}

Yes this is only a small improvement but when dealing with nested loops and complex algorithms this can help speed up your code a lot!

Textures and image compression:

This can be a sensitive subject because having the highest quality assets is what we all want in our games. Unity's default Max Size for a Texture Type is 2048, this is fine for most applications but it is a good habit to set this value closest to the pixel size that your texture image is. Meaning if your image size is 350 set the Max Size to the next largest, 512 in this case. This saves size in you game and saves Unity from using extra memory.

Setting the Format of a Texture to Compressed or Automatic Compression is usually OK but it means the compression can be different depending on the texture type or size. This can make some texture appear inconsistent. The best method it to test out different compression to get the look and performance you want in your game. However, this can vary a lot depending on the "look" of the game so unfortunately the performance boost will vary significantly.


Other:

Using Unity's built in Profiler is a great way to learn what is eating performance in your game however, it is not always obvious what is taking up resources using this.
Unity can explain the Profiler much better than I could, here is some great info on the Profiler.


What are some ways you know of to improve your game?
Thanks for reading!


Comments

Popular posts from this blog

How to create slowly revealed text in a Unity 3d game

Fade in and fade out Script for Unity 3d

How to do chain damage in a Unity 3D game using C#