A* Pathfinding Project  3.8.8.1
The A* Pathfinding Project for Unity 3D
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Events Macros Groups Pages
NavGraph Class Referenceabstract

Base class for all graphs. More...

Detailed Description

Base class for all graphs.

Public Member Functions

virtual void Awake ()
 This will be called on the same time as Awake on the gameObject which the AstarPath script is attached to.
 
virtual int CountNodes ()
 Count nodes in the graph.
 
virtual void DeserializeExtraInfo (GraphSerializationContext ctx)
 Deserializes graph type specific node data.
 
virtual void DeserializeSettingsCompatibility (GraphSerializationContext ctx)
 An old format for serializing settings.
 
NNInfo GetNearest (Vector3 position)
 Returns the nearest node to a position using the default NNConstraint.
 
NNInfo GetNearest (Vector3 position, NNConstraint constraint)
 Returns the nearest node to a position using the specified NNConstraint.
 
virtual NNInfo GetNearest (Vector3 position, NNConstraint constraint, GraphNode hint)
 Returns the nearest node to a position using the specified NNConstraint.
 
virtual NNInfo GetNearestForce (Vector3 position, NNConstraint constraint)
 Returns the nearest node to a position using the specified constraint .
 
abstract void GetNodes (GraphNodeDelegateCancelable del)
 Calls a delegate with all nodes in the graph.
 
virtual Color NodeColor (GraphNode node, PathHandler data)
 
virtual void OnDestroy ()
 Function for cleaning up references.
 
virtual void OnDrawGizmos (bool drawNodes)
 Draw gizmos for the graph.
 
virtual void PostDeserialization ()
 Called after all deserialization has been done for all graphs.
 
virtual void RelocateNodes (Matrix4x4 oldMatrix, Matrix4x4 newMatrix)
 Relocates the nodes in this graph.
 
void Scan ()
 
void ScanGraph ()
 
void ScanInternal ()
 Internal method for scanning graphs.
 
abstract void ScanInternal (OnScanStatus statusCallback)
 Scans the graph, called from AstarPath.ScanLoop.
 
virtual void SerializeExtraInfo (GraphSerializationContext ctx)
 Serializes graph type specific node data.
 
void SetMatrix (Matrix4x4 m)
 Use to set both matrix and inverseMatrix at the same time.
 

Static Public Member Functions

static bool InSearchTree (GraphNode node, Path path)
 Returns if the node is in the search tree of the path.
 

Public Attributes

AstarPath active
 Reference to the AstarPath object in the scene.
 
bool drawGizmos = true
 
uint graphIndex
 Index of the graph, used for identification purposes.
 
Guid guid
 Used as an ID of the graph, considered to be unique.
 
bool infoScreenOpen
 Used in the editor to check if the info screen is open.
 
uint initialPenalty
 Default penalty to apply to all nodes.
 
Matrix4x4 inverseMatrix = Matrix4x4.identity
 Inverse of matrix.
 
Matrix4x4 matrix = Matrix4x4.identity
 A matrix for translating/rotating/scaling the graph.
 
string name
 Name of the graph.
 
bool open
 Is the graph open in the editor.
 

Package Functions

virtual void UnloadGizmoMeshes ()
 Called when temporary meshes used in OnDrawGizmos need to be unloaded to prevent memory leaks.
 

Member Function Documentation

virtual void Awake ( )
virtual

This will be called on the same time as Awake on the gameObject which the AstarPath script is attached to.

(remember, not in the editor) Use this for any initialization code which can't be placed in Scan

virtual int CountNodes ( )
virtual

Count nodes in the graph.

Note that this is, unless the graph type has overriden it, an O(n) operation.

Todo:
GridGraph should override this

Reimplemented in PointGraph, GridGraph, and LayerGridGraph.

virtual void DeserializeExtraInfo ( GraphSerializationContext  ctx)
virtual

Deserializes graph type specific node data.

