Instantly share code, notes, and snippets.
Created
November 13, 2018 00:40
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save tsubaki/6b0c9f4a8762b2eea09dc336b1e7a247 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing. Is it worth?