NavMeshAssetManager.cs 14 KB

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