Editing graph connections manually
How to make edits to the graph structure.
Sometimes the graph is almost right, but there are maybe some cases where you need it fixed up a bit. This is most common with the point graph and this page is mostly intended for that graph type. The methods do work for other graph types as well to some extent however.
The NodeLink component
If you have a point graph where you want to either add a connection, remove an existing connection or perhaps change the cost of some connection then you can use the NodeLink component. Point graphs are generally created from a collection of GameObjects. You can add the NodeLink component to one node and then set the target to another node. By changing the options the component can then either add a new connection between those two nodes, remove an already existing connection (if one exists) or change the cost of the connection between them. These new connections are identical to the ones created by the point graph originally.
To make it easier to modify the graph there are some keyboard shortcuts. You can find the menu items in Unity under menubar -> Edit -> Pathfinding.
alt+ctrl+L (alt+cmd+L on macOS): Link two nodes. Connect the nodes if there is no NodeLink connection between them if they are already connected then explicitly delete their connection.
alt+ctrl+U (alt+cmd+U on macOS): Unlink two nodes. This removes the NodeLink components that link the two nodes, regardless of if the NodeLink is configured to add or delete a connection.
alt+ctrl+B (alt+cmd+B on macOS): Destroy all NodeLink components on the selected GameObjects.
Editing graph connections using code
You can also modify graph connections using code.
AstarPath.active.AddWorkItem(new AstarWorkItem(ctx => {
// Connect two nodes
var node1 = AstarPath.active.GetNearest(transform.position, NNConstraint.None).node;
var node2 = AstarPath.active.GetNearest(transform.position + Vector3.right, NNConstraint.None).node;
var cost = (uint)(node2.position - node1.position).costMagnitude;
node1.AddConnection(node2, cost);
node2.AddConnection(node1, cost);
node1.ContainsConnection(node2); // True
node1.RemoveConnection(node2);
node2.RemoveConnection(node1);
}));
See
Pathfinding.GraphNode.AddConnection
Pathfinding.GraphNode.RemoveConnection
Pathfinding.GraphNode.ContainsConnection.