I have maps as prefabs for a number of reasons, and am wanting to use graphs that are baked in editor and saved/loaded to file.
My original plan was while in a prefab stage to create an GameObject with HideFlags and a AstarPath component, and set its parent to the root GameObject of the stage. Then scan and save. However it seems to ignore RecastNavmeshModifier components, or maybe just some colliders/GameObjects in general? And if another AstarPath component has been created it will throw exceptions half the time, even when I setting the active instance.
Part of the reason for doing this is for runtime performance, but also for the level designer to more quick see navmesh errors and fix them.
I also realized that some prefabs are not spawned at 0, 0, 0 so would need a way to move the graphs. Best I thought of was to iterate on all of the nodes and apply an offset to them, but idk if that is even possible (haven’t looked in to this part yet).
Anyway, I know this is not really an intentional workflow, but was wondering if there was any suggestions for how to possibly approach some of this. Or is this somewhat futile? Thanks!
So are you scanning from inside the prefab with its own AstarPath component? AFAIK you shouldn’t pretty much ever use more than one AstarPath component. If anything I’d imagine you just add the prefab to your open scene, or some kind of “navmesh baking scene” and then bake and then save.
Are you able to give me a detailed step by step to reproduce this to see where the issue might be?
Yeah, the idea is to scan from within the prefab so we can ensure that the graphs are always up to date and the level designer can debug easily. While I understand you shouldn’t really ever have multiple, at a glance it looked like it should be fine for just scanning.
The exact steps to reproduce:
Have a scene open with a AstarPath component on one of the GameObjects in the scene.
Open a prefab (double click) from the project browser window.
Run the below method from the menu
[MenuItem("Tools/Scan Graph In Prefab")]
static void Scan() {
var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
if (prefabStage == null) return;
var pathGameObject = EditorUtility.CreateGameObjectWithHideFlags("Path", HideFlags.DontSaveInEditor, typeof(AstarPath));
pathGameObject.transform.SetParent(prefabStage.prefabContentsRoot.transform);
AstarPath.active = pathGameObject.GetComponent<AstarPath>();
AstarPath.active.scanOnStartup = true;
AstarPath.active.showGraphs = true;
var graph = AstarPath.active.data.AddGraph<RecastGraph>();
graph.forcedBoundsSize = new Vector3(300, 100, 300);
AstarPath.active.Scan();
}
As I was writting this reply, I found the NavmeshPrefab component which looks like it would be about perfect from a brief look. But again does not seem to like baking from prefab stage either.
And yes, adding the prefabs to a scene and then baking would be possible, it would also either add decent overhead to iteration time, or be easy to forget. So for us that is for sure a last resort type of option.
Quick thought, wouldn’t you be able to modify this script to simply 1. load prefab 2. disable other stuff (maybe just use offset to prevent even having to do this) 3. then scan and remove the prefab? Or something of the sort? You kinda don’t have to worry about the iteration time in that case because it’s the same thing just simply not trying to force it to happen in the prefab space.
Sorry for the delay in getting back to you on this. While yes that would work as an alternative, it doesn’t allow the level designer to see the graph as they work and make changes to the level to fix it. The prefabs are also quite large, so loading them up and doing a scan in editor would not be seamless.
The idea for me at least would be to automatically bake a graph every time the prefab is saved/modified. So the level designer could have the prefab open and working on it, hit save and then just see the new graph.
Does your method work if you try it in a scene without an AstarPath in it? Or in this scenario do you still run into the issue where it ignores scripts in the prefab scene?
I’ll have to have Aron take a look at this one when he’s available. I’ve been trying to look for a solution that may work but I think this gets pretty deep into the weeds if you specifically want it to work the way you do.