NavMeshSurfaceTests.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #if UNITY_EDITOR || UNITY_STANDALONE
  2. using UnityEngine;
  3. using UnityEngine.AI;
  4. using UnityEngine.TestTools;
  5. using NUnit.Framework;
  6. using System.Collections;
  7. [TestFixture]
  8. public class NavMeshSurfaceTests
  9. {
  10. GameObject plane;
  11. NavMeshSurface surface;
  12. [SetUp]
  13. public void CreatePlaneWithSurface()
  14. {
  15. plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
  16. surface = new GameObject().AddComponent<NavMeshSurface>();
  17. Assert.IsFalse(HasNavMeshAtOrigin());
  18. }
  19. [TearDown]
  20. public void DestroyPlaneWithSurface()
  21. {
  22. GameObject.DestroyImmediate(plane);
  23. GameObject.DestroyImmediate(surface.gameObject);
  24. Assert.IsFalse(HasNavMeshAtOrigin());
  25. }
  26. [Test]
  27. public void NavMeshIsAvailableAfterBuild()
  28. {
  29. surface.BuildNavMesh();
  30. Assert.IsTrue(HasNavMeshAtOrigin());
  31. }
  32. [Test]
  33. public void NavMeshCanBeRemovedAndAdded()
  34. {
  35. surface.BuildNavMesh();
  36. Assert.IsTrue(HasNavMeshAtOrigin());
  37. surface.RemoveData();
  38. Assert.IsFalse(HasNavMeshAtOrigin());
  39. surface.AddData();
  40. Assert.IsTrue(HasNavMeshAtOrigin());
  41. }
  42. [Test]
  43. public void NavMeshIsNotAvailableWhenDisabled()
  44. {
  45. surface.BuildNavMesh();
  46. surface.enabled = false;
  47. Assert.IsFalse(HasNavMeshAtOrigin());
  48. surface.enabled = true;
  49. Assert.IsTrue(HasNavMeshAtOrigin());
  50. }
  51. [Test]
  52. public void CanBuildWithCustomArea()
  53. {
  54. surface.defaultArea = 4;
  55. var expectedAreaMask = 1 << 4;
  56. surface.BuildNavMesh();
  57. Assert.IsTrue(HasNavMeshAtOrigin(expectedAreaMask));
  58. }
  59. [Test]
  60. public void CanBuildWithCustomAgentTypeID()
  61. {
  62. surface.agentTypeID = 1234;
  63. surface.BuildNavMesh();
  64. Assert.IsTrue(HasNavMeshAtOrigin(NavMesh.AllAreas, 1234));
  65. }
  66. [Test]
  67. public void CanBuildCollidersAndIgnoreRenderMeshes()
  68. {
  69. plane.GetComponent<MeshRenderer>().enabled = false;
  70. surface.useGeometry = NavMeshCollectGeometry.PhysicsColliders;
  71. surface.BuildNavMesh();
  72. Assert.IsTrue(HasNavMeshAtOrigin());
  73. surface.useGeometry = NavMeshCollectGeometry.RenderMeshes;
  74. surface.BuildNavMesh();
  75. Assert.IsFalse(HasNavMeshAtOrigin());
  76. }
  77. [Test]
  78. public void CanBuildRenderMeshesAndIgnoreColliders()
  79. {
  80. plane.GetComponent<Collider>().enabled = false;
  81. surface.useGeometry = NavMeshCollectGeometry.PhysicsColliders;
  82. surface.BuildNavMesh();
  83. Assert.IsFalse(HasNavMeshAtOrigin());
  84. surface.useGeometry = NavMeshCollectGeometry.RenderMeshes;
  85. surface.BuildNavMesh();
  86. Assert.IsTrue(HasNavMeshAtOrigin());
  87. }
  88. [Test]
  89. public void BuildIgnoresGeometryOutsideBounds()
  90. {
  91. surface.collectObjects = CollectObjects.Volume;
  92. surface.center = new Vector3(20, 0, 0);
  93. surface.size = new Vector3(10, 10, 10);
  94. surface.BuildNavMesh();
  95. Assert.IsFalse(HasNavMeshAtOrigin());
  96. }
  97. [Test]
  98. public void BuildIgnoresGeometrySiblings()
  99. {
  100. surface.collectObjects = CollectObjects.Children;
  101. surface.BuildNavMesh();
  102. Assert.IsFalse(HasNavMeshAtOrigin());
  103. }
  104. [Test]
  105. public void BuildUsesOnlyIncludedLayers()
  106. {
  107. plane.layer = 4;
  108. surface.layerMask = ~(1 << 4);
  109. surface.BuildNavMesh();
  110. Assert.IsFalse(HasNavMeshAtOrigin());
  111. }
  112. [Test]
  113. public void DefaultSettingsMatchBuiltinSettings()
  114. {
  115. var bs = surface.GetBuildSettings();
  116. Assert.AreEqual(NavMesh.GetSettingsByIndex(0), bs);
  117. }
  118. [Test]
  119. public void ActiveSurfacesContainsOnlyActiveAndEnabledSurface()
  120. {
  121. Assert.IsTrue(NavMeshSurface.activeSurfaces.Contains(surface));
  122. Assert.AreEqual(1, NavMeshSurface.activeSurfaces.Count);
  123. surface.enabled = false;
  124. Assert.IsFalse(NavMeshSurface.activeSurfaces.Contains(surface));
  125. Assert.AreEqual(0, NavMeshSurface.activeSurfaces.Count);
  126. surface.enabled = true;
  127. surface.gameObject.SetActive(false);
  128. Assert.IsFalse(NavMeshSurface.activeSurfaces.Contains(surface));
  129. Assert.AreEqual(0, NavMeshSurface.activeSurfaces.Count);
  130. }
  131. [UnityTest]
  132. public IEnumerator NavMeshMovesToSurfacePositionNextFrame()
  133. {
  134. plane.transform.position = new Vector3(100, 0, 0);
  135. surface.transform.position = new Vector3(100, 0, 0);
  136. surface.BuildNavMesh();
  137. Assert.IsFalse(HasNavMeshAtOrigin());
  138. surface.transform.position = Vector3.zero;
  139. Assert.IsFalse(HasNavMeshAtOrigin());
  140. yield return null;
  141. Assert.IsTrue(HasNavMeshAtOrigin());
  142. }
  143. [UnityTest]
  144. public IEnumerator UpdatingAndAddingNavMesh()
  145. {
  146. var navmeshData = new NavMeshData();
  147. var oper = surface.UpdateNavMesh(navmeshData);
  148. Assert.IsFalse(HasNavMeshAtOrigin());
  149. do { yield return null; } while (!oper.isDone);
  150. surface.RemoveData();
  151. surface.navMeshData = navmeshData;
  152. surface.AddData();
  153. Assert.IsTrue(HasNavMeshAtOrigin());
  154. }
  155. public static bool HasNavMeshAtOrigin(int areaMask = NavMesh.AllAreas, int agentTypeID = 0)
  156. {
  157. var hit = new NavMeshHit();
  158. var filter = new NavMeshQueryFilter();
  159. filter.areaMask = areaMask;
  160. filter.agentTypeID = agentTypeID;
  161. return NavMesh.SamplePosition(Vector3.zero, out hit, 0.1f, filter);
  162. }
  163. }
  164. #endif