Advertisement
Guest User

Untitled

a guest
Aug 30th, 2017
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 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(250.0f, 128.0f, 250.0f);
  16.    
  17.     NavMeshData m_NavMesh;
  18.     NavMeshDataInstance m_Instance;
  19.     List<NavMeshBuildSource> m_Sources = new List<NavMeshBuildSource>();
  20.    
  21.     void OnEnable()
  22.     {
  23.         // Construct and add navmesh
  24.         m_NavMesh = new NavMeshData();
  25.         m_Instance = NavMesh.AddNavMeshData(m_NavMesh);
  26.         if (m_Tracked == null)
  27.             m_Tracked = transform;
  28.         StartCoroutine(BakeCoRoutine());
  29.     }
  30.    
  31.     IEnumerator BakeCoRoutine()
  32.     {
  33.         while(true)
  34.         {
  35.         NavMeshSourceTag.Collect(ref m_Sources);
  36.         var defaultBuildSettings = NavMesh.GetSettingsByID(0);
  37.         var bounds = QuantizedBounds();
  38.             NavMeshBuilder.UpdateNavMeshDataAsync(m_NavMesh, defaultBuildSettings, m_Sources, bounds);
  39.             yield return new WaitForSeconds(1f);
  40.         }
  41.     }
  42.    
  43.     static Vector3 Quantize(Vector3 v, Vector3 quant)
  44.     {
  45.         float x = quant.x * Mathf.Floor(v.x / quant.x);
  46.         float y = quant.y * Mathf.Floor(v.y / quant.y);
  47.         float z = quant.z * Mathf.Floor(v.z / quant.z);
  48.         return new Vector3(x, y, z);
  49.     }
  50.    
  51.     Bounds QuantizedBounds()
  52.     {
  53.         // Quantize the bounds to update only when theres a 10% change in size
  54.         var center = m_Tracked ? m_Tracked.position : transform.position;
  55.         return new Bounds(Quantize(center, 0.1f * m_Size), m_Size);
  56.     }
  57.    
  58.     void OnDrawGizmosSelected()
  59.     {
  60.         if (m_NavMesh)
  61.         {
  62.             Gizmos.color = Color.green;
  63.             Gizmos.DrawWireCube(m_NavMesh.sourceBounds.center, m_NavMesh.sourceBounds.size);
  64.         }
  65.        
  66.         Gizmos.color = Color.yellow;
  67.         var bounds = QuantizedBounds();
  68.         Gizmos.DrawWireCube(bounds.center, bounds.size);
  69.        
  70.         Gizmos.color = Color.green;
  71.         var center = m_Tracked ? m_Tracked.position : transform.position;
  72.         Gizmos.DrawWireCube(center, m_Size);
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement