Guest User

Untitled

a guest
Nov 12th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.Collections;
  4. using UnityEngine;
  5. using UnityEngine.Assertions;
  6. using UnityEngine.Experimental.AI;
  7. using Unity.Jobs;
  8.  
  9. public class NavMeshTestAsync : MonoBehaviour
  10. {
  11. [SerializeField] Transform start, end;
  12.  
  13. const int extents = 10;
  14. const int maxPath = 64;
  15.  
  16. private NavMeshQuery query;
  17. private NativeArray<PathQueryStatus> status;
  18. private NativeArray<PolygonId> path;
  19. private NativeArray<NavMeshLocation> pathStraigth;
  20. private NativeArray<StraightPathFlags> pathStreaigthFlag;
  21. private NativeArray<float> vertexSize;
  22.  
  23. private JobHandle handle;
  24.  
  25. [SerializeField]
  26. private int currentState = 0;
  27.  
  28. public int CurrentState { get => currentState; set => currentState = value; }
  29.  
  30. private void OnEnable()
  31. {
  32. status = new NativeArray<PathQueryStatus>(1, Allocator.Persistent);
  33. query = new NavMeshQuery(NavMeshWorld.GetDefaultWorld(), Allocator.Persistent, maxPath);
  34.  
  35. pathStraigth = new NativeArray<NavMeshLocation>(maxPath, Allocator.Persistent);
  36. pathStreaigthFlag = new NativeArray<StraightPathFlags>(maxPath, Allocator.Persistent);
  37. vertexSize = new NativeArray<float>(maxPath, Allocator.Persistent);
  38. }
  39.  
  40. private void OnDisable()
  41. {
  42. handle.Complete();
  43. pathStraigth.Dispose();
  44. pathStreaigthFlag.Dispose();
  45. vertexSize.Dispose();
  46.  
  47. status.Dispose();
  48. query.Dispose();
  49. }
  50.  
  51. void Update()
  52. {
  53. switch (CurrentState)
  54. {
  55. case 0:
  56. BeginPathFind();
  57. break;
  58. case 1:
  59. UpdatPathFind();
  60. break;
  61. case 2:
  62. ShowResult();
  63. break;
  64. }
  65. }
  66. void BeginPathFind()
  67. {
  68. handle = new BeginFindPathJob(query, status, start.position, end.position).Schedule();
  69. handle = new CheckStateJob(status).Schedule(handle);
  70. JobHandle.ScheduleBatchedJobs();
  71. CurrentState = 1;
  72. }
  73.  
  74. void UpdatPathFind()
  75. {
  76. handle.Complete();
  77. if( status[0] != PathQueryStatus.Success)
  78. {
  79. handle = new UpdateFindPathJob(query, status).Schedule(handle);
  80. handle = new CheckStateJob(status).Schedule(handle);
  81. JobHandle.ScheduleBatchedJobs();
  82. }
  83. else
  84. {
  85. path = new NativeArray<PolygonId>(maxPath, Allocator.TempJob);
  86. handle = new GetResultJob(query, status, start.position, end.position, path, pathStraigth, pathStreaigthFlag, vertexSize).Schedule(handle);
  87. handle = new CheckStateJob(status).Schedule(handle);
  88. JobHandle.ScheduleBatchedJobs();
  89. CurrentState = 2;
  90. }
  91. }
  92.  
  93. void ShowResult()
  94. {
  95. handle.Complete();
  96.  
  97. for (int i = 0; i < maxPath - 1 && pathStreaigthFlag[i] != StraightPathFlags.End; i++)
  98. Debug.DrawLine(pathStraigth[i].position, pathStraigth[i + 1].position, Color.red);
  99. }
  100. [Unity.Burst.BurstCompile]
  101. private struct BeginFindPathJob : IJob
  102. {
  103. NavMeshQuery query;
  104. NativeArray<PathQueryStatus> status;
  105. Vector3 startPos, endPos;
  106.  
  107. public BeginFindPathJob(NavMeshQuery query, NativeArray<PathQueryStatus> status, Vector3 startPos, Vector3 endPos)
  108. {
  109. this.query = query;
  110. this.status = status;
  111. this.startPos = startPos;
  112. this.endPos = endPos;
  113. }
  114.  
  115. public void Execute()
  116. {
  117. NavMeshLocation startLocation = query.MapLocation(startPos, Vector3.up * extents, 0);
  118. NavMeshLocation endLocation = query.MapLocation(endPos, Vector3.up * extents, 0);
  119.  
  120. status[0] = query.BeginFindPath(startLocation, endLocation);
  121. }
  122. }
  123.  
  124. [Unity.Burst.BurstCompile]
  125. private struct UpdateFindPathJob : IJob
  126. {
  127. NavMeshQuery query;
  128. NativeArray<PathQueryStatus> status;
  129.  
  130. public UpdateFindPathJob(NavMeshQuery query, NativeArray<PathQueryStatus> status)
  131. {
  132. this.query = query;
  133. this.status = status;
  134. }
  135.  
  136. public void Execute()
  137. {
  138. status[0] = query.UpdateFindPath(32, out int iterationsPerformed);
  139. }
  140. }
  141.  
  142. private struct CheckStateJob : IJob
  143. {
  144. public NativeArray<PathQueryStatus> status;
  145.  
  146. public CheckStateJob(NativeArray<PathQueryStatus> status)
  147. {
  148. this.status = status;
  149. }
  150.  
  151. public void Execute()
  152. {
  153. Debug.Log(status[0]);
  154. }
  155. }
  156.  
  157. [Unity.Burst.BurstCompile]
  158. private struct GetResultJob : IJob
  159. {
  160. NavMeshQuery query;
  161. NativeArray<PathQueryStatus> status;
  162.  
  163. Vector3 start, end;
  164. [DeallocateOnJobCompletion]
  165. NativeArray<PolygonId> path;
  166.  
  167. NativeArray<NavMeshLocation> pathStraigth;
  168. NativeArray<StraightPathFlags> pathStreaigthFlag;
  169. NativeArray<float> vertexSize;
  170.  
  171. public GetResultJob(NavMeshQuery query, NativeArray<PathQueryStatus> status, Vector3 start, Vector3 end, NativeArray<PolygonId> path, NativeArray<NavMeshLocation> pathStraigth, NativeArray<StraightPathFlags> pathStreaigthFlag, NativeArray<float> vertexSize)
  172. {
  173. this.query = query;
  174. this.status = status;
  175. this.start = start;
  176. this.end = end;
  177. this.path = path;
  178. this.pathStraigth = pathStraigth;
  179. this.pathStreaigthFlag = pathStreaigthFlag;
  180. this.vertexSize = vertexSize;
  181. }
  182.  
  183. public void Execute()
  184. {
  185. query.EndFindPath(out int pathsize);
  186. var pathSize = query.GetPathResult(path);
  187.  
  188. int streaigthPathCount = 0;
  189.  
  190. status[0] = PathUtils.FindStraightPath(query, start, end, path, pathSize, ref pathStraigth, ref pathStreaigthFlag, ref vertexSize, ref streaigthPathCount, maxPath);
  191.  
  192. }
  193. }
  194. }
Add Comment
Please, Sign In to add comment