public IEnumerator Setup()
    {
        var plane = GameObject.CreatePrimitive(PrimitiveType.Plane);

        plane.name = "NavMeshSurface" + (++m_TestCounter) + "Prefab";
        var surface = plane.AddComponent <NavMeshSurface>();

        surface.collectObjects = CollectObjects.Children;

        m_PrefabPath = Path.Combine(m_TempFolder, plane.name + ".prefab");
        var planePrefab = PrefabUtility.SaveAsPrefabAsset(plane, m_PrefabPath);

        Object.DestroyImmediate(plane);

        AssetDatabase.OpenAsset(planePrefab);
        var prefabStage   = PrefabStageUtility.GetCurrentPrefabStage();
        var prefabSurface = prefabStage.prefabContentsRoot.GetComponent <NavMeshSurface>();

        yield return(BakeNavMeshAsync(() => prefabSurface, k_PrefabDefaultArea));

        PrefabSavingUtil.SavePrefab(prefabStage);
        StageUtility.GoToMainStage();

        NavMesh.RemoveAllNavMeshData();

        yield return(null);
    }
    public IEnumerator NavMeshSurfacePrefab_WhenEmptyAndInstantiated_InstanceHasEmptyNavMeshData()
    {
        var prefab   = AssetDatabase.LoadAssetAtPath <GameObject>(m_PrefabPath);
        var instance = PrefabUtility.InstantiatePrefab(prefab) as GameObject;

        Assert.IsNotNull(instance);
        instance.name = "Surface" + m_TestCounter + "PrefabInstance";
        var instanceSurface = instance.GetComponent <NavMeshSurface>();

        Assert.IsTrue(instanceSurface.navMeshData != null, "NavMeshSurface in prefab instance must have NavMeshData.");

        AssetDatabase.OpenAsset(prefab);
        var prefabStage   = PrefabStageUtility.GetCurrentPrefabStage();
        var prefabSurface = prefabStage.prefabContentsRoot.GetComponent <NavMeshSurface>();

        NavMeshAssetManager.instance.ClearSurfaces(new Object[] { prefabSurface });
        PrefabSavingUtil.SavePrefab(prefabStage);

        StageUtility.GoToMainStage();
        Assert.IsTrue(instanceSurface.navMeshData == null,
                      "After the NavMeshSurface in the prefab has been cleared the prefab instance should no longer hold NavMeshData.");
        var expectedAreaMask = 1 << k_PrefabDefaultArea;

        Assert.IsFalse(HasNavMeshAtPosition(Vector3.zero, expectedAreaMask));

#if !KEEP_ARTIFACTS_FOR_INSPECTION
        Object.DestroyImmediate(instance);
#endif
        yield return(null);
    }
    public IEnumerator NavMeshSurfacePrefab_AfterBakingInPrefabMode_LeavesMainSceneUntouched()
    {
        Assert.IsFalse(HasNavMeshAtPosition(Vector3.zero));

        var prefab = AssetDatabase.LoadAssetAtPath <GameObject>(m_PrefabPath);

        AssetDatabase.OpenAsset(prefab);
        var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();

        Assert.IsNotNull(prefabStage);
        Assert.IsNotNull(prefabStage.prefabContentsRoot);

        var prefabSurface            = prefabStage.prefabContentsRoot.GetComponent <NavMeshSurface>();
        var initialPrefabNavMeshData = prefabSurface.navMeshData;

        yield return(BakeNavMeshAsync(() => prefabSurface, k_RedArea));

        Assert.AreNotSame(initialPrefabNavMeshData, prefabSurface.navMeshData);

        PrefabSavingUtil.SavePrefab(prefabStage);
        StageUtility.GoToMainStage();

        Assert.IsFalse(HasNavMeshAtPosition(Vector3.zero, NavMesh.AllAreas, 0, 1000.0f));

        yield return(null);
    }
    public IEnumerator NavMeshSurfacePrefab_AfterBakingInPrefabMode_DoesNotActivateItsNavMesh()
    {
        var prefab = AssetDatabase.LoadAssetAtPath <GameObject>(m_PrefabPath);

        AssetDatabase.OpenAsset(prefab);
        var prefabStage   = PrefabStageUtility.GetCurrentPrefabStage();
        var prefabSurface = prefabStage.prefabContentsRoot.GetComponent <NavMeshSurface>();

        NavMeshAssetManager.instance.ClearSurfaces(new Object[] { prefabSurface });
        PrefabSavingUtil.SavePrefab(prefabStage);

        yield return(BakeNavMeshAsync(() => prefabSurface, k_RedArea));

        NavMeshHit hit;

        NavMesh.SamplePosition(Vector3.zero, out hit, 1000000f, new NavMeshQueryFilter {
            areaMask = NavMesh.AllAreas, agentTypeID = 0
        });
        Assert.That(hit.hit, Is.False, "The NavMesh instance of a prefab opened for edit should not be active after baking the surface.");

        prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
        PrefabSavingUtil.SavePrefab(prefabStage);

        NavMesh.SamplePosition(Vector3.zero, out hit, 1000000f, new NavMeshQueryFilter {
            areaMask = NavMesh.AllAreas, agentTypeID = 0
        });
        Assert.That(hit.hit, Is.False, "The NavMesh instance of a prefab opened for edit should not be active after baking the surface.");
    }
