Programming and Code Architecture

  • Understanding the Unity Player Loop
  • Minimize code that runs every frame.
  • Avoid empty unity events.
  • Remove debug.log statements.
  • Use hash values instead of string functions.
    • ○ Use the animator’s hash functions instead of the string function.
  • Choose the right Data structure.
    • ○ Array, list, directory
    • ○ recommended read (MSDN guide to data structures)
  • Adding Components at runtime.
    • ○ Find, GameObject.GetComponent and Camera .main (in versions prior to 2020.2) can be expensive, so avoid calling them in Update methods. Instead, call them in Start and cache the results.
  • Use Object Pooling.
  • Use scriptable objects.
    • ○ Store enduring values or settings in a ScriptableObject instead of a MonoBehaviour. The ScriptableObject is an asset that lives inside of the project that you only need to set up once. It cannot be directly attached to a GameObject.
    • ○ Create fields in the ScriptableObject to store your values or settings, then reference the ScriptableObject in your Monobehaviours.
Project Configuration
  • Reduce or disable accelerometer frequency.
  • Disable unnecessary Player Setting
    /Quality Setting.

    • ○ In the Player settings, disable Graphics.API for unsupported platforms to prevent generating excessive shader variants. Disable Target. Architectures for older CPUs if your application is not supporting them.
    • ○ In the Quality settings, disable unneeded Quality level.
  • Disable unnecessary Physics.
    • ○ If your game is not using physics, uncheck Auto Simulation and Auto Sync Transforms. These will just slow down your application with no discernible benefit.
    • ○ Uncheck unnecessary collisions in the game.
  • Choose the right frame rate.
    • ○ Mobile projects must balance frame rates against battery life and thermal throttling. Instead of pushing the limits of your device to 60 fps, consider running at 30 fps as a compromise. Unity defaults to 30 fps for mobile.
  • Avoid Large hierarchy
  • Transform once instead of twice
  • Assume Vsync is enabled
    • ○ Mobile platforms won’t render half-frames. Even if you disable Vsync in the Editor (Project.Settings.>.Quality), Vsync is enabled at the hardware level. If the GPU cannot refresh fast enough, the current frame will be held, effectively reducing your fps.

 

Assets

  • Import texture correctly
    • ○ Lower the Max Size
    • ○ Use POT
    • ○ Atlas your texture
    • ○ Toggle Read/Write to enable the option
    • ○ Disable unnecessary mip maps
    • ○ Compress texture.
  • Adaptive scalable texture compression(ASTC)
  • Adjust the mesh Import setting
    • ○ Compress the mesh
    • ○ Disable Read/Write
    • ○ Disable rigs and blend shapes
    • ○ Disable Normal and tangents (if possible)
    • ○ Check the Polygon count

 

Graphics and GPU Optimization

  • Universal Render Pipeline (URP)
    • ○ Single pass forward.
  • Batches and Draw calls
    • ○ Dynamic Batching
    • ○ Static Batching
    • ○ GPU instancing
    • ○ SPR batching (URP)
    • ○ Use Frame Debugger
    • ○ Disable Shadows
    • ○ Bake Lights
    • ○ Use Level of Details(LOD)
  • Limit camera Use
  • Keep Shaders simple
  • Limit post-processing

 

User Interface

  • Divide Canvases
  • Hide invisible UI elements
  • Limit graphic raycaster
  • Avoid Layout Groups
  • Use multiple resolutions and Aspect Ratios.

 

Audio (when possible)

  • Make Sound mono
  • Use wav files
  • Compress the clip and reduce the Compression bitrate.
    • ○ Use Vorbis for most sounds (or MP3 for sounds not intended to loop).
    • ○ Use ADPCM for short, frequently used sounds(e.g. footsteps, gunshots)
    • Small clips (<200kb) should be Decompress on load
    • Medium clips (>=200kb) should be Compress in Memory
    • Large clips (Bg Music) should be set to Streaming
  • Unload muted audio sources from memory

 

Animation

A Humanoid Rig consumes 30–50% more CPU time than the equivalent Generic Rig because it calculates inverse kinematics and animation retargeting each frame, even when not in use. If you don’t need these specific features of the Humanoid Rig, use the Generic Rig.

  • Avoid Excessive use of Animators

 

Physics

  • Optimize your settings
    • ○ Prebake collision meshes
    • ○ Uncheck Auto Simulation
    • ○ Uncheck Auto Sync Transforms
      • ■ These will slow down your application with no discernible benefit.
    • ○ Simplify Colliders

 

Conclusion

Performance optimization is a vast topic. Understand how your mobile hardware operates, along with its limitations. In order to find an efficient solution that satisfies your design requirements, you will need to master Unity’s classes and components, algorithms and data structures, and your platform’s profiling tools

Of course, a little bit of creativity helps here, too.

Join our newsletter

Volutpat vel turpis nulla lorem sed semper. Aliquam sagittis sem libero viverra vehicula nullam ut nisl.

Leave A Comment