I never did something with threading before, but I try to create an endless terrain with the Unity Job System.
I use JobHandle.ScheduleBatchedJobs();
, but sometimes the job gets calculated on the main thread. Is there any way to make it run in the background, so the game doesn’t lag?
Yes, the Job is very hard so process and need some optimizations. It gives out the vertices and triangles of a 3DPerlinNoise with a Marching Cubes algorithm. The job is called around 41-170 times at once. It makes sense to use the main thread, but is there any option to queue all the jobs, so the main thread doesnt need to be included in the process?
In this example, the job gets called 41 times. I use an IEnumerator to check if every single job is finished. Maybe thats also slowing everything down?
Many calculations take around 200 frames in the workers thread. How do I spread it over multiple frames?
Iam sorry, I am very a noob in this topic.
AFAIK job can be scheduled to run on the main thread, but only if you called Job.Complete() before it was actually completed (that way, it simply utilizes all available threads, since the main thread is blocked by Complete() anyway). Try to first check if it’s actually done (by using IsCompleted), and wait for the next frame if it’s not. It’s perfectly OK to have the job running during multiple frames.