|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using UnityEditor.Experimental.SceneManagement; |
| 4 | +using UnityEditor.SceneManagement; |
| 5 | +using UnityEngine.AI; |
| 6 | +using UnityEngine; |
| 7 | + |
| 8 | +namespace UnityEditor.AI |
| 9 | +{ |
| 10 | + public class NavMeshAssetManager : ScriptableSingleton<NavMeshAssetManager> |
| 11 | + { |
| 12 | + internal struct AsyncBakeOperation |
| 13 | + { |
| 14 | + public NavMeshSurface surface; |
| 15 | + public NavMeshData bakeData; |
| 16 | + public AsyncOperation bakeOperation; |
| 17 | + } |
| 18 | + |
| 19 | + List<AsyncBakeOperation> m_BakeOperations = new List<AsyncBakeOperation>(); |
| 20 | + internal List<AsyncBakeOperation> GetBakeOperations() { return m_BakeOperations; } |
| 21 | + |
| 22 | + struct SavedPrefabNavMeshData |
| 23 | + { |
| 24 | + public NavMeshSurface surface; |
| 25 | + public NavMeshData navMeshData; |
| 26 | + } |
| 27 | + |
| 28 | + List<SavedPrefabNavMeshData> m_PrefabNavMeshDataAssets = new List<SavedPrefabNavMeshData>(); |
| 29 | + |
| 30 | + static string GetAndEnsureTargetPath(NavMeshSurface surface) |
| 31 | + { |
| 32 | + // Create directory for the asset if it does not exist yet. |
| 33 | + var activeScenePath = surface.gameObject.scene.path; |
| 34 | + |
| 35 | + var targetPath = "Assets"; |
| 36 | + if (!string.IsNullOrEmpty(activeScenePath)) |
| 37 | + { |
| 38 | + targetPath = Path.Combine(Path.GetDirectoryName(activeScenePath), Path.GetFileNameWithoutExtension(activeScenePath)); |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + var prefabStage = PrefabStageUtility.GetPrefabStage(surface.gameObject); |
| 43 | + var isPartOfPrefab = prefabStage != null && prefabStage.IsPartOfPrefabContents(surface.gameObject); |
| 44 | + |
| 45 | + if (isPartOfPrefab) |
| 46 | + { |
| 47 | +#if UNITY_2020_1_OR_NEWER |
| 48 | + var assetPath = prefabStage.assetPath; |
| 49 | +#else |
| 50 | + var assetPath = prefabStage.prefabAssetPath; |
| 51 | +#endif |
| 52 | + if (!string.IsNullOrEmpty(assetPath)) |
| 53 | + { |
| 54 | + var prefabDirectoryName = Path.GetDirectoryName(assetPath); |
| 55 | + if (!string.IsNullOrEmpty(prefabDirectoryName)) |
| 56 | + targetPath = prefabDirectoryName; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + if (!Directory.Exists(targetPath)) |
| 61 | + Directory.CreateDirectory(targetPath); |
| 62 | + return targetPath; |
| 63 | + } |
| 64 | + |
| 65 | + static void CreateNavMeshAsset(NavMeshSurface surface) |
| 66 | + { |
| 67 | + var targetPath = GetAndEnsureTargetPath(surface); |
| 68 | + |
| 69 | + var combinedAssetPath = Path.Combine(targetPath, "NavMesh-" + surface.name + ".asset"); |
| 70 | + combinedAssetPath = AssetDatabase.GenerateUniqueAssetPath(combinedAssetPath); |
| 71 | + AssetDatabase.CreateAsset(surface.navMeshData, combinedAssetPath); |
| 72 | + } |
| 73 | + |
| 74 | + NavMeshData GetNavMeshAssetToDelete(NavMeshSurface navSurface) |
| 75 | + { |
| 76 | + if (PrefabUtility.IsPartOfPrefabInstance(navSurface) && !PrefabUtility.IsPartOfModelPrefab(navSurface)) |
| 77 | + { |
| 78 | + // Don't allow deleting the asset belonging to the prefab parent |
| 79 | + var parentSurface = PrefabUtility.GetCorrespondingObjectFromSource(navSurface) as NavMeshSurface; |
| 80 | + if (parentSurface && navSurface.navMeshData == parentSurface.navMeshData) |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + // Do not delete the NavMeshData asset referenced from a prefab until the prefab is saved |
| 85 | + var prefabStage = PrefabStageUtility.GetPrefabStage(navSurface.gameObject); |
| 86 | + var isPartOfPrefab = prefabStage != null && prefabStage.IsPartOfPrefabContents(navSurface.gameObject); |
| 87 | + if (isPartOfPrefab && IsCurrentPrefabNavMeshDataStored(navSurface)) |
| 88 | + return null; |
| 89 | + |
| 90 | + return navSurface.navMeshData; |
| 91 | + } |
| 92 | + |
| 93 | + void ClearSurface(NavMeshSurface navSurface) |
| 94 | + { |
| 95 | + var hasNavMeshData = navSurface.navMeshData != null; |
| 96 | + StoreNavMeshDataIfInPrefab(navSurface); |
| 97 | + |
| 98 | + var assetToDelete = GetNavMeshAssetToDelete(navSurface); |
| 99 | + navSurface.RemoveData(); |
| 100 | + |
| 101 | + if (hasNavMeshData) |
| 102 | + { |
| 103 | + SetNavMeshData(navSurface, null); |
| 104 | + EditorSceneManager.MarkSceneDirty(navSurface.gameObject.scene); |
| 105 | + } |
| 106 | + |
| 107 | + if (assetToDelete) |
| 108 | + AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(assetToDelete)); |
| 109 | + } |
| 110 | + |
| 111 | + public void StartBakingSurfaces(UnityEngine.Object[] surfaces) |
| 112 | + { |
| 113 | + // Remove first to avoid double registration of the callback |
| 114 | + EditorApplication.update -= UpdateAsyncBuildOperations; |
| 115 | + EditorApplication.update += UpdateAsyncBuildOperations; |
| 116 | + |
| 117 | + foreach (NavMeshSurface surf in surfaces) |
| 118 | + { |
| 119 | + StoreNavMeshDataIfInPrefab(surf); |
| 120 | + |
| 121 | + var oper = new AsyncBakeOperation(); |
| 122 | + |
| 123 | + oper.bakeData = InitializeBakeData(surf); |
| 124 | + oper.bakeOperation = surf.UpdateNavMesh(oper.bakeData); |
| 125 | + oper.surface = surf; |
| 126 | + |
| 127 | + m_BakeOperations.Add(oper); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + static NavMeshData InitializeBakeData(NavMeshSurface surface) |
| 132 | + { |
| 133 | + var emptySources = new List<NavMeshBuildSource>(); |
| 134 | + var emptyBounds = new Bounds(); |
| 135 | + return UnityEngine.AI.NavMeshBuilder.BuildNavMeshData(surface.GetBuildSettings(), emptySources, emptyBounds |
| 136 | + , surface.transform.position, surface.transform.rotation); |
| 137 | + } |
| 138 | + |
| 139 | + void UpdateAsyncBuildOperations() |
| 140 | + { |
| 141 | + foreach (var oper in m_BakeOperations) |
| 142 | + { |
| 143 | + if (oper.surface == null || oper.bakeOperation == null) |
| 144 | + continue; |
| 145 | + |
| 146 | + if (oper.bakeOperation.isDone) |
| 147 | + { |
| 148 | + var surface = oper.surface; |
| 149 | + var delete = GetNavMeshAssetToDelete(surface); |
| 150 | + if (delete != null) |
| 151 | + AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(delete)); |
| 152 | + |
| 153 | + surface.RemoveData(); |
| 154 | + SetNavMeshData(surface, oper.bakeData); |
| 155 | + |
| 156 | + if (surface.isActiveAndEnabled) |
| 157 | + surface.AddData(); |
| 158 | + CreateNavMeshAsset(surface); |
| 159 | + EditorSceneManager.MarkSceneDirty(surface.gameObject.scene); |
| 160 | + } |
| 161 | + } |
| 162 | + m_BakeOperations.RemoveAll(o => o.bakeOperation == null || o.bakeOperation.isDone); |
| 163 | + if (m_BakeOperations.Count == 0) |
| 164 | + EditorApplication.update -= UpdateAsyncBuildOperations; |
| 165 | + } |
| 166 | + |
| 167 | + public bool IsSurfaceBaking(NavMeshSurface surface) |
| 168 | + { |
| 169 | + if (surface == null) |
| 170 | + return false; |
| 171 | + |
| 172 | + foreach (var oper in m_BakeOperations) |
| 173 | + { |
| 174 | + if (oper.surface == null || oper.bakeOperation == null) |
| 175 | + continue; |
| 176 | + |
| 177 | + if (oper.surface == surface) |
| 178 | + return true; |
| 179 | + } |
| 180 | + |
| 181 | + return false; |
| 182 | + } |
| 183 | + |
| 184 | + public void ClearSurfaces(UnityEngine.Object[] surfaces) |
| 185 | + { |
| 186 | + foreach (NavMeshSurface s in surfaces) |
| 187 | + ClearSurface(s); |
| 188 | + } |
| 189 | + |
| 190 | + static void SetNavMeshData(NavMeshSurface navSurface, NavMeshData navMeshData) |
| 191 | + { |
| 192 | + var so = new SerializedObject(navSurface); |
| 193 | + var navMeshDataProperty = so.FindProperty("m_NavMeshData"); |
| 194 | + navMeshDataProperty.objectReferenceValue = navMeshData; |
| 195 | + so.ApplyModifiedPropertiesWithoutUndo(); |
| 196 | + } |
| 197 | + |
| 198 | + void StoreNavMeshDataIfInPrefab(NavMeshSurface surfaceToStore) |
| 199 | + { |
| 200 | + var prefabStage = PrefabStageUtility.GetPrefabStage(surfaceToStore.gameObject); |
| 201 | + var isPartOfPrefab = prefabStage != null && prefabStage.IsPartOfPrefabContents(surfaceToStore.gameObject); |
| 202 | + if (!isPartOfPrefab) |
| 203 | + return; |
| 204 | + |
| 205 | + // check if data has already been stored for this surface |
| 206 | + foreach (var storedAssetInfo in m_PrefabNavMeshDataAssets) |
| 207 | + if (storedAssetInfo.surface == surfaceToStore) |
| 208 | + return; |
| 209 | + |
| 210 | + if (m_PrefabNavMeshDataAssets.Count == 0) |
| 211 | + { |
| 212 | + PrefabStage.prefabSaving -= DeleteStoredNavMeshDataAssetsForOwnedSurfaces; |
| 213 | + PrefabStage.prefabSaving += DeleteStoredNavMeshDataAssetsForOwnedSurfaces; |
| 214 | + |
| 215 | + PrefabStage.prefabStageClosing -= ForgetUnsavedNavMeshDataChanges; |
| 216 | + PrefabStage.prefabStageClosing += ForgetUnsavedNavMeshDataChanges; |
| 217 | + } |
| 218 | + |
| 219 | + var isDataOwner = true; |
| 220 | + if (PrefabUtility.IsPartOfPrefabInstance(surfaceToStore) && !PrefabUtility.IsPartOfModelPrefab(surfaceToStore)) |
| 221 | + { |
| 222 | + var basePrefabSurface = PrefabUtility.GetCorrespondingObjectFromSource(surfaceToStore) as NavMeshSurface; |
| 223 | + isDataOwner = basePrefabSurface == null || surfaceToStore.navMeshData != basePrefabSurface.navMeshData; |
| 224 | + } |
| 225 | + m_PrefabNavMeshDataAssets.Add(new SavedPrefabNavMeshData { surface = surfaceToStore, navMeshData = isDataOwner ? surfaceToStore.navMeshData : null }); |
| 226 | + } |
| 227 | + |
| 228 | + bool IsCurrentPrefabNavMeshDataStored(NavMeshSurface surface) |
| 229 | + { |
| 230 | + if (surface == null) |
| 231 | + return false; |
| 232 | + |
| 233 | + foreach (var storedAssetInfo in m_PrefabNavMeshDataAssets) |
| 234 | + { |
| 235 | + if (storedAssetInfo.surface == surface) |
| 236 | + return storedAssetInfo.navMeshData == surface.navMeshData; |
| 237 | + } |
| 238 | + |
| 239 | + return false; |
| 240 | + } |
| 241 | + |
| 242 | + void DeleteStoredNavMeshDataAssetsForOwnedSurfaces(GameObject gameObjectInPrefab) |
| 243 | + { |
| 244 | + // Debug.LogFormat("DeleteStoredNavMeshDataAsset() when saving prefab {0}", gameObjectInPrefab.name); |
| 245 | + |
| 246 | + var surfaces = gameObjectInPrefab.GetComponentsInChildren<NavMeshSurface>(true); |
| 247 | + foreach (var surface in surfaces) |
| 248 | + DeleteStoredPrefabNavMeshDataAsset(surface); |
| 249 | + } |
| 250 | + |
| 251 | + void DeleteStoredPrefabNavMeshDataAsset(NavMeshSurface surface) |
| 252 | + { |
| 253 | + for (var i = m_PrefabNavMeshDataAssets.Count - 1; i >= 0; i--) |
| 254 | + { |
| 255 | + var storedAssetInfo = m_PrefabNavMeshDataAssets[i]; |
| 256 | + if (storedAssetInfo.surface == surface) |
| 257 | + { |
| 258 | + var storedNavMeshData = storedAssetInfo.navMeshData; |
| 259 | + if (storedNavMeshData != null && storedNavMeshData != surface.navMeshData) |
| 260 | + { |
| 261 | + var assetPath = AssetDatabase.GetAssetPath(storedNavMeshData); |
| 262 | + AssetDatabase.DeleteAsset(assetPath); |
| 263 | + } |
| 264 | + |
| 265 | + m_PrefabNavMeshDataAssets.RemoveAt(i); |
| 266 | + break; |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + if (m_PrefabNavMeshDataAssets.Count == 0) |
| 271 | + { |
| 272 | + PrefabStage.prefabSaving -= DeleteStoredNavMeshDataAssetsForOwnedSurfaces; |
| 273 | + PrefabStage.prefabStageClosing -= ForgetUnsavedNavMeshDataChanges; |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + void ForgetUnsavedNavMeshDataChanges(PrefabStage prefabStage) |
| 278 | + { |
| 279 | + // Debug.Log("On prefab closing - forget about this object's surfaces and stop caring about prefab saving"); |
| 280 | + |
| 281 | + if (prefabStage == null) |
| 282 | + return; |
| 283 | + |
| 284 | + var allSurfacesInPrefab = prefabStage.prefabContentsRoot.GetComponentsInChildren<NavMeshSurface>(true); |
| 285 | + NavMeshSurface surfaceInPrefab = null; |
| 286 | + var index = 0; |
| 287 | + do |
| 288 | + { |
| 289 | + if (allSurfacesInPrefab.Length > 0) |
| 290 | + surfaceInPrefab = allSurfacesInPrefab[index]; |
| 291 | + |
| 292 | + for (var i = m_PrefabNavMeshDataAssets.Count - 1; i >= 0; i--) |
| 293 | + { |
| 294 | + var storedPrefabInfo = m_PrefabNavMeshDataAssets[i]; |
| 295 | + if (storedPrefabInfo.surface == null) |
| 296 | + { |
| 297 | + // Debug.LogFormat("A surface from the prefab got deleted after it has baked a new NavMesh but it hasn't saved it. Now the unsaved asset gets deleted. ({0})", storedPrefabInfo.navMeshData); |
| 298 | + |
| 299 | + // surface got deleted, thus delete its initial NavMeshData asset |
| 300 | + if (storedPrefabInfo.navMeshData != null) |
| 301 | + { |
| 302 | + var assetPath = AssetDatabase.GetAssetPath(storedPrefabInfo.navMeshData); |
| 303 | + AssetDatabase.DeleteAsset(assetPath); |
| 304 | + } |
| 305 | + |
| 306 | + m_PrefabNavMeshDataAssets.RemoveAt(i); |
| 307 | + } |
| 308 | + else if (surfaceInPrefab != null && storedPrefabInfo.surface == surfaceInPrefab) |
| 309 | + { |
| 310 | + //Debug.LogFormat("The surface {0} from the prefab was storing the original navmesh data and now will be forgotten", surfaceInPrefab); |
| 311 | + |
| 312 | + var baseSurface = PrefabUtility.GetCorrespondingObjectFromSource(surfaceInPrefab) as NavMeshSurface; |
| 313 | + if (baseSurface == null || surfaceInPrefab.navMeshData != baseSurface.navMeshData) |
| 314 | + { |
| 315 | + var assetPath = AssetDatabase.GetAssetPath(surfaceInPrefab.navMeshData); |
| 316 | + AssetDatabase.DeleteAsset(assetPath); |
| 317 | + |
| 318 | + //Debug.LogFormat("The surface {0} from the prefab has baked new NavMeshData but did not save this change so the asset has been now deleted. ({1})", |
| 319 | + // surfaceInPrefab, assetPath); |
| 320 | + } |
| 321 | + |
| 322 | + m_PrefabNavMeshDataAssets.RemoveAt(i); |
| 323 | + } |
| 324 | + } |
| 325 | + } while (++index < allSurfacesInPrefab.Length); |
| 326 | + |
| 327 | + if (m_PrefabNavMeshDataAssets.Count == 0) |
| 328 | + { |
| 329 | + PrefabStage.prefabSaving -= DeleteStoredNavMeshDataAssetsForOwnedSurfaces; |
| 330 | + PrefabStage.prefabStageClosing -= ForgetUnsavedNavMeshDataChanges; |
| 331 | + } |
| 332 | + } |
| 333 | + } |
| 334 | +} |
0 commit comments