First Name
Last Name
Confirm Password
Please validate your account by clicking the link in your email
Resend Validation Email

How to get Output from the Unity Job System


Learn how we can do some Work and get the Output from the Unity Job System.

Download Project Files
Post Comment


Code Monkey
Remember that Jobs don't need to be used solely with ECS. You can benefit from any sort of intense work by using multi-threaded code.
For example you could have your normal game built with GameObjects and use Jobs to calculate Pathfinding or an RTS finding the nearest Target which is something I will cover in the next video.
Christos Rymenidis
It's that easy!
Code Monkey
🤖Join the Community Discord https://discord.gg/eHjUVrm
Hakan Karaduman
Hi Code Monkey, thanks a lot for this playlist. I hope we get more of it. A request if you have time and if it interests you, can you make a walk through of the fps sample, focusing on the multiplayer part, so we can more easily learn how to work with ecs and multiplay?
As a C# dev I decided I need to check that limitation on the structs not returning the value directly, and after few tricks I managed to (more or less) get it to "directly" return the result. It required allowing /unsafe compilation flag, unsafe{} clause and keeping an int* (pointer to int) in the struct (or, if one prefers, a pointer to the reference type instance), with a [NativeDisableUnsafePtrRestriction] attribute on it. So while I managed to do it, I don't think I'll actually use that approach ;)
Code Monkey
Interesting, I haven't tried messing around with unsafe code much.
@Code Monkey as far as my brief dive into the ECS libraries showed me, this is exactly what they are doing. Which makes me think I defnitely shouldn't :D
The only way to give enough likes to your videos would be with a job system :)
I really like it! Thanks!
Laurentiu Ganta
Am I right to think this is a bit of a workaround approach (I assume a better approach would be using a "single value kind of container" as opposed to a collection containing one value - for single value cases)? And if this is so, it's because as of the date of this video's upload there is no direct approach to deal with a single value, but only through the Native Collections?


Good content btw, thanks for all the DOTS videos!
Code Monkey
It looks like a workaround but it is the correct way to do it. Native Containers are the only way to have memory shared between jobs and the main thread even if all you need is a single value.

You can of course make your own native container that just holds a single value to make it easier to use but in the end it needs to use a Native Collection.
You are killing it with these ECS videos! Keep it up!

Personally i'd love to see one about getting a collision result from Unity Physics, just some way to call a bit of code when 2 things collide in a ECS framework, like OnCollisionEnter or OnTriggerEnter in standard Unity, but with the new 'Unity Physics'.
Code Monkey
Yeah ECS Physics is something I definitely want to cover but haven't had the time to research yet.
Currently working on converting my animation system into ECS so I can make a nice mini game with thousands of units.
Brave Squire Games
Nice video! It's always good to be aware of the implications of value types.
I was thinking:

Could we also box / unbox the result?
object result = (object)0;
Debug Log((int)simpleJob.result);
struct simpleJob{
public object result;

Or use a nullable?
int? result = 0;
Debug Log(simpleJob.result.Value);
struct simpleJob{
public int? result;
Code Monkey
No because the job system doesnt let you use reference types. So no object, no classes, no nullables.
The only way to get a reference to shared memory is really with the Native Containers.
Dom O
Great video but I'd like to use the job system to generate my next "chunk" of map data for a large map. However how would I be able to return my specific map data as it's a collection of map objects so can't use a native array - is this possible?
Code Monkey
You would need to make your map generation with a data oriented design so working with structs instead of class objects.
Receive a NativeArray<MapChunkStruct> then your job does whatever it needs to generate the data used in your maps and then read it back after the job is complete.
Dom O
@Code Monkey Sounds good, I will give it a go. Thanks for the help.
YouTube user
Thank for this awesome video
I Loved to apply it into my game.👍👍👍
The Skull
Thanks for the great video! I'm having a hard time understanding why passing a and b into the job results in result of 0 and how its related to references. It doesn't make sense to me. Are values of a and b correctly initialized to values you set while initializing a new job, but the result is not correctly returned?
Code Monkey
When you create the instance of the job you pass in the starting values.
Then the Job system receives a Copy of that instance and does work on that copy.
So the original instance you create doesn't change, only the copy that is held by the job system.
The NativeArray works as shared memory between the various copies, so when the copy changes the NativeArray the original changes as well.
A couple of questions:
1) if we scheduling/completing Jobs in Update(), should we be reusing native collections rather than creating/disposing them in every call to Update()?

2) If we should be reusing, what kind of allocator should be used?
Code Monkey
I'm not 100% sure what are the best practices so far but if your array is the exact same size then yes making it with Allocator.Persistent is probably the best approach.
However if the array size changes a lot like when destroying and creating entities it might be better to always use TempJob.
But again I haven't done a lot of testing on all the Allocators so test it in your specific scenario.
Satwant Singh
Brilliant (as always), really looking forward to the pathfinding or nearest target and the next tutorial. Great work, thank you :)
konstantin lozev
I was also quite surprised that I had to use NativeArray with dimension of 1, but thankfully the Unity documentation has such an example. What I am struggling with is NativeArray<Vector3>. It works with NativeArray<float3>, but I always get 0 from NativeArray<Vector3>. Have you noticed a similar issue? Another detaul about NativeArrays is that they are only in one dimension, so if you want to have a NativeArray2D, you would have to implement your own.
Code Monkey
I haven't used a NativeArray<Vector3> since I'm using float3 for everything but what issue are you getting? Converting a float3 to Vector3 is giving you 0? The float3 should implicitly convert into a Vector3, haven't had any issues where conversion didn't work.

Yeah for 2 dimensional arrays it appears the best approach is a flat array and do some math on the index. A bit annoying but doable.
konstantin lozev
@Code Monkey I want to use NativeArray<Vector3> in the job and then flush it directly to Vector3[] with .ToArray(). The problem is that within the job the NativeArray<Vector3> does contain the results, but when I try to flush it to Vector3[] with .ToArray(), I only get zeros. If I use NativeArray<float3>, I would have to have a for loop to fill each index in Vector3[], which I believe is much slower than .ToArray().
Mad Max
Hey guys, I created my first game, could you tell me your opinion please, thanks.

Anton Golubev
Can I use class instead of struct? Class is a reference type, wouldn't it solve this problem?
konstantin lozev
No, the job system works with structs only.
Code Monkey
Jobs require structs, it's the main way they enable multi-threading while making it safe. So yea the only way is really with Native Containers.
Alejandro Castan
I really appreciate your effort for sharing your tutorials with us. It is very good to know that Job System can be used without ECS for another game purposes. I am working with Virtual Reality projects and I am looking for a way to optimize my assets to improve the graphic quality, for it which way you would use to do this? .Would you use DOTS or ECS, Job System and Burst Compiler in individual way?. Please let me know your opinion about this topic. One more time, thanks for your tutorials !!!
Code Monkey
For VR you would definitely benefit from increased performance so Jobs with or without ECS will be a huge help.
How you use it depends very much on the specifics of your game, open up the profiler and see where you are spending the most amount of time then jobify that code or if the issue is more related to rendering make sure you have as few draw calls and as many bathes as possible.
Alejandro Castan
@Code Monkey Thanks for your advice !!!
konstantin lozev
The great thing about jobs and VR is that you can offload to a job any heavy computation amd your main thread remains unaffected by hiw long the job runs. Simply put: there is no freeze of the game to wait for the computation so your frame time is not affected.
Alejandro Castan
@konstantin lozev Hi. Thanks for your advice !!!
Well done . Always good videos