예제 #5
0
    public IEnumerator NavMeshSurfacePrefabVariant_WhenCustomizedAndRebaked_OldAssetDiscardedAndParentAssetUnchanged()
    {
        var prefabVariant         = AssetDatabase.LoadAssetAtPath <GameObject>(m_PrefabVariantPath);
        var theOriginalPrefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(prefabVariant);
        var theOriginalPrefab     = AssetDatabase.LoadAssetAtPath <GameObject>(theOriginalPrefabPath);

        AssetDatabase.OpenAsset(theOriginalPrefab);
        var theOriginalPrefabStage       = PrefabStageUtility.GetCurrentPrefabStage();
        var theOriginalPrefabSurface     = theOriginalPrefabStage.prefabContentsRoot.GetComponent <NavMeshSurface>();
        var theOriginalPrefabNavMeshData = theOriginalPrefabSurface.navMeshData;
        var theOriginalPrefabAssetPath   = AssetDatabase.GetAssetPath(theOriginalPrefabSurface.navMeshData);

        Assert.IsTrue(theOriginalPrefabNavMeshData != null, "Original prefab must have some NavMeshData.");
        Assert.IsTrue(File.Exists(theOriginalPrefabAssetPath), "NavMeshData file must exist for the original prefab. ({0})", theOriginalPrefabAssetPath);

        AssetDatabase.OpenAsset(prefabVariant);
        var prefabVariantStage   = PrefabStageUtility.GetCurrentPrefabStage();
        var prefabVariantSurface = prefabVariantStage.prefabContentsRoot.GetComponent <NavMeshSurface>();

        yield return(BakeNavMeshAsync(() => prefabVariantSurface, k_GrayArea));

        PrefabSavingUtil.SavePrefab(prefabVariantStage);

        var modifiedVariantNavMeshData = prefabVariantSurface.navMeshData;
        var modifiedVariantAssetPath   = AssetDatabase.GetAssetPath(prefabVariantSurface.navMeshData);

        Assert.IsTrue(modifiedVariantNavMeshData != null, "Prefab must have some NavMeshData.");
        Assert.IsTrue(File.Exists(modifiedVariantAssetPath), "NavMeshData file for modifier variant must exist. ({0})", modifiedVariantAssetPath);
        Assert.AreNotEqual(theOriginalPrefabNavMeshData, modifiedVariantNavMeshData, "Modified variant must have a NavMeshData different than that of the original prefab.");

        yield return(BakeNavMeshAsync(() => prefabVariantSurface, k_OrangeArea));

        Assert.IsTrue(modifiedVariantNavMeshData != null, "The initial NavMeshData of a modified variant must still exist immediately after prefab variant re-bake.");
        Assert.IsTrue(File.Exists(modifiedVariantAssetPath), "The initial NavMeshData file of a modified variant must exist after prefab variant re-bake. ({0})", modifiedVariantAssetPath);

        Assert.IsTrue(prefabVariantSurface.navMeshData != null, "NavMeshSurface must have NavMeshData after baking.");
        var unsavedRebakedNavMeshData = prefabVariantSurface.navMeshData;

        yield return(BakeNavMeshAsync(() => prefabVariantSurface, k_RedArea));

        Assert.IsTrue(unsavedRebakedNavMeshData == null, "An unsaved NavMeshData should not exist after a re-bake.");
        Assert.IsTrue(prefabVariantSurface.navMeshData != null, "NavMeshSurface must have NavMeshData after baking.");

        PrefabSavingUtil.SavePrefab(prefabVariantStage);
        var theNewVariantNavMeshData = prefabVariantSurface.navMeshData;
        var theNewVariantAssetPath   = AssetDatabase.GetAssetPath(theNewVariantNavMeshData);

        Assert.IsTrue(modifiedVariantNavMeshData == null, "Initial NavMeshData of the modified variant must no longer exist after saving the variant.");
        // ReSharper disable once HeuristicUnreachableCode - modifiedVariantNavMeshData is affected by BakeNavMeshAsync()
        Assert.IsFalse(File.Exists(modifiedVariantAssetPath), "Initial NavMeshData file of the modified and saved variant must no longer exist after saving the variant. ({0})", modifiedVariantAssetPath);
        Assert.IsTrue(File.Exists(theNewVariantAssetPath), "Variant's own NavMeshData exists in a file after saving. ({0})", theNewVariantAssetPath);
        Assert.IsTrue(File.Exists(theOriginalPrefabAssetPath), "NavMeshData file of the original prefab still exists after saving the variant. ({0})", theOriginalPrefabAssetPath);
        Assert.AreNotEqual(theOriginalPrefabNavMeshData, theNewVariantNavMeshData, "Re-baked modified variant must have a NavMeshData different than that of the original prefab.");

        StageUtility.GoToMainStage();

        yield return(null);
    }
