Unity Design Patterns: Optimization

Some low level optimizations present at Unite Los Angeles and Unite Europe 2016.

Unite Los Angeles 2016

  • Move configuration data to Scriptable Objects.
  • Utilize the new instantiate call when parenting an object.
  • Don’t needlessly parent objects in production.
  • Batch changes to Transforms in order to save on transform change message calls.
  • Optimize game object transforms utilizing the Mecanim system.
  • Move static and dynamic canvas elements onto nested separate canvas.
  • Best Practices

Unite Europe 2016

  • Memory Profiler
  • Utilize AssetPostProcessor class to enforce project asset rules.
  • Disable Write for textures unless performing pixel operations.
  • Disable Mipmaps for UI and 2D textures.
  • Compress textures whenever possible.
  • Disable Read/Write for models to save mesh memory, unless using Mesh Colliders.
  • Disable Rig on non-character models.
  • Copy avatars for characters with shared rigs.
  • Enable mesh compression.
  • Audio files should be forced to Mono to save memory.
  • The Heap only expands at run-time, so manage allocations carefully.
  • Avoid using strings, utilize StringBuilders instead.
  • Avoid Closures and anonymous functions due to heap allocations
  • Avoid Boxing with Enums, instead create an IEqualityComparer class and pass it to your data structure.
  • Avoid Foreach loops.
  • Avoid parsers that utilize Reflection, it’s suggested to use Unity’s JsonUtility class for parsing.
  • When using Shader ID’s hash the string ID’s at startup and save them for reuse.
  • Avoid Vector3.zero and Quaternion.identity instead utilize const values for simple types and static readonly for complex types.
  • Only use these optimizations as needed.