A* Pathfinding Project  4.1.9
The A* Pathfinding Project for Unity 3D
NavGraph Class Referenceabstract

Base class for all graphs. More...

Detailed Description

Base class for all graphs.

Public Member Functions

virtual int CountNodes ()
 Number of nodes in the graph. More...
 
NNInfoInternal GetNearest (Vector3 position)
 Returns the nearest node to a position. More...
 
NNInfoInternal GetNearest (Vector3 position, NNConstraint constraint)
 Returns the nearest node to a position using the specified NNConstraint. More...
 
virtual NNInfoInternal GetNearest (Vector3 position, NNConstraint constraint, GraphNode hint)
 Returns the nearest node to a position using the specified NNConstraint. More...
 
virtual NNInfoInternal GetNearestForce (Vector3 position, NNConstraint constraint)
 Returns the nearest node to a position using the specified constraint . More...
 
void GetNodes (System.Func< GraphNode, bool > action)
 Calls a delegate with all nodes in the graph until the delegate returns false. More...
 
abstract void GetNodes (System.Action< GraphNode > action)
 Calls a delegate with all nodes in the graph. More...
 
virtual void OnDrawGizmos (RetainedGizmos gizmos, bool drawNodes)
 Draw gizmos for the graph. More...
 
void RelocateNodes (Matrix4x4 oldMatrix, Matrix4x4 newMatrix)
 Moves nodes in this graph. More...
 
virtual void RelocateNodes (Matrix4x4 deltaMatrix)
 Moves the nodes in this graph. More...
 
void Scan ()
 Scan the graph. More...
 
void ScanGraph ()
 Scan the graph. More...
 
void SetMatrix (Matrix4x4 m)
 Use to set both matrix and inverseMatrix at the same time. More...
 

Public Attributes

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

Protected Member Functions

virtual void DeserializeExtraInfo (GraphSerializationContext ctx)
 Deserializes graph type specific node data. More...
 
virtual void DeserializeSettingsCompatibility (GraphSerializationContext ctx)
 An old format for serializing settings. More...
 
virtual void DestroyAllNodes ()
 Destroys all nodes in the graph. More...
 
void DrawUnwalkableNodes (float size)
 
virtual void OnDestroy ()
 Function for cleaning up references. More...
 
virtual void PostDeserialization (GraphSerializationContext ctx)
 Called after all deserialization has been done for all graphs. More...
 
abstract IEnumerable< ProgressScanInternal ()
 Internal method to scan the graph. More...
 
virtual void SerializeExtraInfo (GraphSerializationContext ctx)
 Serializes graph type specific node data. More...
 

Properties

bool exists [get]
 True if the graph exists, false if it has been destroyed. More...
 

Private Member Functions

void IGraphInternals. DeserializeExtraInfo (GraphSerializationContext ctx)
 
void IGraphInternals. DeserializeSettingsCompatibility (GraphSerializationContext ctx)
 
void IGraphInternals. DestroyAllNodes ()
 
void IGraphInternals. OnDestroy ()
 
void IGraphInternals. PostDeserialization (GraphSerializationContext ctx)
 
IEnumerable< Progress > IGraphInternals. ScanInternal ()
 
void IGraphInternals. SerializeExtraInfo (GraphSerializationContext ctx)
 

Member Function Documentation

◆ CountNodes()

virtual int CountNodes ( )
virtual

Number of nodes in the graph.

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

This is an O(1) operation for grid graphs and point graphs. For layered grid graphs it is an O(n) operation.

Reimplemented in GridGraph, PointGraph, and LayerGridGraph.

◆ DeserializeExtraInfo() [1/2]

virtual void DeserializeExtraInfo ( GraphSerializationContext  ctx)
protectedvirtual

Deserializes graph type specific node data.

See also
SerializeExtraInfo

Implements IGraphInternals.

Reimplemented in GridGraph, NavmeshBase, LayerGridGraph, and PointGraph.

◆ DeserializeExtraInfo() [2/2]

void IGraphInternals. DeserializeExtraInfo ( GraphSerializationContext  ctx)
private

Implements IGraphInternals.

◆ DeserializeSettingsCompatibility() [1/2]

virtual void DeserializeSettingsCompatibility ( GraphSerializationContext  ctx)
protectedvirtual

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.

Implements IGraphInternals.

Reimplemented in GridGraph, RecastGraph, PointGraph, and NavMeshGraph.

◆ DeserializeSettingsCompatibility() [2/2]

void IGraphInternals. DeserializeSettingsCompatibility ( GraphSerializationContext  ctx)
private

Implements IGraphInternals.

◆ DestroyAllNodes() [1/2]

virtual void DestroyAllNodes ( )
protectedvirtual

Destroys all nodes in the graph.

Warning
This is an internal method. Unless you have a very good reason, you should probably not call it.

Implements IGraphInternals.

Reimplemented in GridGraph.

◆ DestroyAllNodes() [2/2]

void IGraphInternals. DestroyAllNodes ( )
private

Implements IGraphInternals.

◆ DrawUnwalkableNodes()

void DrawUnwalkableNodes ( float  size)
protected

◆ GetNearest() [1/3]

NNInfoInternal GetNearest ( Vector3  position)

Returns the nearest node to a position.

Parameters
positionThe position to try to find a close node to
See also
Pathfinding.NNConstraint.None

◆ GetNearest() [2/3]

NNInfoInternal 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.

◆ GetNearest() [3/3]

virtual NNInfoInternal 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 GridGraph, LayerGridGraph, NavmeshBase, and PointGraph.

◆ GetNearestForce()

virtual NNInfoInternal 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 GridGraph, LayerGridGraph, NavmeshBase, and PointGraph.

◆ GetNodes() [1/2]

