Published on

Unity performance tips

Authors
  • disable AudioSource if it's too far away or can't be heard

  • disable Animator if not visible

  • on awake do not enable/disable objects, do it in editor beforehand

  • for procedural mesh collider - generate it on separate thread to not stall the main thread

  • AudioSource.PlayOneShot creates a new audio source, play it and destroy it. make your own pool solution instead

  • Debug.Log is very expensive, use it only for debugging

  • setting .name is expensive, do it only in editor

  • .tag == tag is expensive, use .CompareTag(tag) instead

  • build navmesh on separate thread (install NavMeshComponents)

  • sleeping rigidbody still wastes CPU. I destroy it if no player controls entity, but create it when someone gets control

  • Don't use CharacterController.OnControllerColliderHit(ControllerColliderHit other). It generates 80B of garbage each frame, because ControllerColliderHit is class recreated each frame. Better manuall check ground using SphereCast, which doesn't allocate anything (same as Raycast, they both are just check of distance to colliders)

  • Disable shadows on what is not important, or user basic shapes

  • Use cheap colliders. Sphere, or Box. Better avoid MeshCollider to save performance

  • Unity methods which return Array - always generate garbage. Carefully cache them before needed (e.g. meshRenderer.materials)

  • Dont change UI canvas each frame, it rerenders it

  • if using yield return new WaitForSeconds();, make sure to cache the Wait. It is recreated each frame (if looped), so allocated garbage

  • original date: '2024-03-20'

  • modified: '2024-09-22'