Advertisement
MaximilianPs

BakeNaveMesh

Sep 6th, 2021
1,193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. ///<summary>
  2. ///
  3. /// This class is used for dungeons or terrain(not tested)
  4. /// once the scene is ready and the NavMesh as been baked, will activate all NPCs
  5. /// via AIManager
  6. ///
  7. ///     ADD THIS COMPONENT TO A PARENT THAT INCLUDE ALL WALKABLE SURFACE
  8. ///    
  9. ///</summary>
  10.  
  11. using System.Collections.Generic;
  12. using UnityEngine;
  13. using UnityEngine.AI;
  14. [RequireComponent(typeof(AiManager))]
  15. public class NavMeshBaker : MonoBehaviour
  16. {
  17.     [SerializeField] bool OnStart = false;
  18.     [SerializeField] bool debugBounds = false;
  19.     [Space]
  20.     [SerializeField] List<NavMeshSurface> navMeshSurfaces = new List<NavMeshSurface>();
  21.  
  22.     NavMeshBuildSettings set = NavMesh.CreateSettings();
  23.    
  24.     private void Start()
  25.     {
  26.         if (OnStart)
  27.             BakeNavMesh();
  28.     }
  29.     /// <summary>
  30.     /// It will store all tiles with a NavMeshSurface and generate the NavMesh, then will awake all NPCs
  31.     /// </summary>
  32.     public void BakeNavMesh()
  33.     {
  34.         // Add all floors the array
  35.         navMeshSurfaces = new List<NavMeshSurface>(GetComponentsInChildren<NavMeshSurface>());
  36.        
  37.         NavMeshData built = NavMeshBuilder.BuildNavMeshData(set,Nav2Nav(navMeshSurfaces), CalculateLocalBounds(), transform.position, transform.rotation);
  38.         NavMesh.AddNavMeshData(built);
  39.  
  40.         GetComponent<AiManager>().EnableEnemies();
  41.     }
  42.  
  43.     private List<NavMeshBuildSource> Nav2Nav(List<NavMeshSurface> array)
  44.     {
  45.         List<NavMeshBuildSource> nmBuildSource = new List<NavMeshBuildSource>();
  46.  
  47.         foreach (var tile in array)
  48.         {
  49.             NavMeshBuildSource floor = new NavMeshBuildSource();
  50.             floor.transform = tile.transform.localToWorldMatrix;
  51.             floor.shape = NavMeshBuildSourceShape.Box;
  52.             floor.size = tile.GetComponent<Renderer>().bounds.size;
  53.             nmBuildSource.Add(floor);
  54.         }
  55.  
  56.         return nmBuildSource;
  57.     }
  58.  
  59.     private Bounds CalculateLocalBounds()
  60.     {
  61.         navMeshSurfaces.Clear();
  62.         navMeshSurfaces = new List<NavMeshSurface>(GetComponentsInChildren<NavMeshSurface>());
  63.  
  64.         Bounds bounds = new Bounds(this.transform.position, Vector3.zero);
  65.  
  66.         foreach (var floor in navMeshSurfaces)
  67.         {
  68.             bounds.Encapsulate(floor.GetComponent<Renderer>().bounds);
  69.         }
  70.  
  71.         Vector3 localCenter = bounds.center - this.transform.localPosition;
  72.         bounds.center = localCenter;
  73.         Debug.Log("The local bounds of this model is " + bounds);
  74.  
  75.         return bounds;
  76.     }
  77.  
  78.     void OnDrawGizmosSelected()
  79.     {
  80.  
  81.         BakeNavMesh();
  82.  
  83.         if (debugBounds)
  84.         {
  85.             Bounds bounds = CalculateLocalBounds();
  86.             Debug.Log("Selected obj " + bounds);
  87.  
  88.             Gizmos.color = new Color(1, 1, 1, 0.25f);
  89.             Gizmos.DrawCube(bounds.center, bounds.size);
  90.             Gizmos.DrawWireCube(bounds.center, bounds.size);
  91.         }
  92.     }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement