Skip to content

Instantly share code, notes, and snippets.

@Protonz
Last active August 24, 2017 21:31
Show Gist options
  • Save Protonz/22753bc773f691c9d409b90b246298df to your computer and use it in GitHub Desktop.
Save Protonz/22753bc773f691c9d409b90b246298df to your computer and use it in GitHub Desktop.
Create and Destroy NavGraphs at Runtime
using Pathfinding;
using System.Collections;
using UnityEngine;
public class LayerGridGraphToggle : MonoBehaviour {
public int Width = 200; // (nodes)
public int Depth = 200; // (nodes)
public float NodeSize = 0.5f;
public Vector3 Center = Vector3.zero;
public KeyCode ToggleKeyCode = KeyCode.Alpha1;
bool ToggleIsOn = false;
// An already existing NavGraph with a Width & Depth of One that we can copy settings from
static LayerGridGraph _ReferenceLayerGridGraph;
static LayerGridGraph ReferenceLayerGridGraph {
get {
if( _ReferenceLayerGridGraph == null && AstarPath.active != null ) {
_ReferenceLayerGridGraph = AstarPath.active.data.FindGraph(navGraph => {
LayerGridGraph layerGridGraph = navGraph as LayerGridGraph;
if( layerGridGraph == null )
return false;
if( layerGridGraph.width > 1 || layerGridGraph.depth > 1 )
return false;
return true;
}) as LayerGridGraph;
// Remove the reference graph
if( _ReferenceLayerGridGraph != null ) {
AstarPath.active.data.RemoveGraph(_ReferenceLayerGridGraph);
}
}
return _ReferenceLayerGridGraph;
}
}
// The Graph for this Toggle
LayerGridGraph LayerGridGraph = null;
// Editor Gizmos
Color OnColor = new Color(0, 1, 0, 0.25f); // Green
Color OffColor = new Color(1, 0, 0, 0.25f); // Red
private void OnDrawGizmos() {
Gizmos.color = ToggleIsOn ? OnColor : OffColor;
Gizmos.DrawCube( Center, new Vector3(Width * NodeSize, 1, Depth * NodeSize) );
}
private void Start() {
ToggleOn();
}
void Update () {
if( ReferenceLayerGridGraph == null )
return;
if( Input.GetKeyDown(ToggleKeyCode) ) {
if( ToggleIsOn == false )
ToggleOn();
else
ToggleOff();
}
}
// Create the NavGraph
void ToggleOn() {
LayerGridGraph = AstarPath.active.data.AddGraph(typeof(LayerGridGraph)) as LayerGridGraph;
// Dynamic Settings
LayerGridGraph.SetDimensions(Width, Depth, NodeSize);
LayerGridGraph.center = Center;
// Reference Settings (I don't know a better way to copy them)
LayerGridGraph.neighbours = ReferenceLayerGridGraph.neighbours;
LayerGridGraph.characterHeight = ReferenceLayerGridGraph.characterHeight;
LayerGridGraph.cutCorners = ReferenceLayerGridGraph.cutCorners;
LayerGridGraph.maxClimb = ReferenceLayerGridGraph.maxClimb;
LayerGridGraph.maxSlope = ReferenceLayerGridGraph.maxSlope;
LayerGridGraph.erodeIterations = ReferenceLayerGridGraph.erodeIterations;
LayerGridGraph.collision.collisionCheck = ReferenceLayerGridGraph.collision.collisionCheck;
LayerGridGraph.collision.type = ReferenceLayerGridGraph.collision.type;
LayerGridGraph.collision.diameter = ReferenceLayerGridGraph.collision.diameter;
LayerGridGraph.collision.height = ReferenceLayerGridGraph.collision.height;
LayerGridGraph.collision.collisionOffset = ReferenceLayerGridGraph.collision.collisionOffset;
LayerGridGraph.collision.mask = ReferenceLayerGridGraph.collision.mask;
LayerGridGraph.collision.heightCheck = ReferenceLayerGridGraph.collision.heightCheck;
LayerGridGraph.collision.fromHeight = ReferenceLayerGridGraph.collision.fromHeight;
LayerGridGraph.collision.heightMask = ReferenceLayerGridGraph.collision.heightMask;
LayerGridGraph.collision.thickRaycast = ReferenceLayerGridGraph.collision.thickRaycast;
LayerGridGraph.collision.unwalkableWhenNoGround = ReferenceLayerGridGraph.collision.unwalkableWhenNoGround;
LayerGridGraph.penaltyPosition = ReferenceLayerGridGraph.penaltyPosition;
LayerGridGraph.penaltyPositionOffset = ReferenceLayerGridGraph.penaltyPositionOffset;
LayerGridGraph.penaltyPositionFactor = ReferenceLayerGridGraph.penaltyPositionFactor;
StartCoroutine(ScanAsync(LayerGridGraph));
ToggleIsOn = true;
}
IEnumerator ScanAsync( NavGraph navGraph ) {
// Only 1 NavGraph can scan at a time
while( AstarPath.active.isScanning )
yield return 0;
// Check if the NavGraph has been removed before the scanning can start
if( navGraph.active == null )
yield break;
foreach( Progress progress in AstarPath.active.ScanAsync(navGraph) )
yield return 0;
}
// Destroy the NavGraph
void ToggleOff() {
AstarPath.active.data.RemoveGraph(LayerGridGraph);
LayerGridGraph = null;
ToggleIsOn = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment