Version: Unity 6.7 Beta (6000.7)
Language : English
Run 2D physics in 3D space with 2D Physics Core
2D game development in URP

Optimize 2D Physics Core with multithreading

To improve the performance of 2D Physics Core, use your available CPU cores to run the physics simulation and your own 2D physics code on multiple threads.

Note: This documentation is about using Pose, Area, and Constraint components. To use 2D physics in the Unity Editor using components like the Rigidbody 2D component, refer to 2D physics instead.

Run the simulation on multiple threads

To increase the number of worker threads the 2D physics system uses to calculate the simulation, set the Simulation Workers property after you create a world. The number of CPU cores on your device might mean the system uses fewer threads than you request. For more information about this property, refer to Physics Simulation Definition asset reference.

To avoid performance issues, avoid changing the number of simulation workers at runtime.

Run your own code on multiple threads

To run 2D Physics Core methods on all available CPU cores, use the job system to split the calculations across worker threads. For example, use multiple threads to check for overlaps between a very large number of physics objects.

Most of 2D Physics Core is thread-safe, which means multiple worker threads can read and write the same physics data without interfering with each other. Inside a physics world, any number of threads can read data simultaneously, but only one thread can write data at a time. This is called a Write Once, Read Many (WORM) locking mechanism.

Each physics world is isolated from the others, so you can write data to different physics worlds on separate threads simultaneously.

The following methods aren’t guaranteed to be thread-safe, so you can’t use them in the job system:

  • PhysicsWorld.Create
  • PhysicsWorld.Destroy
  • PhysicsWorld.WriteDefinition
  • PhysicsWorld.ReadDefinition
  • PhysicsWorld.Reset

For more information about creating a multithreaded job, refer to Create and run a job.

Additional resources

Run 2D physics in 3D space with 2D Physics Core
2D game development in URP