Friday, March 8, 2013

Tips for Unity3D mobile development

There have been several articles written how to optimize Unity3d for mobile development.  Most of those articles focus on draw calls, polygon reduction, texture atlases, etc.  Those are all great topics, but I wanted to pass on a few more tips.  Enjoy!


1) Favor cookie-cut geometry over cookie-cut textures.

As it is well known, mobile devices are not optimized for handling textures with transparencies.  Instead of having a cookie-cut alpha channel on a textures, it is often possible to achieve similar results by simply increasing the geometry. 

For example, if you needed to create a "star" you could use a 2d plane textured map with a an alpha-blended or cut-out image of a star.  Alternatively, you could model the outline shape simple star (a few more polygons will do the trick) and then apply a non-transparent texture.  In this second way, the geometry provides the cookie-cut.  While this technique is not appropriate for all cases, it does often work well and leads to higher performance, because mobile devices are optimized for polygons not transparent textures.


2) Use a texture atlas for in game text.

Never use text GUI elements during gamlay, and try to avoid Text3D/Mesh (as they use transparencies).  Instead, when possible, using an opaque texture atlas.  For example, in our recent game we wanted to display the number of coins the user has collected.  So we created this texture atlas.

By dynamcailly moving the UV coridatnes on a set of rectangle polygons, we were able to achieve much faster frame rates than if we were using text3d or text meshes.  It is a bit of work to set this up, but it is worth it (assuming you do not need transparent text).   To change the UV coordinates:

        // Grab the mesh first
        Mesh mesh =gameObject.GetComponent<MeshFilter> ().mesh. 

       //Create an array of uvs... set them to be whatever you want
       Vector2[] uvs = new Vector2[4];
      
      // Apply the UVs to your mesh
      mesh.uv = uvs;


3) Avoid objects GameObjects with attached Behavior scripts

The easiest way to attached code to an GameObject is to attach a MonoBehavoir script.  However, this comes with a performance penalty.  Instead, I would recommend limiting the number game objects that have attached scripts.   In fact, in many cases you can almost do away with MonoBehavoir scripts, aside from a a few "main" scripts that load the objects and gets things initialized.

For example, consider creating a custom star field that has hundreds of stars.  To create this, you could have hundreds of GameObjects with attached behavior scripts.  But this would come with quite a performance penalty. 

Instead, you could have a single behavior script that contains an array of GameObject Transforms.  This single script simply moves the transforms of each star at each frame.  This approach saves memory and saves cpu cycles.  Coupled with a nice object orientated design, you can take this same approach and extend it to most objects in your scene.


4) Look for the "new" keyword and avoid it when possible

The "new" keyword allocates a new object on the heap (or stack if a struct).  This operation is a bit expensive, and if used frequently will force frequent garbage collection.  Clearly we need to use "new" to get new objects, but avoid creating new objects within functions that are called at each frame.  If it is being called each frame, there is no reason to construct a new object each time.  Create the object once, in the start() or constructor, and then re-use it.   Some might consider this over optimization, but I consider it smart game programming.  It is a simple paradigm to follow and will drastically improve game performance.


No comments:

Post a Comment