I'm looking to do some possibly heavy calculation in my game. An agent in my game needs to evaluate up to 1000 objects while performing heavy calculations to select his next job.
I want to either offload this to a separate thread (and wait for it to finish) without blocking and causing stuttering on the main Unity thread, or I'd like to use coroutines to spread the heavy calculations over multiple frames.
public static Job RequestJob()
{
Job suitableJob = JobManager.FindSuitableJob(Jobs);
if (suitableJob != null)
{
DequeueJob(suitableJob as PlantCutJob);
return suitableJob;
}
return null;
}
I'd like the JobManager.FindSuitableJob
method to be spread during several frames or multithreaded. This is the method:
public static FindSuitableJob (List<Job> jobs)
{
for (int i = 0; i < jobs.Count; i++)
{
if (jobs[i].CheckRequirements())
{
return jobs[i];
}
}
return null;
}
I have tried making a couroutine, which seems like exactly what I need, but I need to call it outside of a MonoBehavior
(from just a regular class) which I'm not sure is possible without some heavy workarounds. I have no experience with coroutines or multithreading in Unity and C# so any advice is appreciated.