예제 #6
0
    public IEnumerator NavMeshSurfacePrefabVariant_WhenFreshAndRebaked_ParentAssetUnchanged()
    {
        var theOriginalPrefab = AssetDatabase.LoadAssetAtPath <GameObject>(m_PrefabPath);

        AssetDatabase.OpenAsset(theOriginalPrefab);
        var theOriginalPrefabStage       = PrefabStageUtility.GetCurrentPrefabStage();
        var theOriginalPrefabSurface     = theOriginalPrefabStage.prefabContentsRoot.GetComponent <NavMeshSurface>();
        var theOriginalPrefabNavMeshData = theOriginalPrefabSurface.navMeshData;
        var theOriginalPrefabAssetPath   = AssetDatabase.GetAssetPath(theOriginalPrefabSurface.navMeshData);

        Assert.IsTrue(theOriginalPrefabNavMeshData != null, "Original prefab must have some NavMeshData.");
        Assert.IsTrue(File.Exists(theOriginalPrefabAssetPath), "NavMeshData file must exist for the original prefab. ({0})", theOriginalPrefabAssetPath);

        var prefabVariant = AssetDatabase.LoadAssetAtPath <GameObject>(m_PrefabVariantPath);

        AssetDatabase.OpenAsset(prefabVariant);
        var prefabVariantStage        = PrefabStageUtility.GetCurrentPrefabStage();
        var prefabVariantSurface      = prefabVariantStage.prefabContentsRoot.GetComponent <NavMeshSurface>();
        var initialVariantNavMeshData = prefabVariantSurface.navMeshData;
        var initialVariantAssetPath   = AssetDatabase.GetAssetPath(prefabVariantSurface.navMeshData);

        Assert.AreEqual(theOriginalPrefabNavMeshData, initialVariantNavMeshData, "Fresh variant must have the same NavMeshData as the original prefab.");

        Assert.IsTrue(initialVariantNavMeshData != null, "Prefab must have some NavMeshData.");
        Assert.IsTrue(File.Exists(initialVariantAssetPath), "NavMeshData file must exist. ({0})", initialVariantAssetPath);

        yield return(BakeNavMeshAsync(() => prefabVariantSurface, k_GrayArea));

        Assert.IsTrue(initialVariantNavMeshData != null, "The initial NavMeshData (from original prefab) must still exist immediately after prefab variant re-bake.");
        Assert.IsTrue(File.Exists(initialVariantAssetPath), "The initial NavMeshData file (from original prefab) must exist after prefab variant re-bake. ({0})", initialVariantAssetPath);

        Assert.IsTrue(prefabVariantSurface.navMeshData != null, "NavMeshSurface must have NavMeshData after baking.");
        var unsavedRebakedNavMeshData = prefabVariantSurface.navMeshData;

        yield return(BakeNavMeshAsync(() => prefabVariantSurface, k_BrownArea));

        Assert.IsTrue(unsavedRebakedNavMeshData == null, "An unsaved NavMeshData should not exist after a re-bake.");
        Assert.IsTrue(prefabVariantSurface.navMeshData != null, "NavMeshSurface must have NavMeshData after baking.");

        PrefabSavingUtil.SavePrefab(prefabVariantStage);

        var theNewVariantNavMeshData = prefabVariantSurface.navMeshData;
        var theNewVariantAssetPath   = AssetDatabase.GetAssetPath(theNewVariantNavMeshData);

        Assert.IsTrue(File.Exists(theNewVariantAssetPath), "Variant's own NavMeshData exists in a file after saving. ({0})", theNewVariantAssetPath);
        Assert.IsTrue(File.Exists(theOriginalPrefabAssetPath), "NavMeshData file of the original prefab still exists after saving the variant. ({0})", theOriginalPrefabAssetPath);
        Assert.IsTrue(theOriginalPrefabNavMeshData != null, "Original prefab must still have NavMeshData.");

        StageUtility.GoToMainStage();

        yield return(null);
    }
    public IEnumerator ModifierVolume_WhenInsidePrefabMode_ModifiesTheNavMeshInPrefab()
    {
        var prefab   = AssetDatabase.LoadAssetAtPath <GameObject>(m_PrefabPath);
        var instance = PrefabUtility.InstantiatePrefab(prefab) as GameObject;

        Assert.IsNotNull(instance);
        instance.name = "SurfaceSeekingModVol" + m_TestCounter + "PrefabInstance";

        NavMeshHit hit;
        var        filter = new NavMeshQueryFilter {
            agentTypeID = 0, areaMask = NavMesh.AllAreas
        };

        NavMesh.SamplePosition(Vector3.zero, out hit, 0.1f, filter);
        Assert.That(hit.hit, Is.False, "Prefab should not have a NavMesh in the beginning.");

        AssetDatabase.OpenAsset(prefab);
        var prefabStage    = PrefabStageUtility.GetCurrentPrefabStage();
        var prefabSurface  = prefabStage.prefabContentsRoot.GetComponent <NavMeshSurface>();
        var modifierVolume = prefabStage.prefabContentsRoot.AddComponent <NavMeshModifierVolume>();

        modifierVolume.area   = k_RedArea;
        modifierVolume.center = Vector3.zero;
        modifierVolume.size   = Vector3.one;
        yield return(BakeNavMeshAsync(() => prefabSurface, k_PrefabDefaultArea));

        PrefabSavingUtil.SavePrefab(prefabStage);
        StageUtility.GoToMainStage();

        NavMeshHit hitCenter;

        NavMesh.SamplePosition(Vector3.zero, out hitCenter, 0.1f, filter);
        Assert.That(hitCenter.hit, Is.True, "A NavMesh should have been baked in the center of the prefab.");
        Assert.That(hitCenter.mask, Is.EqualTo(1 << k_RedArea),
                    "Area type (0x{0:x8}) found in the center should be 0x{1:x8}.", hitCenter.mask, 1 << k_RedArea);

        NavMeshHit hitSides;

        NavMesh.SamplePosition(new Vector3(0.6f, 0, 0.6f), out hitSides, 0.1f, filter);
        Assert.That(hitSides.hit, Is.True, "A NavMesh should have been baked in the outer sides of the prefab.");
        Assert.That(hitSides.mask, Is.EqualTo(1 << k_PrefabDefaultArea),
                    "Area type (0x{0:x8}) found on the sides should be 0x{1:x8}.", hitSides.mask, 1 << k_PrefabDefaultArea);

        Assert.That(hitCenter.mask, Is.Not.EqualTo(hitSides.mask),
                    "Area type (0x{0:x8}) in the center should be different than on the sides.", hitCenter.mask);

        EditorSceneManager.SaveScene(SceneManager.GetActiveScene());

#if !KEEP_ARTIFACTS_FOR_INSPECTION
        Object.DestroyImmediate(instance);
#endif
    }
    public IEnumerator NavMeshSurfacePrefab_AfterModifiedInstanceAppliedBack_UpdatedAccordingToInstance()
    {
        var prefab      = AssetDatabase.LoadAssetAtPath <GameObject>(m_PrefabPath);
        var instanceOne = PrefabUtility.InstantiatePrefab(prefab) as GameObject;

        Assert.IsNotNull(instanceOne);
        instanceOne.name = "Surface" + m_TestCounter + "PrefabInstanceOne";
        TestNavMeshExistsAloneAtPosition(k_PrefabDefaultArea, Vector3.zero);

        var instanceTwo = PrefabUtility.InstantiatePrefab(prefab) as GameObject;

        Assert.IsNotNull(instanceTwo);
        instanceTwo.name = "Surface" + m_TestCounter + "PrefabInstanceTwo";
        // reactivate the object to apply the change of position immediately
        instanceTwo.SetActive(false);
        instanceTwo.transform.position = new Vector3(20, 0, 0);
        instanceTwo.SetActive(true);

        var instanceOneSurface = instanceOne.GetComponent <NavMeshSurface>();

        Assert.IsNotNull(instanceOneSurface);

        yield return(BakeNavMeshAsync(() => instanceOneSurface, k_RedArea));

        TestNavMeshExistsAloneAtPosition(k_RedArea, Vector3.zero);
        TestNavMeshExistsAloneAtPosition(k_PrefabDefaultArea, instanceTwo.transform.position);

        PrefabUtility.ApplyPrefabInstance(instanceOne, InteractionMode.AutomatedAction);

        TestNavMeshExistsAloneAtPosition(k_RedArea, instanceTwo.transform.position);

        AssetDatabase.OpenAsset(prefab);
        var prefabStage   = PrefabStageUtility.GetCurrentPrefabStage();
        var prefabSurface = prefabStage.prefabContentsRoot.GetComponent <NavMeshSurface>();

        yield return(BakeNavMeshAsync(() => prefabSurface, k_GrayArea));

        PrefabSavingUtil.SavePrefab(prefabStage);
        StageUtility.GoToMainStage();

        TestNavMeshExistsAloneAtPosition(k_GrayArea, Vector3.zero);
        TestNavMeshExistsAloneAtPosition(k_GrayArea, instanceTwo.transform.position);

#if !KEEP_ARTIFACTS_FOR_INSPECTION
        Object.DestroyImmediate(instanceOne);
        Object.DestroyImmediate(instanceTwo);
#endif
        yield return(null);
    }
    public IEnumerator ModifierVolume_WhenInsidePrefabMode_DoesNotAffectTheNavMeshInMainScene()
    {
        var go = GameObject.CreatePrimitive(PrimitiveType.Plane);

        go.name = "SurfaceOutsidePrefab";
        var mainSceneSurface = go.AddComponent <NavMeshSurface>();

        mainSceneSurface.defaultArea    = k_PinkArea;
        mainSceneSurface.agentTypeID    = 0;
        mainSceneSurface.collectObjects = CollectObjects.All;

        NavMeshHit hit;
        var        filter = new NavMeshQueryFilter {
            agentTypeID = 0, areaMask = NavMesh.AllAreas
        };

        NavMesh.SamplePosition(Vector3.zero, out hit, 0.1f, filter);
        Assert.That(hit.hit, Is.False, "Prefab should not have a NavMesh in the beginning.");

        EditorSceneManager.SaveScene(SceneManager.GetActiveScene());

        var prefab = AssetDatabase.LoadAssetAtPath <GameObject>(m_PrefabPath);

        AssetDatabase.OpenAsset(prefab);
        var prefabStage  = PrefabStageUtility.GetCurrentPrefabStage();
        var prefabModVol = prefabStage.prefabContentsRoot.AddComponent <NavMeshModifierVolume>();

        prefabModVol.area   = k_PrefabDefaultArea;
        prefabModVol.center = Vector3.zero;
        prefabModVol.size   = new Vector3(100, 100, 100);

        // bake the NavMeshSurface from the main scene while the prefab mode is open
        yield return(BakeNavMeshAsync(() => mainSceneSurface, mainSceneSurface.defaultArea));

        PrefabSavingUtil.SavePrefab(prefabStage);
        EditorSceneManager.SaveScene(SceneManager.GetActiveScene());
        StageUtility.GoToMainStage();

        NavMesh.SamplePosition(Vector3.zero, out hit, 0.1f, filter);
        Assert.That(hit.hit, Is.True, "A NavMesh should have been baked by the surface in the main scene.");
        Assert.That(hit.mask, Is.EqualTo(1 << mainSceneSurface.defaultArea),
                    "NavMesh has the area type 0x{0:x8} instead of the expected 0x{1:x8}.", hit.mask,
                    1 << mainSceneSurface.defaultArea);

#if !KEEP_ARTIFACTS_FOR_INSPECTION
        Object.DestroyImmediate(go);
#endif
    }
    public IEnumerator NavMeshSurfacePrefab_WhenRebakedButInstanceModified_DoesNotChangeInstance()
    {
        var prefab   = AssetDatabase.LoadAssetAtPath <GameObject>(m_PrefabPath);
        var instance = PrefabUtility.InstantiatePrefab(prefab) as GameObject;

        Assert.IsNotNull(instance);
        instance.name = "Surface" + m_TestCounter + "PrefabInstance";

        var instanceSurface = instance.GetComponent <NavMeshSurface>();

        Assert.IsNotNull(instanceSurface);
        yield return(BakeNavMeshAsync(() => instanceSurface, k_RedArea));

        var instanceNavMeshData = instanceSurface.navMeshData;

        TestNavMeshExistsAloneAtPosition(k_RedArea, Vector3.zero);

        AssetDatabase.OpenAsset(prefab);
        var prefabStage              = PrefabStageUtility.GetCurrentPrefabStage();
        var prefabSurface            = prefabStage.prefabContentsRoot.GetComponent <NavMeshSurface>();
        var initialPrefabNavMeshData = prefabSurface.navMeshData;

        yield return(BakeNavMeshAsync(() => prefabSurface, k_GrayArea));

        PrefabSavingUtil.SavePrefab(prefabStage);
        StageUtility.GoToMainStage();

        AssetDatabase.OpenAsset(prefab);
        var prefabStageReopened   = PrefabStageUtility.GetCurrentPrefabStage();
        var prefabSurfaceReopened = prefabStageReopened.prefabContentsRoot.GetComponent <NavMeshSurface>();
        var prefabNavMeshData     = prefabSurfaceReopened.navMeshData;

        Assert.IsTrue(prefabNavMeshData != null,
                      "NavMeshSurface in prefab must have NavMeshData after baking, saving, closing and reopening.");
        Assert.AreNotSame(instanceNavMeshData, prefabNavMeshData);
        Assert.AreNotSame(initialPrefabNavMeshData, prefabNavMeshData);

        StageUtility.GoToMainStage();
        Assert.AreSame(instanceNavMeshData, instanceSurface.navMeshData);

#if !KEEP_ARTIFACTS_FOR_INSPECTION
        Object.DestroyImmediate(instance);
#endif
        yield return(null);
    }
