Friday 15 May 2015

12+1 Tips to Optimize your Unity 2D Mobile game. iOS and Android


Learning how to optimize a game for mobiles is something that is hard to teach. Whatever you will read here now will be meaningless unless you find yourself in similar situations. iOS and Android devices are unfortunately weak and this will force you be a stronger developer. While optimizing a program should not be done at the beginning of the development cycle, you will encounter easy to follow practises that will prevent you for doing the same mistakes again and again.

Optimizing a game is half experience half art. You have to learn the shortcuts and second ways of every Import option, Unity behaviour and Line of code you write. When you will find yourself in the situation were your game is too slow in that old iPhone or Android phone of yours, you will have this guide to give you some ideas of how to proceed. In my particular case, it has been ROAR! The first game I have had to optimize seriously due to Vuforia AR plugin already using a lot of the device resources. :)

  • Tip #0: Choose your development device wisely
The slower, the better. However, don't overdo it. Having a too slow device will slow you down and will make you optimize too early, when is not necessary. I guess somewhere in the middle is where the virtue is.

There has never been so much hardware to choose from! :P


Optimizing Build Size

Sometimes your games will use far too much space for no reason at all. Keeping the size of your build is very important as it will affect directly how many people will be able download your game. Also is important to note that when users need to un-install games from their device they will usually start with the bigger first. Call it survival :)


  • Tip #1: Compress textures and audio files
Build your game and check your Editor Log for the files that use more space in your game. Most of the time will be either textures or audio files. Textures should be squared and POT to be compressed, around 1024x1024. Preferably never above 2048. Audio files should have the compression enabled always, unless the sound is super short and compression messes up with it's quality (rare thing).

  • Tip #2: Pack your sprites
All the sprites in your UI and game should be packed whenever possible. In Unity 5 you have the Sprite Packer for free, but you may be using others like NGUI. The sprite packer, for example, is a great way of removing the limitation of POT sprites and atlas creation from your artist. Just set the packing tag and Unity will take care of everything for yourself, easily saving tons of space.

  • Tip #3: Enable stripping mscorlib level
Whenever your project (and plugins you are using) allows you, you should enable the stripping of unused dlls from your project. Chances are high that your 2D game will not use many of the Unity features, maybe not even physics, that use a lot of space.

  • Tip #4: Be sure to not include unnecessary assets
If you have a disabled gameObject in your scene, the unity build will include every asset that the gameObject references even if you never enable it. Clear your hierarchy from unused gameObjects and references to assets. Any file under a "Resources" folder will be included in the build as well, so be careful!


Remember that Optimization is like a present you do to your beloved players :)


Optimizing Memory Usage

As the Operational Systems get upgraded they will start to use more and more RAM leaving less for you (yes iOS, I'm looking at you... ¬¬). It's very wise to reduce the amount of assets loaded into memory as much as possible. Memory Pressure is the number 1 reason why your app will be killed by the OS. That's right, random crashes are usually memory crashes :)

  • Tip #5: Pack your sprites... smartly
Once the game starts running the files must be uncompressed, so they will again use a lot of space. However if you did your homework properly and packed the sprites conveniently you will still save a lot of RAM. The rule of thumb is pack together what appears together and try not to leave big empty spaces in your atlases. For example, you should pack all your UI into a sepparate atlas. In the case theree is a big part of the UI assets that you usually don't see, they should be packed in a different atlas. I almost forgot! Use 9-sliced sprites and avoid huge background pictures at all costs! (Instead build them from tiles or simpler sprites). Disable MIP maps.

  • Tip #6: Reduce quality of images and audio
Are you sure that you need a character that is 1024 pixels wide? Since expected resolutions for mobiles devices are 1024x768 I guess not (unless it will be as big as the screen). Your music loop is 3 minutes long? Maybe you can make it 30 seconds. It´s very easy to exceed the maximum quality you can display on a phone. Most of the people will not be able to see the difference in image or audio quality in their phone if you lower it down.

  • Tip #7: Be careful with the scene references
It happened once to me that I was doing an app with a lot of pictures. Depending which one the player selected I was Instantiating one of the prefabs from my long list. To my surprise, that list of references was causing ALL the prefabs to load into memory, and therefore all the images. Moral of the story? Profiling never lies.

  • Tip #8: Use Resources folder when necessary
The solution for the previous case is place all those prefabs/assets in the Resources folder and load dynamically the one you need. It's not as convenient as references because you will have to provide a path to the asset and moving it to another folder will cause problems. But I guess you can write an Editor script that handles that for you :)

This is what the OS is silently telling to you when it randomly closes your app.


Optimizing CPU Usage

An slow game is a dead game. Players usually accept many degrees of mediocrity: bad user interface, bad story, bad graphics... Everything can be forgiven but bad performance. You need to know your limitations and in situations where you have to decide you should usually sacrifice everything for the sake of decent Frames Per Second. That's the hard truth xD

  • Tip #9: Whatch out for syntax candy Eg: foreach loop.
This is one of the most repeated tips for CPU optimization in Unity. Because of the old version of the .NET framework that Unity uses many things are not as efficient as they should be. Since this is not changing soon you should get used to replace "foreach" loops for "for" loops and "lists" for "arrays" once in a while ;) More information on official code optimization tips here. General good code practices from any OOP language apply here aswell, be sure not to miss them :)

  • Tip #10: Hide those Debug logs.
