How to do it...

We will create a new component called NavMeshBuilder:

  1. Create a new file called NavMeshBuilder.cs:
using UnityEngine;using UnityEngine.AI;using System.Collections;using System.Collections.Generic;public class NavMeshBuilder : MonoBehaviour{  // Next steps}
  1. Add a member variable for storing the navigation surfaces:
public NavMeshSurface[] surfaces;
  1. Implement the method for building all the NavMesh at once:
public void Build(){  for (int i = 0; i < surfaces.Length; i++)  {    surfaces[i].BuildNavMesh();  }}
  1. Implement the method for building all the NavMesh in between frames:
public IEnumerator BuildInFrames(System.Action eventHandler){  for (int i = 0; i < surfaces.Length; i++)  {    surfaces[i].BuildNavMesh(); yield return null; ...

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.