Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

NavMeshAssetManager.cs 15KB

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