Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. using Unity.Collections;
  2. using UnityEngine;
  3. using Unity.Jobs;
  4. using Unity.Burst;
  5.  
  6. public class LongJob : MonoBehaviour
  7. {
  8. public long totalSteps = 1;
  9. public long intermediateSteps = 1;
  10.  
  11. private LongLongJob theJob;
  12.  
  13. private NativeArray<long> results;
  14. private JobHandle longHandle;
  15. private int numJobs = 0;
  16. private long currentStep = 0;
  17. private int jobIndex = 0;
  18.  
  19. [BurstCompile]
  20. struct LongLongJob : IJob
  21. {
  22. public long input;
  23. public NativeArray<long> output;
  24. public long steps;
  25. public int resultIndex;
  26.  
  27. public void Execute()
  28. {
  29. for (long i = 0; i < steps; i++)
  30. {
  31. output[resultIndex]++;
  32. }
  33.  
  34. output[resultIndex] += input;
  35. }
  36. }
  37.  
  38. private void scheduleJob(long inputValue, long steps, int index)
  39. {
  40. theJob = new LongLongJob()
  41. {
  42. steps = steps,
  43. input = inputValue,
  44. output = results,
  45. resultIndex = index
  46. };
  47.  
  48. longHandle = theJob.Schedule(longHandle);
  49. }
  50. void Start()
  51. {
  52. numJobs = (int)((totalSteps + intermediateSteps - 1) / intermediateSteps);
  53. results = new NativeArray<long>(numJobs, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  54. }
  55.  
  56. private bool done = false;
  57. void Update()
  58. {
  59.  
  60. if (currentStep < totalSteps && longHandle.IsCompleted)
  61. {
  62. longHandle.Complete();
  63. long previousResults = jobIndex > 0 ? results[jobIndex - 1] : 0;
  64. Debug.Log("Previous intermediate result: " + jobIndex + " = " + previousResults);
  65. long nextSteps = totalSteps - currentStep > intermediateSteps
  66. ? intermediateSteps
  67. : totalSteps - currentStep;
  68. scheduleJob(previousResults, nextSteps, jobIndex++);
  69. currentStep += nextSteps;
  70. } else if(currentStep >= totalSteps && longHandle.IsCompleted && !done) {
  71. longHandle.Complete();
  72. Debug.Log("Result: " + results[results.Length - 1]);
  73. done = true;
  74. }
  75.  
  76. }
  77.  
  78. private void OnDestroy()
  79. {
  80. longHandle.Complete();
  81. results.Dispose();
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement