How to get a callback when NavMesh gets updated?

I’m making my own nav mesh agent and I need to know when a NavMeshObstacle has carved a hole and updated the navmesh, so that I can recalculate the path.

Does the NavMesh API provide a callback for this? Or an efficient way to detect a change?

Just create one using a delegate or event?

If not, use this API Unity - Scripting API: AI.NavMeshBuilder.UpdateNavMeshData making sure you have using UnityEngine.AI.

You will need to carefully think about how to generate navmeshes this way though.

Thanks for the answer but that’s not exactly what I’m looking for. Basically, what I’d like is to have an event that fires when Unity’s navmesh system itself calls “UpdateNavmeshData” (or an equivalent) in the background

The navmesh system knows when it has to recalculate, based on obstacles being moved, etc… So it would be a massive waste of performance to have to manually check for re-calculations every frame

I understand, but unfortunately things are not really made that way. If you want some kind of callback you will need to make one, (which would not waste performance as a callback is event based anyway?), otherwise switch from using the navmesh baker to using the navmeshbuilder api like I said, so you can control all navmesh stuff from code, making this possible as you could then write an event or delegate that wraps up the updatenavmeshdata.

oh wait, I didn’t see that part of UpdateNavMeshData:

This will do the job just fine. I thought it recomputed everything every time.

Thanks

1 Like

Sorry for necroing this thread, but how would you go about hooking in a callback in the NavMesh? This seems to me to be the easiest approach if you’re not already building around using the NavMeshBuilder, but I can’t wrap my head around as to how you’d make that actual callback.
Thanks in advance!

2 Likes