暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OffMeshLinkMultipleAddComponent.cs 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Collections;
  2. using NUnit.Framework;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. using UnityEngine.SceneManagement;
  6. using UnityEngine.TestTools;
  7. namespace Unity.AI.Navigation.Tests
  8. {
  9. [TestFixture]
  10. [PrebuildSetup("Unity.AI.Navigation.Tests." + nameof(SimpleScene2PlanesNavigationSetup))]
  11. [PostBuildCleanup("Unity.AI.Navigation.Tests." + nameof(SimpleScene2PlanesNavigationSetup))]
  12. class OffMeshLinkMultipleAddComponent
  13. {
  14. const string k_SceneName = "OffMeshLinkTwoPlanesScene";
  15. GameObject m_LinkGO;
  16. [UnitySetUp]
  17. public IEnumerator UnitySetUp()
  18. {
  19. yield return SceneManager.LoadSceneAsync(k_SceneName, LoadSceneMode.Additive);
  20. yield return null;
  21. SceneManager.SetActiveScene(SceneManager.GetSceneByName(k_SceneName));
  22. m_LinkGO = new GameObject("OffMeshLinkMultipleAddComponent");
  23. }
  24. [Test]
  25. [UnityPlatform(exclude = new[] { RuntimePlatform.OSXServer, RuntimePlatform.WindowsServer, RuntimePlatform.LinuxServer })]
  26. public void OffMeshLink_WhenMultipleAddedToGameObject_AreAllUsable()
  27. {
  28. var a = GameObject.Find("plane1").GetComponent<Transform>();
  29. var b = GameObject.Find("plane2").GetComponent<Transform>();
  30. Assert.That(a, Is.Not.Null, "Plane1 is missing.");
  31. Assert.That(b, Is.Not.Null, "Plane2 is missing.");
  32. var pathAB = new NavMeshPath();
  33. var pathBA = new NavMeshPath();
  34. var foundAB = NavMesh.CalculatePath(a.position, b.position, -1, pathAB);
  35. var foundBA = NavMesh.CalculatePath(b.position, a.position, -1, pathBA);
  36. Assert.That(foundAB, Is.True, "Found unexpected path A->B.");
  37. Assert.That(foundBA, Is.True, "Found unexpected path B->A.");
  38. // Create setup where one GO has two OffMeshLinks with 'Bi Directional' set to false
  39. AddOneWayLink(a, b);
  40. AddOneWayLink(b, a);
  41. // Tests that path a->b and b->a are valid and have same end-points (mirrored).
  42. foundAB = NavMesh.CalculatePath(a.position, b.position, -1, pathAB);
  43. foundBA = NavMesh.CalculatePath(b.position, a.position, -1, pathBA);
  44. Assert.That(foundAB, Is.True, "No path from A->B");
  45. Assert.That(foundBA, Is.True, "No path from B->A");
  46. var d1 = Vector3.Distance(pathAB.corners[0], pathBA.corners[pathBA.corners.Length - 1]);
  47. var d2 = Vector3.Distance(pathAB.corners[pathAB.corners.Length - 1], pathBA.corners[0]);
  48. Assert.That(d1, Is.EqualTo(0.0f).Within(1e-5f), "Endpoint mismatch: A start -> B end.");
  49. Assert.That(d2, Is.EqualTo(0.0f).Within(1e-5f), "Endpoint mismatch: B start -> A end.");
  50. }
  51. void AddOneWayLink(Transform start, Transform end)
  52. {
  53. var offMeshLink = m_LinkGO.AddComponent<NavMeshLink>();
  54. Assert.That(offMeshLink, Is.Not.Null, "Failed to create NavMeshLink.");
  55. offMeshLink.bidirectional = false;
  56. offMeshLink.startTransform = start;
  57. offMeshLink.endTransform = end;
  58. // we modified the endpoint references above - now explicitly update positions.
  59. offMeshLink.UpdateLink();
  60. }
  61. [UnityTearDown]
  62. public IEnumerator UnityTearDown()
  63. {
  64. Object.DestroyImmediate(m_LinkGO);
  65. yield return SceneManager.UnloadSceneAsync(k_SceneName);
  66. }
  67. }
  68. }