コード例 #1
0
        void SaveCombinedAsset()
        {
            // Create an asset describing all surfaces.
            var targetPath        = GetAndEnsureTargetPath(this);
            var combinedAssetData = new List <NavMeshData>();

            // Collect valid navmesh data.
            foreach (var s in NavMeshSurface.activeSurfaces)
            {
                if (s.bakedNavMeshData != null)
                {
                    combinedAssetData.Add(s.bakedNavMeshData);
                }
            }

            // Clear the legacy navmesh data reference on the scene
            if (NavMeshEditorHelpers.CurrentNavMeshAssetFormat() == NavMeshEditorHelpers.NavMeshDataFormat.Single)
            {
                NavMeshBuilder.ClearAllNavMeshes();
            }

            // Store baked navmesh data in one asset.
            var combinedAssetPath = Path.Combine(targetPath, "NavMesh.asset");

            if (combinedAssetData.Count == 0)
            {
                AssetDatabase.DeleteAsset(combinedAssetPath);
            }
            else
            {
                if (!File.Exists(combinedAssetPath))
                {
                    // NOTE: Creates empty dummy NavMeshData to represent the main asset,
                    // this is done in order to show all the built meshes equally in the list.
                    var dummy = new NavMeshData();
                    AssetDatabase.CreateAsset(dummy, combinedAssetPath);
                }

                // Only add the data that has not been added already.
                // Call to NavMeshSurface.Clear () also deletes the data from the asset.
                foreach (var data in combinedAssetData)
                {
                    if (!AssetDatabase.Contains(data))
                    {
                        AssetDatabase.AddObjectToAsset(data, combinedAssetPath);
                    }
                }
                AssetDatabase.SaveAssets();
                AssetDatabase.ImportAsset(combinedAssetPath);
            }
        }
コード例 #2
0
        public static void BuildNavMeshForMultipleScenes(string[] paths)
        {
            if (paths.Length == 0)
            {
                return;
            }

            for (int i = 0; i < paths.Length; i++)
            {
                for (int j = i + 1; j < paths.Length; j++)
                {
                    if (paths[i] == paths[j])
                    {
                        throw new System.Exception("No duplicate scene names are allowed");
                    }
                }
            }

            if (!EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
            {
                return;
            }

            if (!EditorSceneManager.OpenScene(paths[0]).IsValid())
            {
                throw new System.Exception("Could not open scene: " + paths[0]);
            }
            for (int i = 1; i < paths.Length; ++i)
            {
                EditorSceneManager.OpenScene(paths[i], OpenSceneMode.Additive);
            }

            NavMeshBuilder.BuildNavMesh();
            Object asset = NavMeshBuilder.sceneNavMeshData;

            for (int i = 0; i < paths.Length; ++i)
            {
                if (EditorSceneManager.OpenScene(paths[i]).IsValid())
                {
                    NavMeshBuilder.sceneNavMeshData = asset;
                    EditorSceneManager.SaveScene(SceneManager.GetActiveScene());
                }
            }
        }
コード例 #3
0
        static void BakeButtons()
        {
            const float kButtonWidth = 95;

            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();

            bool wasEnabled = GUI.enabled;

            GUI.enabled &= !Application.isPlaying;
            if (GUILayout.Button("Clear", GUILayout.Width(kButtonWidth)))
            {
                EditorNavMeshBuilder.ClearAllNavMeshes();
            }
            GUI.enabled = wasEnabled;

            if (EditorNavMeshBuilder.isRunning)
            {
                if (GUILayout.Button("Cancel", GUILayout.Width(kButtonWidth)))
                {
                    EditorNavMeshBuilder.Cancel();
                }
            }
            else
            {
                wasEnabled   = GUI.enabled;
                GUI.enabled &= !Application.isPlaying;
                if (GUILayout.Button("Bake", GUILayout.Width(kButtonWidth)))
                {
                    EditorNavMeshBuilder.BuildNavMeshAsync();
                }
                GUI.enabled = wasEnabled;
            }

            GUILayout.EndHorizontal();

            EditorGUILayout.Space();
        }
コード例 #4
0
 public static void BuildNavMeshForMultipleScenes(string[] paths)
 {
     if (paths.Length != 0)
     {
         for (int i = 0; i < paths.Length; i++)
         {
             for (int j = i + 1; j < paths.Length; j++)
             {
                 if (paths[i] == paths[j])
                 {
                     throw new Exception("No duplicate scene names are allowed");
                 }
             }
         }
         if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
         {
             if (!EditorSceneManager.OpenScene(paths[0]).IsValid())
             {
                 throw new Exception("Could not open scene: " + paths[0]);
             }
             for (int k = 1; k < paths.Length; k++)
             {
                 EditorSceneManager.OpenScene(paths[k], OpenSceneMode.Additive);
             }
             NavMeshBuilder.BuildNavMesh();
             UnityEngine.Object sceneNavMeshData = NavMeshBuilder.sceneNavMeshData;
             for (int l = 0; l < paths.Length; l++)
             {
                 if (EditorSceneManager.OpenScene(paths[l]).IsValid())
                 {
                     NavMeshBuilder.sceneNavMeshData = sceneNavMeshData;
                     EditorSceneManager.SaveScene(SceneManager.GetActiveScene());
                 }
             }
         }
     }
 }