No Description
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.

ConverterTests.cs 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #if UNITY_2022_2_OR_NEWER
  2. using NUnit.Framework;
  3. using UnityEditor;
  4. using UnityEditor.SceneManagement;
  5. using UnityEngine;
  6. using UnityEngine.AI;
  7. using Unity.AI.Navigation.Updater;
  8. using NavMeshBuilder = UnityEditor.AI.NavMeshBuilder;
  9. namespace Unity.AI.Navigation.Editor.Tests
  10. {
  11. [Description("Tests suite related to the systems used to convert editor data from the legacy NavMesh systems to the modern component-based navigation extension")]
  12. class ConverterTests
  13. {
  14. const string k_RootFolder = "Assets";
  15. const string k_TestFolder = "ConverterTests";
  16. const string k_TestFolderPath = k_RootFolder + "/" + k_TestFolder;
  17. const string k_TestScenePath = k_TestFolderPath + "/ConverterTestsScene.unity";
  18. const string k_BuildHeightMeshPropertyName = "m_BuildSettings.buildHeightMesh";
  19. bool m_BuildHeightMeshPreviousValue;
  20. [OneTimeSetUp]
  21. public void OneTimeSetUp()
  22. {
  23. if (!AssetDatabase.IsValidFolder(k_TestFolderPath))
  24. AssetDatabase.CreateFolder(k_RootFolder, k_TestFolder);
  25. Assume.That(AssetDatabase.IsValidFolder(k_TestFolderPath));
  26. var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
  27. var planeGameObject = GameObject.CreatePrimitive(PrimitiveType.Plane);
  28. #pragma warning disable 618
  29. GameObjectUtility.SetStaticEditorFlags(planeGameObject, StaticEditorFlags.NavigationStatic);
  30. #pragma warning restore 618
  31. EditorSceneManager.SaveScene(scene, k_TestScenePath);
  32. // Enable desired build settings (build HeightMesh)
  33. var settingsObject = new SerializedObject(NavMeshBuilder.navMeshSettingsObject);
  34. Assume.That(settingsObject, Is.Not.Null, "Unable to get the build settings object");
  35. var buildHeightMeshProperty = settingsObject.FindProperty(k_BuildHeightMeshPropertyName);
  36. Assume.That(buildHeightMeshProperty, Is.Not.Null, "Unable to get the buildHeightMesh property from the build settings object");
  37. m_BuildHeightMeshPreviousValue = buildHeightMeshProperty.boolValue;
  38. buildHeightMeshProperty.boolValue = true;
  39. settingsObject.ApplyModifiedProperties();
  40. Assume.That(buildHeightMeshProperty.boolValue, Is.True, "buildHeightMesh property from the build settings object should be true");
  41. NavMeshBuilder.BuildNavMesh();
  42. EditorSceneManager.SaveScene(scene, k_TestScenePath);
  43. Assume.That(NavMeshUpdaterUtility.IsSceneReferencingLegacyNavMesh(k_TestScenePath));
  44. NavMeshUpdaterUtility.ConvertScene(k_TestScenePath);
  45. }
  46. [Test]
  47. public void Converter_AfterConversion_SceneNavMeshAssetIsGone()
  48. {
  49. var navMeshOwnedByScene = NavMeshUpdaterUtility.IsSceneReferencingLegacyNavMesh(k_TestScenePath);
  50. Assert.IsFalse(navMeshOwnedByScene, "Converted scene should not own a NavMesh after conversion");
  51. }
  52. [Test]
  53. public void Converter_AfterConversion_NavMeshSurfaceIsPresent()
  54. {
  55. var surface = Object.FindAnyObjectByType<NavMeshSurface>();
  56. Assert.IsNotNull(surface, "Unable to find a NavMesh surface, it should have been created by the conversion");
  57. }
  58. [Test]
  59. public void Converter_AfterConversion_NavMeshIsPresent()
  60. {
  61. var sampleSuccess = NavMesh.SamplePosition(Vector3.zero, out var hit, 1.0f, NavMesh.AllAreas);
  62. Assert.IsTrue(sampleSuccess && hit.hit, "NavMesh should still be present after conversion");
  63. }
  64. [Test]
  65. public void Converter_AfterConversion_NoNavigationStaticGameObjects()
  66. {
  67. var gameObjects = Object.FindObjectsByType<GameObject>(FindObjectsSortMode.None);
  68. foreach (var gameObject in gameObjects)
  69. {
  70. #pragma warning disable 618
  71. Assert.IsFalse(GameObjectUtility.AreStaticEditorFlagsSet(gameObject, StaticEditorFlags.NavigationStatic), "Objects should not be flagged as NavigationStatic after conversion");
  72. #pragma warning restore 618
  73. }
  74. }
  75. [Test]
  76. public void Converter_AfterConversion_HeightMeshIsPresent()
  77. {
  78. var surface = Object.FindAnyObjectByType<NavMeshSurface>();
  79. Assume.That(surface, Is.Not.Null, "Unable to find a NavMesh surface, it should have been created by the conversion");
  80. Assert.IsTrue(surface.buildHeightMesh, "A scene NavMesh built with HeightMesh should be converted to a surface with the buildHeightMesh option enabled");
  81. }
  82. [OneTimeTearDown]
  83. public void OneTimeTearDown()
  84. {
  85. EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
  86. if (AssetDatabase.IsValidFolder(k_TestFolderPath))
  87. AssetDatabase.DeleteAsset(k_TestFolderPath);
  88. // Restore build settings value
  89. var settingsObject = new SerializedObject(NavMeshBuilder.navMeshSettingsObject);
  90. Assume.That(settingsObject, Is.Not.Null, "Unable to get the build settings object");
  91. var buildHeightMeshProperty = settingsObject.FindProperty(k_BuildHeightMeshPropertyName);
  92. Assume.That(buildHeightMeshProperty, Is.Not.Null, "Unable to get the buildHeightMesh property from the build settings object");
  93. buildHeightMeshProperty.boolValue = m_BuildHeightMeshPreviousValue;
  94. settingsObject.ApplyModifiedProperties();
  95. }
  96. }
  97. }
  98. #endif