Unity C# Jobs with 40,000 Objects

Understand What is a framerate

  • 60 FPS means 16 ms Update loop
  • 100 FPS means 10 ms Update loop

Having 40,000 object in the scene presents a challenge for the Unity rendering layers. The C# Job System helps ensure your game logic doesn’t cause slow downs, then you just need to play a series of tradeoffs with the Unity engine so it doesn’t get in the way.

Spawning 40,000 on the screen and having them all within the camera’s view will cause the framerate to drop on its own because rendering them all will cause many draw calls.

Once you have your code for moving objects in C# Jobs, the next problem will be the renderer. At this point, moving or still, the need to render the objects will hurt framerate. You can make decisions that simplify the work of the renderer.

Optimizing Framerates

Tools to Assess Progress

  • The Stats button on the Game panel in Unity.
  • Use the Unity profiler to see where time is eaten up.

The Profiler (CTRL-7 on Windows) will tell you where the time is being spent and can be used to open up the Frame Debugger which helps identify issues in batching up draw calls.

Tips on Optimizations

Some things to look for when optimizing rendering and the main thread:

  • remove the collider component to avoid physics taking time
  • casting shadows - turn them off if possible
  • turn GPU instancing in the material you are using (helps batch draw calls)
  • combine objects where you can - parenting can reduce the culling and geometry work
  • Change the camera’s depth from 1000 to something much smaller - it will reduce how much is visible so it’s only acceptable in some situations.

Relevant Blog

Unity SRP Batcher

Moving Logic to the C# Job System

Packages to Install

  • Burst Compiler (forces install of Mathematics as a dependency)
  • Mathematics (for Unity.Mathematics package)

Unity’s Documentation

You can’t use the Unity Help menu to find documentation for packages but you can find them by clicking on the installed package in the Package Manager.

The C# Job System is now integrated in Unity. For this you will want to consult two parts of the standard Unity Scripting Reference:

  • Unity.Collections namespace for NativeArray etc
  • Unity.Jobs namespace for IJobXXX interfaces and the IJobXXXExtension classes
  • UnityEngine.Jobs for TransformAccess and TransformAccessArray