Advertisement
Guest User

LocalNavMeshBuilder

a guest
Feb 26th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.AI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using NavMeshBuilder = UnityEngine.AI.NavMeshBuilder;
  6.  
  7. // Build and update a localized navmesh from the sources marked by NavMeshSourceTag
  8. [DefaultExecutionOrder(-102)]
  9. public class LocalNavMeshBuilder : MonoBehaviour
  10. {
  11.     // The center of the build
  12.     public Transform m_Tracked;
  13.  
  14.     // The size of the build bounds
  15.     public Vector3 m_Size = new Vector3(80.0f, 20.0f, 80.0f);
  16.  
  17.     NavMeshData m_NavMesh;
  18.     AsyncOperation m_Operation;
  19.     NavMeshDataInstance m_Instance;
  20.     List<NavMeshBuildSource> m_Sources = new List<NavMeshBuildSource>();
  21.  
  22.     //IEnumerator Start()
  23.     //{
  24.     //    while (true)
  25.     //    {
  26.     //        UpdateNavMesh(true);
  27.     //        yield return m_Operation;
  28.     //    }
  29.     //}
  30.  
  31.     void OnEnable()
  32.     {
  33.         // Construct and add navmesh
  34.         m_NavMesh = new NavMeshData();
  35.         m_Instance = NavMesh.AddNavMeshData(m_NavMesh);
  36.         if (m_Tracked == null)
  37.             m_Tracked = transform;
  38.         UpdateNavMesh(false);
  39.     }
  40.  
  41.     void OnDisable()
  42.     {
  43.         // Unload navmesh and clear handle
  44.         m_Instance.Remove();
  45.     }
  46.  
  47.     void UpdateNavMesh(bool asyncUpdate = false)
  48.     {
  49.         NavMeshSourceTag.Collect(ref m_Sources);
  50.         var defaultBuildSettings = NavMesh.GetSettingsByID(0);
  51.         var bounds = QuantizedBounds();
  52.  
  53.         if (asyncUpdate)
  54.             m_Operation = NavMeshBuilder.UpdateNavMeshDataAsync(m_NavMesh, defaultBuildSettings, m_Sources, bounds);
  55.         else
  56.             NavMeshBuilder.UpdateNavMeshData(m_NavMesh, defaultBuildSettings, m_Sources, bounds);
  57.     }
  58.  
  59.     static Vector3 Quantize(Vector3 v, Vector3 quant)
  60.     {
  61.         float x = quant.x * Mathf.Floor(v.x / quant.x);
  62.         float y = quant.y * Mathf.Floor(v.y / quant.y);
  63.         float z = quant.z * Mathf.Floor(v.z / quant.z);
  64.         return new Vector3(x, y, z);
  65.     }
  66.  
  67.     Bounds QuantizedBounds()
  68.     {
  69.         // Quantize the bounds to update only when theres a 10% change in size
  70.         var center = m_Tracked ? m_Tracked.position : transform.position;
  71.         return new Bounds(Quantize(center, 0.1f * m_Size), m_Size);
  72.     }
  73.  
  74.     void OnDrawGizmosSelected()
  75.     {
  76.         if (m_NavMesh)
  77.         {
  78.             Gizmos.color = Color.green;
  79.             Gizmos.DrawWireCube(m_NavMesh.sourceBounds.center, m_NavMesh.sourceBounds.size);
  80.         }
  81.  
  82.         Gizmos.color = Color.yellow;
  83.         var bounds = QuantizedBounds();
  84.         Gizmos.DrawWireCube(bounds.center, bounds.size);
  85.  
  86.         Gizmos.color = Color.green;
  87.         var center = m_Tracked ? m_Tracked.position : transform.position;
  88.         Gizmos.DrawWireCube(center, m_Size);
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement