Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. public class TestJob : MonoBehaviour {
  2. private NativeArray<Vector2> inputArray;
  3. private NativeArray<Vector2> outputArray;
  4. private JobHandle handle;
  5.  
  6. void OnEnable()
  7. {
  8. this.inputArray = new NativeArray<Vector2>(1, Allocator.Persistent);
  9. this.outputArray = new NativeArray<Vector2>(1, Allocator.Persistent);
  10. CreateJob();
  11. }
  12.  
  13. void OnDisable()
  14. {
  15. this.inputArray.Dispose();
  16. this.outputArray.Dispose();
  17. }
  18.  
  19. void CreateJob()
  20. {
  21. SimpleJob job = new SimpleJob();
  22. job.delta = Time.deltaTime;
  23.  
  24. Vector2 position = this.transform.position;
  25. job.position = position;
  26.  
  27. Vector2 newPosition = position + Vector2.right;
  28. this.inputArray[0] = newPosition;
  29.  
  30. job.inputArray = this.inputArray;
  31. job.outputArray = this.outputArray;
  32.  
  33. this.handle = job.Schedule();
  34. this.handle.Complete();
  35. }
  36.  
  37. void Update()
  38. {
  39. if (this.handle.IsCompleted)
  40. {
  41. Vector2 newPosition = this.outputArray[0];
  42. this.transform.position = newPosition;
  43.  
  44. CreateJob();
  45. }
  46. }
  47. }
  48.  
  49.  
  50. public struct SimpleJob : IJob
  51. {
  52.  
  53. [ReadOnly]
  54. public Unity.Collections.NativeArray<Vector2> inputArray;
  55. [WriteOnly]
  56. public NativeArray<Vector2> outputArray;
  57.  
  58. public Vector2 position;
  59. public float delta;
  60.  
  61. public void Execute()
  62. {
  63. Vector2 newPosition = this.inputArray[0];
  64. newPosition = Vector2.Lerp(this.position, newPosition, this.delta);
  65. this.outputArray[0] = newPosition;
  66. }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement