Honkbark studios menu logo

Optimize game performance with Unity3D

Optimize game performance with Unity3D

Please share this post with your friends

How do you optimize game performance with Unity3D ? These are the steps taken to get Friendsheep to run smooth with high FPS on mobiles. Some of these tips are mobile platform specific, but most of them you can use in every game.

 

The profiler

To find out what is unoptimized in your game you can use the profiler that comes with unity. You can find it by clicking on Window -> Profiler.

window profiler - optimize game performance with Unity3D
window profiler

And this is what the profiler window looks like. Play your game and you will see what is using resources in it, it might for example be renderings, scripts or the physics engine that is doing to much work. This is what Friendsheep looks like in the profiler before optimizing it.

Friendsheep profiler unoptimized
Friendsheep profiler unoptimized

You can run the profiler while playing the game on your phone as well. Just go to Edit-> Build settings…  Then select development build and autoconnect profiler. Connect your phone to your computer with a cable and select build and run.

You can read more about the profiler at Unity3D manual.

 

Static objects

There is a property on the GameObject in unity that is called Static. This is used to tell Unity systems if the gameobject is moving or not. By going through all you game objects in your scene and setting this property to true if your object is not moving you tell unity it won’t be invalidated by a change in the objects position. So no need to do calculations on that object, which gives unity time to work on something else instead.  You can read more about the static property at unity documentation.

Static game object
Static game object

 

Disabling accelerometer on iOS devices

This tip is only for iOS devices. The accelerometer is used for dectecing changes in the position of the device the player is holding. If your game does not use the accelerometer its better to turn it off. Or you can change how often you unity game will sample it. The default is 60 times per second (hz).

This setting is set in the Player settings. Go to Edit -> Project Settings -> Player. Then set the value you prefer in the Accelerometer Frequency dropdown.

Accelerometer Processing Frequency
Accelerometer Processing Frequency

Read more about tuning accelerometer processing frequency at unity documentation.

 

Simplify colliders

For example a Mesh or a polygon collider uses more resources to calculate collisions than a more primitive collider like the box collider.

When switching from polygon collider to boxcollider on objects that didn’t need that well defined collissions makes it easier for the physics engine.

Or maybe you can delete som polygons on the existing collider to make the physics engine do less computations.

 

Simplify Polygon collider - Friendsheep
Simplify Polygon collider – Friendsheep

This is what made the most difference in when optimizing friendsheep. When looking in the profiler i could see that Physics2D.FixedUpdate used a lot of resources. This took care of that problem.

 

Cache component lookups

Update on this tip from Max Stankevich:

“most common mistake in unity performance optimization guides is to suggest transform caching :( people start to cache ALL components without need and as a result – unwanted memory waste”

So make sure to keep an eye on the memory in the profiler.

The large optimization problems in Friendsheep is already taken care of. But i wanted to do more to optimize! This tip i found over at Glen Stevens’ thoughts on optimizing unity games. This is what he writes about it:

“A simple way to optimize component lookups that occur often is to cache the result. This is helpful for components that don’t change ever but are used often.”

For example when having code like this one, that calls on methods on the transform property.

using System.Collections; 
public class example : MonoBehaviour { 
    void Update() { 
        transform.Translate(0, 0, 5); 
    } 
}

You should add this:

using System.Collections;
using UnityEngine;

public class example : MonoBehaviour {
    Transform thisTransform;
    public new Transform transform {
        get {
            if (thisTransform == null) {
                thisTransform = base.transform;
            return thisTransform;
        }
    }
    void Update() {
        transform.Translate(0, 0, 5);
    }
}

“The latter code will run a lot faster since Unity doesn’t have to find the transform component in the game object each frame. The same applies for scripted components, where you use GetComponent instead of the transform or other shorthand property.”

 

Physics Layers

By default every game object in unity is added to the Default layer. This makes unities physics engine having to calculate collisions between every object. This is probably not what you want, and it is up to the developer to add game objects to the correct layers. This also fixed a lot of the Physics2D.FixedUpdate problems in the profiler.

Unity has made a video where they explain the layers functionality. Check out that video first, then read the topic Layers and collision matrix 

 

LAYERS AND COLLISION MATRIX
Layers and collision matrix

 

These are the steps i took to optimize Friendsheep to run smooth. I know every games is different but hopefully some of these examples could help you, or get you on your way to optimize your own game.

Friendly game-developers in the linkedin group for Unity has given their optimization tips if you are looking for more.

Categories: Unity3D