DOTS - Unity3d
- DOTS are Unity’s new high-performance, multithreaded, and Data-Oriented design and programming techniques.
- Create richer user experiences and iterate faster with C# code that’s easier to read and reuse across other projects.
- Rebuilding Unity’s Core: DOTS are using for the making of great games to run faster on multi-core processors without the heavy programming technique and it’s a headache.
- Highly Performant C# Code: DOTS provides safe multithreaded code for massive performance, while also optimizing thermal control and a battery of user’s mobile devices by moving from the Object-Oriented-Programming technique to Data-Oriented-Programming techniques. It’s easier to understand and reuse.
- Faster Performance: The new conversion workflow converts your Gameobject to Entity for making performance very faster and hyper-optimized, streamable data.
- DOTS Packages: For implementing DOTS we need to use some unity packages, some of the packages are in preview and those all packages are available on Package Manager->Select Preview Packages, of Unity version 2018.1 or above, they can make a huge impact on the performance of your project. DOTS packages are Jobs, Burst, Entities, Havok Physics, Hybrid Renderer, DOTS Platforms, etc...
- Unity DOTS consists of 3 main pieces:
- The C# Job System
- The Burst Compiler
DOTS = C# Job System + Burst Compiler + ECS
1) C# Job System:
Here mentioned only an overview of the C# Job System.
· It takes advantage of the multiple cores in today’s computers. It’s designed to empower C# programmers to write safe, fast and jobified code.
· The C# Job System exposes the Native C++ Job System allowing C# scripts to be jobified alongside Unity internal components.
· Provides protection from some of the pitfalls of multithreading such as race conditions.
What is a Job System?
- A job system manages multithreaded code by creating jobs instead of threads.
- A job system manages a group of worker threads across multiple cores. It usually has one worker thread per logical CPU core, to avoid context switching.
- A job system puts jobs into a job queue to execute. Worker threads in a job system take items from the job queue and execute them. A job system manages dependencies and ensures that jobs execute in the appropriate order.
What is a Job?
- A job is a a small unit of work that does one specific task. A job receives parameters and operates on data, similar to how a method call behaves. Jobs can be self-contained, or they can depend on other jobs to complete before they can run.
What are job dependencies?
- In complex systems, like those required for game development, it is unlikely that each job is self-contained. One job is usually preparing the data for the next job. Jobs are aware of and support dependencies to make this work. If jobA has a dependency on jobB, the job system ensures that jobA does not start executing until jobB is complete.
- The drawback to the safety system’s process of copying data is that it also isolates the results of a job within each copy. To overcome this limitation you need to store the results in a type of shared memory called NativeContainer.
- Creating Jobs:
To create a job in Unity you need to implement the IJob interface. IJob allows you to schedule a single job that runs in parallel to any other jobs that are running.
To create a job you need to,
- Create a struct that implements IJob.
- Add the member variables that the job uses.
- Create a method in your struct called Execute with the implementation of the job inside it.
// Job adding two floating-point values together
public struct MyJob : IJob
public float a;
public float b;
public NativeArray<float> result;
public void Execute()
result = a + b;
- Scheduling Jobs:
To schedule a job in the main thread, you must:
- Instantiate the job.
- Populate the job’s data.
- Call the Schedule method.
Calling Schedule puts the job into the job queue for execution at the appropriate time. Once scheduled, you cannot interrupt a job.
Note: You can only call Schedule from the main thread.
// Create a native array of a single float to store the result. This example waits for the job to complete for illustration purposes
NativeArray<float> result = new NativeArray<float>(1, Allocator.TempJob);
// Set up the job data
MyJob jobData = new MyJob();
jobData.a = 10;
jobData.b = 10;
jobData.result = result;
// Schedule the job
JobHandle handle = jobData.Schedule();
// Wait for the job to complete
// All copies of the NativeArray point to the same memory, you can access the result in "your" copy of the NativeArray
float aPlusB = result;
// Free the memory allocated by the result array
Full Documentation about C# Job System: https://docs.unity3d.com/2019.3/Documentation/Manual/JobSystem.html
2) Burst Compiler:
- Burst is a new math-aware compiler developed by Unity. It can produce a highly optimized machine code that takes full advantage of the platforms you’re compiling for.
- it translates from IL/.NET bytecode to highly optimized native code using LLVM, And the best part is that it does this all automatically.
- It is released as a unity package and integrated into Unity using the Unity Package Manager.
- All you need to do is add the Unity. Burst package to your project. Then mark your C# jobs with the BurstCompile attributes.
- Once you’ve done that, Unity will take care of the rest, compiling all of your Unity Job System code into highly optimized machine code.
3) ECS [Entity Component System]:
The Entity Component System (ECS) is the core of the Unity Data-Oriented Tech Stack(DOTS). As the name indicates, ECS has three principal parts:
Entities — the entities, or things, that populate your game or program
Components — the data associated with your entities, but organized by the data itself rather than by entity. (This difference in organization is one of the key differences between an object-oriented and a data-oriented design.)
Systems — the logic or set of instructions that transforms the component data from its current state to its next state— for example, a system might update the positions of all moving entities by their velocity times the time interval since the previous frame.
There are two types of ECS:
i) PURE ECS:
- No game objects
- No Monobehaviours
- Entities are the “new” game objects, data are stored in Components and logic is held within System.
- Utilize the new C# Job System.
ii) HYBRID ECS:
- All the features of Pure ECS.
- Also includes Helper Classes + Native Unity Classes of OLD systems like Gameobjects as Entities and Monobehaviours as Components.
Example of Pure ECS and Hybrid ECS:
Download Sample Project of DOTS:
Studios Using DOTS:
- Tic Toc Games
- Far North Entertainment
- Moonlight Mouse