Cloning a graph in realtime

  • A* version: 5.4.6
  • Unity version: 6000.3.10f1

Hi, just wondered if there was an efficient way to copy/clone a recast graph?

I use multiple graphs (in fixed positions), and sometimes they are of the same area, so one great optimisation to avoid a graph rescan would be if I could copy/clone a recast graph.

I’ve searched and found mention of using SerializeGraph, which I’ve tested:

    public byte[] CloneGraph()
    {
        return AstarPath.active.data.SerializeGraph(new Pathfinding.Serialization.SerializeSettings { nodes = true }, graph);
    }

    public IEnumerator RestoreGraph(byte[] serializedGraph)
    {
        if (graph != null)
        {
            AstarData activeData = AstarPath.active.data;
            activeData.RemoveGraph(graph);
        }

        NavGraph[] result = AstarPath.active.data.DeserializeGraphsAdditive(serializedGraph);
        graph = result[0] as RecastGraph;

        graph.name = $"Graph_{GetName()}";

        yield return PositionGraph(centreTile.Centre.x, centreTile.Centre.z);
    }

Unfortunately, its quite slow for realtime use and most of the time a graph.Scan() is actually faster - which is impressive, but that still takes fair bit of processing time.

Since a graph has already been scanned (which surely must be quite heavy work), and the graph is there in memory, I was wondering if there was a way to take a copy of it?