See Also
SerializeExtraInfo

Reimplemented in RecastGraph, GridGraph, LayerGridGraph, NavMeshGraph, and PointGraph.

virtual void DeserializeSettingsCompatibility ( GraphSerializationContext  ctx)
virtual

An old format for serializing settings.

Deprecated:
This is deprecated now, but the deserialization code is kept to avoid loosing data when upgrading from older versions.

Reimplemented in RecastGraph, GridGraph, NavMeshGraph, and PointGraph.

NNInfo GetNearest ( Vector3  position)

Returns the nearest node to a position using the default NNConstraint.

Parameters
positionThe position to try to find a close node to
See Also
Pathfinding.NNConstraint.None
NNInfo GetNearest ( Vector3  position,
NNConstraint  constraint 
)

Returns the nearest node to a position using the specified NNConstraint.

Parameters
positionThe position to try to find a close node to
constraintCan for example tell the function to try to return a walkable node. If you do not get a good node back, consider calling GetNearestForce.
virtual NNInfo GetNearest ( Vector3  position,
NNConstraint  constraint,
GraphNode  hint 
)
virtual

Returns the nearest node to a position using the specified NNConstraint.

Parameters
positionThe position to try to find a close node to
hintCan be passed to enable some graph generators to find the nearest node faster.
constraintCan for example tell the function to try to return a walkable node. If you do not get a good node back, consider calling GetNearestForce.

Reimplemented in RecastGraph, LayerGridGraph, GridGraph, NavMeshGraph, and PointGraph.

virtual NNInfo GetNearestForce ( Vector3  position,
NNConstraint  constraint 
)
virtual

Returns the nearest node to a position using the specified constraint .

Returns
an NNInfo. This method will only return an empty NNInfo if there are no nodes which comply with the specified constraint.

Reimplemented in RecastGraph, LayerGridGraph, GridGraph, NavMeshGraph, and PointGraph.

abstract void GetNodes ( GraphNodeDelegateCancelable  del)
pure virtual

Calls a delegate with all nodes in the graph.

This is the primary way of "looping" through all nodes in a graph.

This function should not change anything in the graph structure.

myGraph.GetNodes ((node) => {
Debug.Log ("I found a node at position " + (Vector3)node.Position);
return true;
});

Implemented in RecastGraph, PointGraph, GridGraph, LayerGridGraph, NavMeshGraph, and QuadtreeGraph.

static bool InSearchTree ( GraphNode  node,
Path  path 
)
static

Returns if the node is in the search tree of the path.

Only guaranteed to be correct if path is the latest path calculated. Use for gizmo drawing only.

virtual Color NodeColor ( GraphNode  node,
PathHandler  data 
)
virtual
virtual void OnDestroy ( )
virtual

Function for cleaning up references.

This will be called on the same time as OnDisable on the gameObject which the AstarPath script is attached to (remember, not in the editor). Use for any cleanup code such as cleaning up static variables which otherwise might prevent resources from being collected. Use by creating a function overriding this one in a graph class, but always call base.OnDestroy () in that function. All nodes should be destroyed in this function otherwise a memory leak will arise.

Reimplemented in RecastGraph, GridGraph, NavMeshGraph, and LayerGridGraph.

virtual void OnDrawGizmos ( bool  drawNodes)
virtual

Draw gizmos for the graph.

Reimplemented in RecastGraph, GridGraph, LayerGridGraph, NavMeshGraph, and QuadtreeGraph.

virtual void PostDeserialization ( )
virtual

Called after all deserialization has been done for all graphs.

Can be used to set up more graph data which is not serialized

Reimplemented in GridGraph, LayerGridGraph, and PointGraph.

virtual void RelocateNodes ( Matrix4x4  oldMatrix,
Matrix4x4  newMatrix 
)
virtual

Relocates the nodes in this graph.

Assumes the nodes are already transformed using the "oldMatrix", then transforms them such that it will look like they have only been transformed using the "newMatrix". The "oldMatrix" is not required by all implementations of this function though (e.g the NavMesh generator).

