DMeville

Untitled

Jun 23rd, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. // JOBS. HOW DO THEY WORK??
  2. //
  3. //1) We need a struct that extends IJob...
  4. //implementing Execute, that does not use any unity methods inside, just does "stuff" on data like math!
  5. //2) In some main thread method, create the job with new, set the data it needs and then
  6. //call job.Schedule
  7. //job.Schedule will send the job to a main thread to be executed at it's own pace.
  8. //Then in late update, (or somewhere after). Call job.Complete() and copy the data we computed on back into unity classes or whatever we want to do with it.
  9. //Complete FORCES the job to complete, by dedicating MAIN THREAD time to it until it is done. This is blocking, but useful if you are scheduling a job every frame
  10. //If you have a job that you don't want to block, and can be finished "whenever it's had enough time to finish" (all on the non-main thread). You can listen for job.IsComplete in Update or something, and it will be set to true whenever the job has finished. At this point you need to call Complete() to finalize it.
  11. /You can ONLY access the (processed) data on your job once you've called Complete(), so you HAVE to call it at some point before accessing your data
  12.  
  13.  
  14.  
  15. public class JobTest:MonoBehaviour {
  16.  
  17. public List<TestJob> j = new List<TestJob>();
  18. public List<JobHandle> jh = new List<JobHandle>();
  19. public System.Diagnostics.Stopwatch s;
  20.  
  21.  
  22. void Update() {
  23. TestJob tj = new TestJob() {
  24. someDataWeNeed = 10;
  25. };
  26.  
  27. j.Add(tj);
  28. //batch size can't be too big or too small, too small = slower, too many iterations to do. too big = slower, too many executes to do per batch
  29. jh.Add(tj.Schedule());
  30.  
  31.  
  32.  
  33. }
  34. public void LateUpdate() {
  35. for(int i = 0; i < jh.Count; i++) {
  36. jh[i].Complete();
  37. }
  38.  
  39.  
  40. }
  41.  
  42. [BurstCompile]
  43. public struct TestJob:IJob{
  44.  
  45. public int someDataWeNeed;
  46.  
  47. public void Execute() {
  48. someDataWeNeed= someDataWeNeed++;
  49.  
  50. }
  51. }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment