Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using Unity.Collections;
- using UnityEngine;
- using UnityEngine.Assertions;
- using UnityEngine.Experimental.AI;
- using Unity.Jobs;
- public class NavMeshTestAsync : MonoBehaviour
- {
- [SerializeField] Transform start, end;
- const int extents = 10;
- const int maxPath = 64;
- private NavMeshQuery query;
- private NativeArray<PathQueryStatus> status;
- private NativeArray<PolygonId> path;
- private NativeArray<NavMeshLocation> pathStraigth;
- private NativeArray<StraightPathFlags> pathStreaigthFlag;
- private NativeArray<float> vertexSize;
- private JobHandle handle;
- [SerializeField]
- private int currentState = 0;
- public int CurrentState { get => currentState; set => currentState = value; }
- private void OnEnable()
- {
- status = new NativeArray<PathQueryStatus>(1, Allocator.Persistent);
- query = new NavMeshQuery(NavMeshWorld.GetDefaultWorld(), Allocator.Persistent, maxPath);
- pathStraigth = new NativeArray<NavMeshLocation>(maxPath, Allocator.Persistent);
- pathStreaigthFlag = new NativeArray<StraightPathFlags>(maxPath, Allocator.Persistent);
- vertexSize = new NativeArray<float>(maxPath, Allocator.Persistent);
- }
- private void OnDisable()
- {
- handle.Complete();
- pathStraigth.Dispose();
- pathStreaigthFlag.Dispose();
- vertexSize.Dispose();
- status.Dispose();
- query.Dispose();
- }
- void Update()
- {
- switch (CurrentState)
- {
- case 0:
- BeginPathFind();
- break;
- case 1:
- UpdatPathFind();
- break;
- case 2:
- ShowResult();
- break;
- }
- }
- void BeginPathFind()
- {
- handle = new BeginFindPathJob(query, status, start.position, end.position).Schedule();
- handle = new CheckStateJob(status).Schedule(handle);
- JobHandle.ScheduleBatchedJobs();
- CurrentState = 1;
- }
- void UpdatPathFind()
- {
- handle.Complete();
- if( status[0] != PathQueryStatus.Success)
- {
- handle = new UpdateFindPathJob(query, status).Schedule(handle);
- handle = new CheckStateJob(status).Schedule(handle);
- JobHandle.ScheduleBatchedJobs();
- }
- else
- {
- path = new NativeArray<PolygonId>(maxPath, Allocator.TempJob);
- handle = new GetResultJob(query, status, start.position, end.position, path, pathStraigth, pathStreaigthFlag, vertexSize).Schedule(handle);
- handle = new CheckStateJob(status).Schedule(handle);
- JobHandle.ScheduleBatchedJobs();
- CurrentState = 2;
- }
- }
- void ShowResult()
- {
- handle.Complete();
- for (int i = 0; i < maxPath - 1 && pathStreaigthFlag[i] != StraightPathFlags.End; i++)
- Debug.DrawLine(pathStraigth[i].position, pathStraigth[i + 1].position, Color.red);
- }
- [Unity.Burst.BurstCompile]
- private struct BeginFindPathJob : IJob
- {
- NavMeshQuery query;
- NativeArray<PathQueryStatus> status;
- Vector3 startPos, endPos;
- public BeginFindPathJob(NavMeshQuery query, NativeArray<PathQueryStatus> status, Vector3 startPos, Vector3 endPos)
- {
- this.query = query;
- this.status = status;
- this.startPos = startPos;
- this.endPos = endPos;
- }
- public void Execute()
- {
- NavMeshLocation startLocation = query.MapLocation(startPos, Vector3.up * extents, 0);
- NavMeshLocation endLocation = query.MapLocation(endPos, Vector3.up * extents, 0);
- status[0] = query.BeginFindPath(startLocation, endLocation);
- }
- }
- [Unity.Burst.BurstCompile]
- private struct UpdateFindPathJob : IJob
- {
- NavMeshQuery query;
- NativeArray<PathQueryStatus> status;
- public UpdateFindPathJob(NavMeshQuery query, NativeArray<PathQueryStatus> status)
- {
- this.query = query;
- this.status = status;
- }
- public void Execute()
- {
- status[0] = query.UpdateFindPath(32, out int iterationsPerformed);
- }
- }
- private struct CheckStateJob : IJob
- {
- public NativeArray<PathQueryStatus> status;
- public CheckStateJob(NativeArray<PathQueryStatus> status)
- {
- this.status = status;
- }
- public void Execute()
- {
- Debug.Log(status[0]);
- }
- }
- [Unity.Burst.BurstCompile]
- private struct GetResultJob : IJob
- {
- NavMeshQuery query;
- NativeArray<PathQueryStatus> status;
- Vector3 start, end;
- [DeallocateOnJobCompletion]
- NativeArray<PolygonId> path;
- NativeArray<NavMeshLocation> pathStraigth;
- NativeArray<StraightPathFlags> pathStreaigthFlag;
- NativeArray<float> vertexSize;
- public GetResultJob(NavMeshQuery query, NativeArray<PathQueryStatus> status, Vector3 start, Vector3 end, NativeArray<PolygonId> path, NativeArray<NavMeshLocation> pathStraigth, NativeArray<StraightPathFlags> pathStreaigthFlag, NativeArray<float> vertexSize)
- {
- this.query = query;
- this.status = status;
- this.start = start;
- this.end = end;
- this.path = path;
- this.pathStraigth = pathStraigth;
- this.pathStreaigthFlag = pathStreaigthFlag;
- this.vertexSize = vertexSize;
- }
- public void Execute()
- {
- query.EndFindPath(out int pathsize);
- var pathSize = query.GetPathResult(path);
- int streaigthPathCount = 0;
- status[0] = PathUtils.FindStraightPath(query, start, end, path, pathSize, ref pathStraigth, ref pathStreaigthFlag, ref vertexSize, ref streaigthPathCount, maxPath);
- }
- }
- }
Add Comment
Please, Sign In to add comment