Performance of multiple Layered Grid Graphs

Besides the initial cost of generating the graph, is there a performance penalty to having many layered grid graphs, one per unit? I’m asking because I could consolidate units into small, medium, and large groups to reduce the number of layered grid graphs, but if there is no cost to having multiple graphs I would just create one per unit type.

Hi

There is a high memory cost as the nodes have to be saved somewhere. Depending on your graph, this may be significant enough to care about.
Graph updates will also be slower as more graphs have to be updated.
However pathfinding performance will not be affected significantly (except due to cache effects of using more memory).

How do I assign a graph to be used for particular unit types? Is this what graphMask is for?

I’ve written code to add a graph at runtime based on the actual dimensions of the units that are imported by the modding system. But I can’t tell that it’s working. There is no new graph in the inspector.

Does this seem correct?

public static int GetGraphIndexForUnit(float characterHeight, float characterDiameter, float maxSlopeDegrees, float maxClimb)
{
if (Terrain.activeTerrain == null)
return -1;
// (Snipped)
LayerGridGraph layeredGridGraph = (LayerGridGraph) AstarPath.active.data.AddGraph(typeof(LayerGridGraph));
// (Snipped)
layeredGridGraph.width = 100;
layeredGridGraph.depth = 100;
AstarPath.active.Scan(layeredGridGraph);

Yes. That is correct.
You just set the bit corresponding to the graph index.

var graphMask = 1 << 5; // Use graph with index 5

Yeah that code looks reasonable.
For slightly improved performance I would recommend adding all graphs first and then scanning them all at the same time.