The matrix the graph is transformed with is typically stored in the matrix field, so the typical usage for this method is

var myNewMatrix = Matrix4x4.TRS (...);
myGraph.RelocateNodes (myGraph.matrix, myNewMatrix);

So for example if you want to move all your nodes in e.g a point graph 10 units along the X axis from the initial position

var graph = AstarPath.astarData.pointGraph;
var m = Matrix4x4.TRS (new Vector3(10,0,0), Quaternion.identity, Vector3.one);
graph.RelocateNodes (graph.matrix, m);
Note
For grid graphs it is recommended to use the helper method RelocateNodes which takes parameters for center and nodeSize (and additional parameters) instead since it is both easier to use and is less likely to mess up pathfinding.
Warning
This method is lossy, so calling it many times may cause node positions to lose precision. For example if you set the scale to 0 in one call, and then to 1 in the next call, it will not be able to recover the correct positions since when the scale was 0, all nodes were scaled/moved to the same point. The same thing happens for other - less extreme - values as well, but to a lesser degree.
Version
Prior to version 3.6.1 the oldMatrix and newMatrix parameters were reversed by mistake.

Reimplemented in PointGraph, RecastGraph, and NavMeshGraph.

void Scan ( )
void ScanGraph ( )
void ScanInternal ( )

Internal method for scanning graphs.

abstract void ScanInternal ( OnScanStatus  statusCallback)
pure virtual

Scans the graph, called from AstarPath.ScanLoop.

Override this function to implement custom scanning logic The statusCallback may be optionally called to show progress info in the editor

Implemented in RecastGraph, GridGraph, NavMeshGraph, PointGraph, LayerGridGraph, and QuadtreeGraph.

virtual void SerializeExtraInfo ( GraphSerializationContext  ctx)
virtual

Serializes graph type specific node data.

This function can be overriden to serialize extra node information (or graph information for that matter) which cannot be serialized using the standard serialization. Serialize the data in any way you want and return a byte array. When loading, the exact same byte array will be passed to the DeserializeExtraInfo function.
These functions will only be called if node serialization is enabled.

Reimplemented in RecastGraph, GridGraph, LayerGridGraph, NavMeshGraph, and PointGraph.

void SetMatrix ( Matrix4x4  m)

Use to set both matrix and inverseMatrix at the same time.

virtual void UnloadGizmoMeshes ( )
packagevirtual

Called when temporary meshes used in OnDrawGizmos need to be unloaded to prevent memory leaks.

Member Data Documentation

AstarPath active

Reference to the AstarPath object in the scene.

Might not be entirely safe to use, it's better to use AstarPath.active

bool drawGizmos = true
uint graphIndex

Index of the graph, used for identification purposes.

Guid guid

Used as an ID of the graph, considered to be unique.

Note
This is Pathfinding.Util.Guid not System.Guid. A replacement for System.Guid was coded for better compatibility with iOS
bool infoScreenOpen

Used in the editor to check if the info screen is open.

Should be inside UNITY_EDITOR only #ifs but just in case anyone tries to serialize a NavGraph instance using Unity, I have left it like this as it would otherwise cause a crash when building. Version 3.0.8.1 was released because of this bug only

uint initialPenalty

Default penalty to apply to all nodes.

Matrix4x4 inverseMatrix = Matrix4x4.identity

Inverse of matrix.

Note
Do not set directly, use SetMatrix
See Also
matrix
Matrix4x4 matrix = Matrix4x4.identity

A matrix for translating/rotating/scaling the graph.

Not all graph generators sets this variable though.

Note
Do not set directly, use SetMatrix
This value is not serialized. It is expected that graphs regenerate this field after deserialization has completed.
string name

Name of the graph.

Can be set in the unity editor

bool open

Is the graph open in the editor.


The documentation for this class was generated from the following file: