Duplicating a 2D grid graph

I have individual areas that I would like to put graphs over, but I cannot figure out how to duplicate a specific graph or create one in a script that will fit my needs. I need it to be 2D and to be a certain size.
I have tried this so far…

GridGraph baseGraph = AstarPath.active.data.gridGraph;
for (int i = 0; i < count; i++)
    GridGraph gg = AstarPath.active.data.AddGraph(typeof(GridGraph)) as GridGraph;
    gg = baseGraph;
    gg.center = offset - (Vector2)tg.size / 2;
    offset += new Vector2(Random.Range(200, 400), Random.Range(-5, 5));

This code seems to change the base one as well as formatting the subsiquent ones wrong. Which makes sense other than it changes the plane of the first one.

GridGraph gg = AstarPath.active.data.AddGraph(typeof(GridGraph)) as GridGraph;
Here you’re accessing the first active graph in the system, and continue changing this one.
You need to create a New GridGraph and add it to the system.


On the line

gg = baseGraph

I’m guessing that you are trying to copy the graph settings. This is not how C# works however. You are just replacing the gg variable with the baseGraph, your later changes will just be to the baseGraph and not to the graph you just created.

If you want to duplicate a graph the easiest way is to serialize it and then load it additively.

var bytes = AstarPath.active.data.SerializeGraphs();

The above code will duplicate all loaded graphs.

Is it possible to just duplicate one of the graphs, instead of all of them? I pretty much want to copy it as a “Prefab”. The reason I can’t just create a new one is because I can’t figure out how to get it to be the same as the one made in the inspector.


Right now it’s not possible to copy just one of them I’m afraid. I guess you could remove all graphs other than the one you want to save first.