123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using UnityEngine;
- namespace Pathfinding {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [AddComponentMenu("Pathfinding/Navmesh/RecastTileUpdateHandler")]
- [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_recast_tile_update_handler.php")]
- public class RecastTileUpdateHandler : MonoBehaviour {
-
- RecastGraph graph;
-
- bool[] dirtyTiles;
-
- bool anyDirtyTiles = false;
-
- float earliestDirty = float.NegativeInfinity;
-
- public float maxThrottlingDelay = 0.5f;
- public void SetGraph (RecastGraph graph) {
- this.graph = graph;
- if (graph == null)
- return;
- dirtyTiles = new bool[graph.tileXCount*graph.tileZCount];
- anyDirtyTiles = false;
- }
-
- public void ScheduleUpdate (Bounds bounds) {
- if (graph == null) {
-
- if (AstarPath.active != null) {
- SetGraph(AstarPath.active.data.recastGraph);
- }
- if (graph == null) {
- Debug.LogError("Received tile update request (from RecastTileUpdate), but no RecastGraph could be found to handle it");
- return;
- }
- }
-
-
-
- int voxelCharacterRadius = Mathf.CeilToInt(graph.characterRadius/graph.cellSize);
- int borderSize = voxelCharacterRadius + 3;
-
- bounds.Expand(new Vector3(borderSize, 0, borderSize)*graph.cellSize*2);
- var touching = graph.GetTouchingTiles(bounds);
- if (touching.Width * touching.Height > 0) {
- if (!anyDirtyTiles) {
- earliestDirty = Time.time;
- anyDirtyTiles = true;
- }
- for (int z = touching.ymin; z <= touching.ymax; z++) {
- for (int x = touching.xmin; x <= touching.xmax; x++) {
- dirtyTiles[z*graph.tileXCount + x] = true;
- }
- }
- }
- }
- void OnEnable () {
- RecastTileUpdate.OnNeedUpdates += ScheduleUpdate;
- }
- void OnDisable () {
- RecastTileUpdate.OnNeedUpdates -= ScheduleUpdate;
- }
- void Update () {
- if (anyDirtyTiles && Time.time - earliestDirty >= maxThrottlingDelay && graph != null) {
- UpdateDirtyTiles();
- }
- }
-
- public void UpdateDirtyTiles () {
- if (graph == null) {
- new System.InvalidOperationException("No graph is set on this object");
- }
- if (graph.tileXCount * graph.tileZCount != dirtyTiles.Length) {
- Debug.LogError("Graph has changed dimensions. Clearing queued graph updates and resetting.");
- SetGraph(graph);
- return;
- }
- for (int z = 0; z < graph.tileZCount; z++) {
- for (int x = 0; x < graph.tileXCount; x++) {
- if (dirtyTiles[z*graph.tileXCount + x]) {
- dirtyTiles[z*graph.tileXCount + x] = false;
- var bounds = graph.GetTileBounds(x, z);
-
-
- bounds.extents *= 0.5f;
- var guo = new GraphUpdateObject(bounds);
- guo.nnConstraint.graphMask = 1 << (int)graph.graphIndex;
- AstarPath.active.UpdateGraphs(guo);
- }
- }
- }
- anyDirtyTiles = false;
- }
- }
- }
|