Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active February 2, 2019 17:55
Show Gist options
  • Save tsubaki/7009f3cb57e7d2d12d6a863464962773 to your computer and use it in GitHub Desktop.
Save tsubaki/7009f3cb57e7d2d12d6a863464962773 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.Experimental.AI;
public class NavMeshTest : MonoBehaviour
{
[SerializeField] Transform start, end;
const int extents = 10;
const int maxPath = 32;
IEnumerator Start()
{
NavMeshQuery query = new NavMeshQuery(NavMeshWorld.GetDefaultWorld(), Allocator.Persistent, maxPath);
// ロケーション(NavMesh表面上の座標)の準備
NavMeshLocation startLocation = query.MapLocation(start.position, Vector3.up * extents, 0);
NavMeshLocation endLocation = query.MapLocation(end.position, Vector3.up * extents, 0);
// パス検索の開始
PathQueryStatus status;
status = query.BeginFindPath(startLocation, endLocation);
Assert.AreEqual(PathQueryStatus.InProgress, status);
// パス検索中
yield return new WaitWhile(() => {
status = query.UpdateFindPath(8, out int iterationsPerformed);
Debug.Log(status);
return status == PathQueryStatus.InProgress;
});
Assert.AreEqual(PathQueryStatus.Success, status);
// パス検索の終了、
status = query.EndFindPath(out int pathsize);
Assert.AreEqual(PathQueryStatus.Success, status);
// パスの結果を受け取る
NativeArray<PolygonId> path = new NativeArray<PolygonId>(pathsize, Allocator.Temp);
int pathSize = query.GetPathResult(path);
NativeArray<NavMeshLocation> pathStraigth = new NativeArray<NavMeshLocation>(maxPath, Allocator.Temp);
NativeArray<StraightPathFlags> pathStreaigthFlag = new NativeArray<StraightPathFlags>(maxPath, Allocator.Temp);
NativeArray<float> vertexSize = new NativeArray<float>(maxPath, Allocator.Temp);
int streaigthPathCount = 0;
status = PathUtils.FindStraightPath(query, start.position, end.position, path, pathSize, ref pathStraigth, ref pathStreaigthFlag, ref vertexSize, ref streaigthPathCount, maxPath);
Assert.AreEqual(PathQueryStatus.Success, status);
// 描画
for (int i = 0; i < streaigthPathCount - 1; i++)
Debug.DrawLine(pathStraigth[i].position, pathStraigth[i + 1].position, Color.red, 32);
// 後片付け
pathStraigth.Dispose();
pathStreaigthFlag.Dispose();
vertexSize.Dispose();
path.Dispose();
query.Dispose();
}
}
@tsubaki
Copy link
Author

tsubaki commented Nov 16, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment