Batch processing of the scene , generating cache

I want to do a batch processing of the scenes I have to enable the cache graph data and node data and then set some couple of options. Then I want to save the scene with different name , preserving my original scene.

Now, I had look at the editor script for the AstarPathEditor and I was hoping I could do the following :

  1. Open scene

  2. Do the following to set up the scene and cache the data

         serializationSettings.nodes =  true;
         serializationSettings.prettyPrint = false;
    
         AstarPath.active.astarData.cacheStartup = true;
         AstarPath.active.scanOnStartup = false;
         AstarPath.active.Scan();
    
         AstarPath.active.astarData.SaveCacheData(serializationSettings);
    
  3. I have serializationSettings decleared exactly the same way as the editor script :

public static Pathfinding.Serialization.SerializeSettings serializationSettings = Pathfinding.Serialization.SerializeSettings.All;

  1. Save as other name and repeat for other scenes.

All of above using editor script and process them in editor mode. (Not in play mode)

But then I get null reference on AstarPath.active.astarData.cacheStartup = true;

All of the scenes do contain a game object with AstarPath script attached to it and all other settings are manually adjusted in. Strange thing is if I do call above script process manually after I have opened up the scene , it works fine. But not when I use script to open the scene and then call the bathing process script right after it.

I am wondering if it needs any time to cache in active path in AstarPath ?

Any helpful direction would be cool

I have sort of figured it out :

Used following code instead :

script = GameObject.FindObjectOfType();
AstarPath.active = script;

Looks like I had to find the AstarPath component and then also make sure it is a singleton.

Not sure if it is 100% right way to go , but it seems to do the job for me.

Well, it seems that it doesn’t actually scan the graph properly after all… If I use editor to generate cache I should get 23.5kb but when I used API call it only gets like 450 bytes.

I must be missing something here… :stuck_out_tongue:

Yes, I have missed doing AstarPath.active.astarData.DeserializeGraphs();

Well, I don’t know what that does, but it seems to work now… so in order to scan the graph and cache in, I need to do the DeserializeGraphs() and Scan() ?

Also, even with using cache on start up , I still get this log :

Scanning - Process took 1459 ms to complete

Thing is… it actually does seem faster, maybe it isn’t really scanning like before. Not sure. Is this normal?

Edit : I have found source of the log and removed it. I was manually calling scan somewhere inside my other old code. :smiley:

Hi

Well, you seem to have solved this all by yourself.

The reason you need to set AstarPath.active is that there is no code that has initialized the singleton pattern yet (as you noted).
The reason you need to call DeserializeGraphs is that since the AstarPath object has not really been initialized, the graphs are just stored in a byte array and the AstarPath object thinks it has no graphs added, calling DeserializeGraphs will load them.