예제 #11
0
    public IEnumerator NavMeshSurfacePrefab_WhenRebaked_TheOldAssetExistsUntilSavingAndNotAfter()
    {
        var prefab = AssetDatabase.LoadAssetAtPath <GameObject>(m_PrefabPath);

        AssetDatabase.OpenAsset(prefab);
        var prefabStage        = PrefabStageUtility.GetCurrentPrefabStage();
        var prefabSurface      = prefabStage.prefabContentsRoot.GetComponent <NavMeshSurface>();
        var initialNavMeshData = prefabSurface.navMeshData;
        var initialAssetPath   = AssetDatabase.GetAssetPath(prefabSurface.navMeshData);

        // Assert.IsNull cannot verify correctly that an UnityEngine.Object is null
        Assert.IsTrue(initialNavMeshData != null, "Prefab must have some NavMeshData.");
        Assert.IsTrue(File.Exists(initialAssetPath), "NavMeshData file must exist. ({0})", initialAssetPath);

        yield return(BakeNavMeshAsync(() => prefabSurface, k_GrayArea));

        Assert.IsTrue(initialNavMeshData != null,
                      "The initial NavMeshData must still exist immediately after prefab re-bake.");
        Assert.IsTrue(File.Exists(initialAssetPath),
                      "The initial NavMeshData file must exist after prefab re-bake. ({0})", initialAssetPath);

        Assert.IsTrue(prefabSurface.navMeshData != null, "NavMeshSurface must have NavMeshData after baking.");
        var unsavedRebakedNavMeshData = prefabSurface.navMeshData;

        yield return(BakeNavMeshAsync(() => prefabSurface, k_OrangeArea));

        Assert.IsTrue(unsavedRebakedNavMeshData == null, "An unsaved NavMeshData should not exist after a re-bake.");
        Assert.IsTrue(prefabSurface.navMeshData != null, "NavMeshSurface must have NavMeshData after baking.");

        PrefabSavingUtil.SavePrefab(prefabStage);
        Assert.IsFalse(File.Exists(initialAssetPath), "NavMeshData file still exists after saving. ({0})",
                       initialAssetPath);
        Assert.IsTrue(initialNavMeshData == null,
                      "The initial NavMeshData must no longer exist after saving the prefab.");

        // ReSharper disable once HeuristicUnreachableCode - initialNavMeshData is affected by BakeNavMeshAsync()
        StageUtility.GoToMainStage();

        yield return(null);
    }
