Search Unity

NavMeshBuilder.BuildNavMeshAsync() where is it ?

Discussion in 'Navigation' started by TheCelt, Sep 20, 2018.

  1. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
    According to the documentation:

    https://docs.unity3d.com/ScriptReference/AI.NavMeshBuilder.BuildNavMeshAsync.html

    This method exists. But i can't seem to access it ? It simply is not something that exists when i try to access it.

    I am really confused why unity is half-assing the navmesh component systems. Is it abandon ware? Whats going on here, why is this stuff so poorly documented, and only available on git hub not even the package manager? It's really annoying me because its almost forcing me to buy an expensive asset or just use unreal at this point.

    Has any one been able to use it ? I don't know why i can't access it.
     
  2. adriant

    adriant

    Unity Technologies

    Joined:
    Nov 1, 2016
    Posts:
    69
    Later edit: As it was pointed out, the correct namespace is UnityEditor.AI .

    The method you are looking for is in the UnityEngine.AI namespace. You can call it using
    UnityEngine.AI.NavMeshBuilder.BuildNavMeshAsync()
    . A short note has been recently added to the 2018.3 documentation in order to clarify which methods are in the UnityEngine.AI namespace and which ones are in the UnityEditor.AI namespace. https://docs.unity3d.com/2018.3/Documentation/ScriptReference/AI.NavMeshBuilder.html

    The NavMesh components are the way to go forward. Although they haven't been turned into a package yet, they are and will be fully supported and ultimately they will be delivered as a package.
     
    Last edited: Sep 21, 2018
  3. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742

    Well i wanted to use NavMeshSurface but it doesn't call the async build version. So its not really the way to go.

    This isn't true though ? The only place it seems to exist is under the editor only code.

    See here:


    Under UnityEngine.AI.NavMeshBuilder, it does not have an async method.

    See here:


    This is what i am saying about the way Unity is handling these components, it's so confusing and the documentation isn't even correct. It all seems a bit of a mess. I am really confused by it all since it was announced in unity 5 but its almost like you guys have abandoned it half completed.
     
  4. adriant

    adriant

    Unity Technologies

    Joined:
    Nov 1, 2016
    Posts:
    69
    Oh, right. I've mixed them up myself. Sorry for that. I was trying to remember them from the top of my head without checking. The 2018.3 documentation indicates the right namespace though. It's
    UnityEditor.AI.NavMeshBuilder.BuildNavMeshAsync()
    .

    The runtime method you would want to use for async operations is
    UnityEngine.AI.NavMeshBuilder.UpdateNavMeshDataAsync
    . The data must first be created with
    UnityEngine.AI.NavMeshBuilder.BuildNavMeshData()
    and then it can be passed into the UpdateNavMeshDataAsync method.

    From the Editor UI the NavMeshSurface builds the NavMesh asynchronously. It uses the previously mentioned pattern. Please have a look at the code starting at this line https://github.com/Unity-Technologi...omponents/Editor/NavMeshSurfaceEditor.cs#L311 .
    To get the same thing at runtime first call
    navMeshSurface.BuildNavMesh()
    . This creates the NavMeshData object and builds the NavMesh once. Afterwards you can call
    navMeshSurface.UpdateNavMesh( navMeshSurface.navMeshData )
    every time you want to update the NavMesh.
     
  5. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
    Okay thank you for clarifying.

    Will you guys be able to add Async for navMeshSurface.BuildNavMesh() because that seems to call the non async version:
    Code (CSharp):
    1.  
    2. // NavMeshSurface.cs
    3. // Method : BuildNavMesh();
    4. // Line: 157
    5. var data = NavMeshBuilder.BuildNavMeshData(GetBuildSettings(),
    6.                     sources, sourcesBounds, transform.position, transform.rotation);
    Unless build async is deliberately only available for editor due to the namespace, which if so, is an unusual decision since async is quite useful for run time.
     
  6. adriant

    adriant

    Unity Technologies

    Joined:
    Nov 1, 2016
    Posts:
    69
    For the moment we will not add a
    navMeshSurface.BuildNavMeshAsync()
    method because all the runtime methods for achieving that purpose are already available and we leave it to the developers to write their own implementation. The InitializeBakeData() implemented here https://github.com/Unity-Technologies/NavMeshComponents/blob/af0ed70c649df4e3a5207be8a446348f641cec26/Assets/NavMeshComponents/Editor/NavMeshSurfaceEditor.cs#L356 gives an example how an initial empty NavMeshData object can be created. Then that object can be used to asynchronously update (generate) the NavMesh into it. In order to actually have that NavMeshData active into the world it then needs to be added tot the system with
    NavMesh.AddNavMeshData()
    , with an option to specify exactly the position and orientation.
    I've taken a note based on your suggestion that we should provide this Build async method by default.

    UnityEditor.AI.NavMeshBuilder.BuildNavMeshAsync()
    is restricted to only the Editor because of several reasons: it can only deal with the entire scene at once, it searches only for geometry marked as "Navigation static" and it only bakes for the default origin position (0,0,0) and up axis (0,1,0).
     
    Last edited: Sep 21, 2018
  7. Rootbeard

    Rootbeard

    Joined:
    Nov 10, 2015
    Posts:
    6
    Hey guys, did this ever happen? Even with relatively small areas, BuildNavMesh causes my game to stutter badly on the frame that it happens. I'd love to be able to BuildNavMeshAsync at runtime. I'm sure there are ways I can do it myself, but please understand that I'm trying to make a game without having to understand how every component works under the covers.
     
    TauSum and animaonline like this.
  8. wilgieseler

    wilgieseler

    Joined:
    Oct 17, 2013
    Posts:
    84
    Can you just add this, please? It's so silly not to have it and having a convenience method for the most valuable runtime use case that is maintained as part of the project is extremely reasonable. Why have the default offer bad performance?
     
    TauSum likes this.
  9. hasnainvohra

    hasnainvohra

    Joined:
    Apr 22, 2021
    Posts:
    1
    +1. Can you please add this method??? If we need to hack together a bunch of methods to do this, why not just add it?
     
  10. wilgieseler

    wilgieseler

    Joined:
    Oct 17, 2013
    Posts:
    84
    @Jakob_Unity Could you or someone else working on the navigation provide an example of how to do this?
     
  11. ckempke

    ckempke

    Joined:
    Dec 15, 2020
    Posts:
    20
    Yeah, still another request to just build this in, or at least provide a modern example. It's been almost half a decade, and the NavMesh Components stuff has all been long since abandoned--but we still need to actually make nav meshes at runtime without locking up the main thread. It may be possible to reverse engineer all that antediluvian component code to "let the developers write their own implementation," but just looking at it gives me a headache. I can't believe the current, non-async runtime code would be acceptable to almost anybody, right? It takes significant fractions of a second even on simple cases.
     
  12. ckempke

    ckempke

    Joined:
    Dec 15, 2020
    Posts:
    20
    So I beat my way through this.

    Short answer:
    In a Co-routine:
    ...CollectSources, get build settings, as usual, then:
    NavMeshData data = new NavMeshData()
    AsyncOperation operation = NavMeshBuilder.UpdateNavMeshDataAsync(data...)
    while (!operation.done) yield return null;
    NavMesh.AddNavMeshData(data);

    If that's not enough, I wrote up the (very) long answer here: https://ckempke.github.io/Blog/blog/runtime_navmesh/

    That gives pretty much all the background, as well as some actual cut-and-pastable code at the end. Hopefully it helps out the next person who needs to do this...
     
  13. jacksonkr

    jacksonkr

    Joined:
    Jan 22, 2013
    Posts:
    23
    @ckempke I was hoping to optimize your answer by leveraging a yield return on (pseudocode) WaitUntil NavBuldMesh.IsRunning != true but I didn't have much luck. Your answer still stands!
     
  14. jamespaterson

    jamespaterson

    Joined:
    Jun 19, 2018
    Posts:
    401
    just want to say thanks to @ckempke for your blog post. Very helpful.
    also, the mind boggles at quite how confusing and multiply-half baked the unity nav mesh system is.
    There is something like four or five different attempts at what is essentially exactly the same actually very useful system with marginal improvements / changes in functionality, different namespaces / integrated / not integrated with Editor, github, package. the list goes on. Kafka would have a field day.