Unity works with strings very slowly and you should run away from string manipulation. Because of this it makes no reason to keep those "Debug.Log" enabled when making the release builds. It's surprising how such small thing makes a big difference.

  • Tip #11: Optimize from the bottom to the top.
When you optimize your game you should look at the Profiler and the responsible classes for the slowness of your game should be quite clear. Start optimizing the ones that are more in the "bottom" of your game. This means that many other classes use them, like for example your AI or Pathfinding classes. With a little bit of luck there will be no need of optimizing the classes on the "top" ;)

  • Tip #12: Search first for classic bottlenecks like big nested loops or scripts instantiated many times.
Imagine that you have an script called "PlayerController" and another called "EnemyController". If you have 100 enemies in the screen at the same time, the "PlayerController" scripts will have to be 100 times slower in order to make the difference in your game. Then start optimizing your "EnemyController" script. Remove from it typical slow calls like "FindObjectOfType", they are forbidden on "Update". Cache variables or even Singleton classes can help you with the performance.

Deep profile allows you to track that slow method but it can affect performance itself! :/

Good luck with your optimizations, and remember to make first a great game that deserves them! :D

Where to go now? Check out this other amazing optimization tricks: iOS, mobile, sounds, general.

This is deffinitively not all of it. When it comes to Unity tips and tricks there is always more and more! Things I forgot to say or I simply ignore. Do you have any of them? Please share! :)

31 comments:

  1. Awesome work i like it very much . God bless you and keep it up .

    ReplyDelete
  2. This is a nice and informative, containing all information and also has a great impact on the new technology. Thanks for sharing it

    Subway surfer hacks

    ReplyDelete
  3. Awesome work i like it very much

    ReplyDelete
  4. Although I am not an information technology expert, I have learned a lot from the article and understood the inbuilt of the operating system of android games. I will be recommending this site to our clients who are majorly students accessing our Research Proposal Aid with the hope that your will post more technological articles in a regular basis.

    ReplyDelete
  5. Hello, blog spot neigbor!
    I like puzzle and brain games.
    I used to play it. I used to play mini games actually.
    time killing games.
    This is my taste.
    I looked around and liked your mini game blog.
    Try to visit my blog. This is my blog with passion.
    I would like to introduce good mini games.
    I think, Still there aren't many internet users look carefully and enjoy all the games.

    http://payworthiest.blogspot.com

    you can leave comment about your game on my blog and explain about your games.
    so that I can play it and post about it on my blog.

    ReplyDelete
  6. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of

    informative things...
    Fitness SMS
    Fitness Text
    Salon SMS
    Salon Text
    Investor Relation SMS
    Investor Relation Text

    ReplyDelete
  7. Thank you for sharing such an informative article. I really hope I can see other interesting posts. Keep up the good work!


    Melbourne SEO Service

    ReplyDelete
  8. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
    SEO company sydney | android app development company

    ReplyDelete
  9. Great post thanks for sharing for more update at
    iOS Online Training


    ReplyDelete
  10. Great info. I love all the posts, I really enjoyed, I would like more information about this, because it is very nice., Thanks for sharing.
    The Xmod Games Hacking APK software
    Auto Farm sources integrated Clash of Clans without Root

    ReplyDelete
  11. Dear Friends, thank you a lot for providing this amazing article here! It ontains a lot of great information we need to be aware of.

    ReplyDelete
  12. Good information... android and IOS development is one of the best industries which gives transformation for your career.
    Mobile game development company
    Android game development company
    Virtual Reality Shopping

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Thanks for sharing such useful & great tips about game developement. I am impressed by your writing style, You are very good at blogging. I am a big fan of your writing work, Thank you so much for sharing this post!
    Hire Game Developer India for creating Fantasy Sports App like Dream11 by MobiWeb Technologies.

    ReplyDelete
  15. Second you have to have a system that has been proven and that you can easily duplicate the processes. How To Make Money Using The Observa App

    ReplyDelete
  16. Very well written, check for more advances tutorials
    www.unitygeek.com

    ReplyDelete
  17. Wonderful blog.. Thanks for sharing informative blog.. its very useful to me.. Keep Posting SEO Companies In Chennai
    E-Commerce Development Company in Chennai

    ReplyDelete
  18. I am really happy to say it’s an interesting post to read. I learn new information from your article, you are doing a great job. Keep it up

    Hire Dedicated Java Developers

    ReplyDelete
  19. This information given by you has helped us a lot and this information will prove to be completely helpful for others. Thanks seo company in mumbai

    ReplyDelete