예제 #12
0
    public IEnumerator Setup()
    {
        var plane = GameObject.CreatePrimitive(PrimitiveType.Plane);

        plane.name = "NavMeshSurfacePrefab" + (++m_TestCounter);
        var surface = plane.AddComponent <NavMeshSurface>();

        surface.collectObjects = CollectObjects.Children;

        m_PrefabPath        = Path.Combine(m_TempFolder, plane.name + ".prefab");
        m_PrefabVariantPath = Path.Combine(m_TempFolder, plane.name + "Variant.prefab");

        var planePrefab = PrefabUtility.SaveAsPrefabAsset(plane, m_PrefabPath);

        Object.DestroyImmediate(plane);

        AssetDatabase.OpenAsset(planePrefab);
        var theOriginalPrefabStage   = PrefabStageUtility.GetCurrentPrefabStage();
        var theOriginalPrefabSurface = theOriginalPrefabStage.prefabContentsRoot.GetComponent <NavMeshSurface>();

        yield return(BakeNavMeshAsync(() => theOriginalPrefabSurface, k_PrefabDefaultArea));

        PrefabSavingUtil.SavePrefab(theOriginalPrefabStage);
        StageUtility.GoToMainStage();

        var instanceForVariant = PrefabUtility.InstantiatePrefab(planePrefab) as GameObject;

        PrefabUtility.SaveAsPrefabAsset(instanceForVariant, m_PrefabVariantPath);

#if !KEEP_ARTIFACTS_FOR_INSPECTION
        Object.DestroyImmediate(instanceForVariant);
#endif
        NavMesh.RemoveAllNavMeshData();

        yield return(null);
    }
    public IEnumerator NavMeshSurfacePrefab_WhenBakingInPreviewScene_CollectsOnlyPreviewSceneObjects()
    {
        var mainScenePlane = GameObject.CreatePrimitive(PrimitiveType.Plane);

        mainScenePlane.transform.localScale = new Vector3(100, 1, 100);

        var prefab = AssetDatabase.LoadAssetAtPath <GameObject>(m_PrefabPath);

        AssetDatabase.OpenAsset(prefab);
        var prefabStage   = PrefabStageUtility.GetCurrentPrefabStage();
        var prefabSurface = prefabStage.prefabContentsRoot.GetComponent <NavMeshSurface>();

        prefabSurface.collectObjects = CollectObjects.All;
        yield return(BakeNavMeshAsync(() => prefabSurface, k_RedArea));

        PrefabSavingUtil.SavePrefab(prefabStage);
        StageUtility.GoToMainStage();

        var instance = PrefabUtility.InstantiatePrefab(prefab) as GameObject;

        Assert.IsNotNull(instance);

        TestNavMeshExistsAloneAtPosition(k_RedArea, Vector3.zero);

        var posNearby = new Vector3(20, 0, 0);

        Assert.IsFalse(HasNavMeshAtPosition(posNearby, 1 << k_RedArea),
                       "NavMesh with the prefab's area exists at position {1}, outside the prefab's plane. ({0})",
                       k_RedArea, posNearby);

#if !KEEP_ARTIFACTS_FOR_INSPECTION
        Object.DestroyImmediate(instance);
        Object.DestroyImmediate(mainScenePlane);
#endif
        yield return(null);
    }