void GetNodes ( System.Func< GraphNode, bool >  action)

Calls a delegate with all nodes in the graph until the delegate returns false.

◆ GetNodes() [2/2]

abstract void GetNodes ( System.Action< GraphNode action)
pure virtual

Calls a delegate with all nodes in the graph.

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

Do not change the graph structure inside the delegate.

var gg = AstarPath.active.data.gridGraph;
gg.GetNodes(node => {
// Here is a node
Debug.Log("I found a node at position " + (Vector3)node.position);
});

If you want to store all nodes in a list you can do this

var gg = AstarPath.active.data.gridGraph;
List<GraphNode> nodes = new List<GraphNode>();
gg.GetNodes((System.Action<GraphNode>)nodes.Add);

Implemented in NavmeshBase, GridGraph, PointGraph, and LayerGridGraph.

◆ OnDestroy() [1/2]

virtual void OnDestroy ( )
protectedvirtual

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.

Implements IGraphInternals.

Reimplemented in NavmeshBase, GridGraph, and LayerGridGraph.

◆ OnDestroy() [2/2]

void IGraphInternals. OnDestroy ( )
private

Implements IGraphInternals.

◆ OnDrawGizmos()

virtual void OnDrawGizmos ( RetainedGizmos  gizmos,
bool  drawNodes 
)
virtual

Draw gizmos for the graph.

Reimplemented in GridGraph.

◆ PostDeserialization() [1/2]

virtual void PostDeserialization ( GraphSerializationContext  ctx)
protectedvirtual

Called after all deserialization has been done for all graphs.

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

Implements IGraphInternals.

Reimplemented in GridGraph, NavmeshBase, LayerGridGraph, and PointGraph.

◆ PostDeserialization() [2/2]

void IGraphInternals. PostDeserialization ( GraphSerializationContext  ctx)
private

Implements IGraphInternals.

◆ RelocateNodes() [1/2]

void RelocateNodes ( Matrix4x4  oldMatrix,
Matrix4x4  newMatrix 
)

Moves nodes in this graph.

Deprecated:
Use RelocateNodes(Matrix4x4) instead. To keep the same behavior you can call RelocateNodes(newMatrix * oldMatrix.inverse).

◆ RelocateNodes() [2/2]

virtual void RelocateNodes ( Matrix4x4  deltaMatrix)
virtual

Moves the nodes in this graph.

Multiplies all node positions by deltaMatrix.

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.data.pointGraph;
var m = Matrix4x4.TRS (new Vector3(10,0,0), Quaternion.identity, Vector3.one);
graph.RelocateNodes (m);
Note
For grid graphs, navmesh graphs and recast graphs it is recommended to use their custom overloads of the RelocateNodes method which take parameters for e.g center and nodeSize (and additional parameters) instead since they are both easier to use and are less likely to mess up pathfinding.
Warning
This method is lossy for PointGraphs, so calling it many times may cause node positions to lose precision. For example if you set the scale to 0 in one call then all nodes will be scaled/moved to the same point and you will not be able to recover their original positions. The same thing happens for other - less extreme - values as well, but to a lesser degree.

Reimplemented in PointGraph, GridGraph, and NavmeshBase.

◆ Scan()

void Scan ( )

Scan the graph.

Consider using AstarPath.Scan() instead since this function only scans this graph and if you are using multiple graphs with connections between them, then it is better to scan all graphs at once.

◆ ScanGraph()

void ScanGraph ( )

Scan the graph.

Deprecated:
Use AstarPath.Scan() instead

◆ ScanInternal() [1/2]

abstract IEnumerable<Progress> ScanInternal ( )
protectedpure virtual

Internal method to scan the graph.

Called from AstarPath.ScanAsync. Override this function to implement custom scanning logic. Progress objects can be yielded to show progress info in the editor and to split up processing over several frames when using async scanning.

Implements IGraphInternals.

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

◆ ScanInternal() [2/2]

IEnumerable<Progress> IGraphInternals. ScanInternal ( )
private

Implements IGraphInternals.

◆ SerializeExtraInfo() [1/2]

virtual void SerializeExtraInfo ( GraphSerializationContext  ctx)
protectedvirtual

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.

Implements IGraphInternals.

Reimplemented in GridGraph, NavmeshBase, LayerGridGraph, and PointGraph.

◆ SerializeExtraInfo() [2/2]

void IGraphInternals. SerializeExtraInfo ( GraphSerializationContext  ctx)
private

Implements IGraphInternals.

◆ SetMatrix()

void SetMatrix ( Matrix4x4  m)

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

Deprecated:
Use the transform field (only available on some graph types) instead

Member Data Documentation

◆ active

AstarPath active

Reference to the AstarPath object in the scene.

◆ drawGizmos

bool drawGizmos = true

Enable to draw gizmos in the Unity scene view.

In the inspector this value corresponds to the state of the 'eye' icon in the top left corner of every graph inspector.

◆ graphIndex

uint graphIndex

Index of the graph, used for identification purposes.

◆ guid

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

◆ infoScreenOpen

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

◆ initialPenalty

uint initialPenalty

Default penalty to apply to all nodes.

◆ inverseMatrix

Matrix4x4 inverseMatrix = Matrix4x4.identity

Inverse of matrix.

Deprecated:
Use the transform field (only available on some graph types) instead

◆ matrix

Matrix4x4 matrix = Matrix4x4.identity

A matrix for translating/rotating/scaling the graph.

Deprecated:
Use the transform field (only available on some graph types) instead

◆ name

string name

Name of the graph.

Can be set in the unity editor

◆ open

bool open

Is the graph open in the editor.

Property Documentation

◆ exists

bool exists
getpackage

True if the graph exists, false if it has been destroyed.


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