Wednesday, July 24, 2013

Unity optimizations to boost performance for mobile!

I know some of the below points will be obvious, but just though I would list it down for anyone suffering from performance issues especially for mobile!

The Unity Profiler is indeed the best tool to identify, troubleshoot & hence fix a lot of performance related issues with your game. Do use it if you have a Pro license. It has helped me tremendously to fix performance issues related to my upcoming mobile game.

So a quick list on what could be done to boost performance on your Unity mobile game.


Change target fps

By default the target fps for your game is set to 30 by Unity for mobile devices. So just add the below line to take your target fps to 60!

Application.targetFrameRate = 60;

Don't use Dynamic Fonts 

Dynamic fonts affect performance if it gets updated during runtime. It is not good for showing say Score in your game as it will try to recreate the texture if the size is too big. So e.g. if you say need a font size say 60 to render on the screen and then change the score frequently, you will notice hiccups during rendering. So the best option would be is import 4 different sizes of fonts and fix them to static. I usually have 4 fonts with different names and have fixed their size during import option. I also have used ASCII text option to limit my texture size.

Use references of GameObjects & call them in Awake()

Want to call another script in your current script, well try avoiding calling it every frame in the Update method of your script. Reference the GameObject you want to call in the Awake() function of your script and then just use it in Update(). 

void Awake()
{
    gameFXRef = GameObject.FindGameObjectWithTag("gameparticlefx");
    ingameHud = GameObject.FindGameObjectWithTag("ingamehud");
              playerPos = playerRef.GetComponent().getPlayerPos();
}

Use OnGUI only if needed

In Unity the OnGUI() function is used to render anything in 2d for rendering buttons, menus, text, etc. Always remember to use them only if the script demands it. Even if the function is written in your script & is left empty, the function will still be called and its a overhead on your script execution. So comment/delete the entire function if its empty & not being used. 

void OnGUI()
{
    //empty...please delete!
}

Dynamic Shadows Off

Try not to use Dynamic shadows for mobile platforms. It is very performance intensive and can cause some serious fps drops. Use them only if you absolutely need them. You can always have a Quality setting to have it on toggle.



Use Mobile Shaders only

Mobile shaders are optimized for smartphones. By default when you import any image/texture into your project, Unity will set the Shader to be used to "Diffuse". Make sure you select each & every texture and change the shader to mobile diffuse shader instead. 


Use Low resolution textures for mobile

Always remember to check what area is your 3d textured object occupying the scene. If its relatively small then the texture resoltuion for that 3d object could be as low as 64x64. So make sure you don't bloat it up to 1024x1024 which Unity keeps it as default.