NavMeshSurfaceLinkTests.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 NavMeshSurfaceLinkTests
  9. {
  10. public GameObject plane1, plane2;
  11. public NavMeshLink link;
  12. public NavMeshSurface surface;
  13. [SetUp]
  14. public void CreatesPlanesAndLink()
  15. {
  16. plane1 = GameObject.CreatePrimitive(PrimitiveType.Plane);
  17. plane2 = GameObject.CreatePrimitive(PrimitiveType.Plane);
  18. plane1.transform.position = 11.0f * Vector3.right;
  19. surface = new GameObject().AddComponent<NavMeshSurface>();
  20. surface.BuildNavMesh();
  21. Assert.IsFalse(HasPathConnecting(plane1, plane2));
  22. Assert.IsFalse(HasPathConnecting(plane2, plane1));
  23. link = new GameObject().AddComponent<NavMeshLink>();
  24. link.startPoint = plane1.transform.position;
  25. link.endPoint = plane2.transform.position;
  26. Assert.IsTrue(HasPathConnecting(plane1, plane2));
  27. Assert.IsTrue(HasPathConnecting(plane2, plane1));
  28. }
  29. [TearDown]
  30. public void DestroyPlanesAndLink()
  31. {
  32. GameObject.DestroyImmediate(surface.gameObject);
  33. GameObject.DestroyImmediate(link.gameObject);
  34. GameObject.DestroyImmediate(plane1);
  35. GameObject.DestroyImmediate(plane2);
  36. }
  37. [Test]
  38. public void NavMeshLinkCanConnectTwoSurfaces()
  39. {
  40. Assert.IsTrue(HasPathConnecting(plane1, plane2));
  41. }
  42. [Test]
  43. public void DisablingBidirectionalMakesTheLinkOneWay()
  44. {
  45. link.bidirectional = false;
  46. Assert.IsTrue(HasPathConnecting(plane1, plane2));
  47. Assert.IsFalse(HasPathConnecting(plane2, plane1));
  48. }
  49. [Test]
  50. public void ChangingAreaTypeCanBlockPath()
  51. {
  52. var areaMask = ~(1 << 4);
  53. Assert.IsTrue(HasPathConnecting(plane1, plane2, areaMask));
  54. link.area = 4;
  55. Assert.IsFalse(HasPathConnecting(plane1, plane2, areaMask));
  56. }
  57. [Test]
  58. public void EndpointsMoveRelativeToLinkOnUpdate()
  59. {
  60. link.transform.position += Vector3.forward;
  61. Assert.IsFalse(HasPathConnectingViaPoint(plane1, plane2, plane1.transform.position + Vector3.forward));
  62. Assert.IsFalse(HasPathConnectingViaPoint(plane1, plane2, plane2.transform.position + Vector3.forward));
  63. link.UpdateLink();
  64. Assert.IsTrue(HasPathConnectingViaPoint(plane1, plane2, plane1.transform.position + Vector3.forward));
  65. Assert.IsTrue(HasPathConnectingViaPoint(plane1, plane2, plane2.transform.position + Vector3.forward));
  66. }
  67. [UnityTest]
  68. public IEnumerator EndpointsMoveRelativeToLinkNextFrameWhenAutoUpdating()
  69. {
  70. link.transform.position += Vector3.forward;
  71. link.autoUpdate = true;
  72. Assert.IsFalse(HasPathConnectingViaPoint(plane1, plane2, plane1.transform.position + Vector3.forward));
  73. Assert.IsFalse(HasPathConnectingViaPoint(plane1, plane2, plane2.transform.position + Vector3.forward));
  74. yield return null;
  75. Assert.IsTrue(HasPathConnectingViaPoint(plane1, plane2, plane1.transform.position + Vector3.forward));
  76. Assert.IsTrue(HasPathConnectingViaPoint(plane1, plane2, plane2.transform.position + Vector3.forward));
  77. }
  78. [Test]
  79. public void ChangingCostModifierAffectsRoute()
  80. {
  81. var link1 = link;
  82. link1.startPoint = plane1.transform.position;
  83. link1.endPoint = plane2.transform.position + Vector3.forward;
  84. var link2 = link.gameObject.AddComponent<NavMeshLink>();
  85. link2.startPoint = plane1.transform.position;
  86. link2.endPoint = plane2.transform.position - Vector3.forward;
  87. link1.costModifier = -1;
  88. link2.costModifier = 100;
  89. Assert.IsTrue(HasPathConnectingViaPoint(plane1, plane2, link1.endPoint));
  90. Assert.IsFalse(HasPathConnectingViaPoint(plane1, plane2, link2.endPoint));
  91. link1.costModifier = 100;
  92. link2.costModifier = -1;
  93. Assert.IsFalse(HasPathConnectingViaPoint(plane1, plane2, link1.endPoint));
  94. Assert.IsTrue(HasPathConnectingViaPoint(plane1, plane2, link2.endPoint));
  95. }
  96. public static bool HasPathConnecting(GameObject a, GameObject b, int areaMask = NavMesh.AllAreas, int agentTypeID = 0)
  97. {
  98. var path = new NavMeshPath();
  99. var filter = new NavMeshQueryFilter();
  100. filter.areaMask = areaMask;
  101. filter.agentTypeID = agentTypeID;
  102. NavMesh.CalculatePath(a.transform.position, b.transform.position, filter, path);
  103. return path.status == NavMeshPathStatus.PathComplete;
  104. }
  105. public static bool HasPathConnectingViaPoint(GameObject a, GameObject b, Vector3 point, int areaMask = NavMesh.AllAreas, int agentTypeID = 0)
  106. {
  107. var path = new NavMeshPath();
  108. var filter = new NavMeshQueryFilter();
  109. filter.areaMask = areaMask;
  110. filter.agentTypeID = agentTypeID;
  111. NavMesh.CalculatePath(a.transform.position, b.transform.position, filter, path);
  112. if (path.status != NavMeshPathStatus.PathComplete)
  113. return false;
  114. for (int i = 0; i < path.corners.Length; ++i)
  115. if (Vector3.Distance(path.corners[i], point) < 0.1f)
  116. return true;
  117. return false;
  118. }
  119. }
  120. #endif