GloballyUpdatedNavMeshSurface.cs 882 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Unity.AI.Navigation.Samples
  5. {
  6. /// <summary>
  7. /// NavMeshSurface that updates only once per frame upon request
  8. /// </summary>
  9. [RequireComponent(typeof(NavMeshSurface))]
  10. public class GloballyUpdatedNavMeshSurface : MonoBehaviour
  11. {
  12. static bool s_NeedsNavMeshUpdate;
  13. NavMeshSurface m_Surface;
  14. public static void RequestNavMeshUpdate()
  15. {
  16. s_NeedsNavMeshUpdate = true;
  17. }
  18. void Start()
  19. {
  20. m_Surface = GetComponent<NavMeshSurface>();
  21. m_Surface.BuildNavMesh();
  22. }
  23. void Update()
  24. {
  25. if (s_NeedsNavMeshUpdate)
  26. {
  27. m_Surface.UpdateNavMesh(m_Surface.navMeshData);
  28. s_NeedsNavMeshUpdate = false;
  29. }
  30. }
  31. }
  32. }