Optimize 2D Physics Core with multithreading
To improve the performance of 2D physics, run the simulation and your own code on multiple threads.
Read time 2 minutesLast updated 17 days ago
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.
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.CreatePhysicsWorld.DestroyPhysicsWorld.WriteDefinitionPhysicsWorld.ReadDefinitionPhysicsWorld.Reset
For more information about creating a multithreaded job, refer to Create and run a job.