TileSaveProcessor.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace DunGen.Editor
  6. {
  7. public static class TileAssetProcessor
  8. {
  9. private static bool isProcessing;
  10. private static readonly List<string> tilesToProcess = new List<string>();
  11. private static string tileScriptPath;
  12. [InitializeOnLoadMethod]
  13. private static void ResetStatics()
  14. {
  15. isProcessing = false;
  16. tilesToProcess.Clear();
  17. tileScriptPath = null;
  18. }
  19. private static void FindTileScriptPath()
  20. {
  21. // Find all MonoScript assets in the project
  22. string[] guids = AssetDatabase.FindAssets("t:MonoScript");
  23. foreach (string guid in guids)
  24. {
  25. string path = AssetDatabase.GUIDToAssetPath(guid);
  26. var monoScript = AssetDatabase.LoadAssetAtPath<MonoScript>(path);
  27. if (monoScript != null)
  28. {
  29. // Check if this MonoScript represents a DunGen Tile
  30. if (monoScript.GetClass() == typeof(Tile))
  31. {
  32. tileScriptPath = path;
  33. break;
  34. }
  35. }
  36. }
  37. }
  38. public class TileSaveDetector : UnityEditor.AssetModificationProcessor
  39. {
  40. static string[] OnWillSaveAssets(string[] paths)
  41. {
  42. if (!DunGenSettings.Instance.RecalculateTileBoundsOnSave)
  43. return paths;
  44. if (isProcessing)
  45. return paths;
  46. // Cache path to the Tile script for quick lookup later
  47. if (tileScriptPath == null)
  48. FindTileScriptPath();
  49. isProcessing = true;
  50. foreach (var path in paths)
  51. {
  52. // We're only interested in prefabs
  53. if (!path.EndsWith(".prefab", System.StringComparison.OrdinalIgnoreCase))
  54. continue;
  55. // Check dependencies as a quick way to see if the prefab has a Tile component
  56. var dependencies = AssetDatabase.GetDependencies(path, true);
  57. if (!dependencies.Contains(tileScriptPath))
  58. continue;
  59. // Load the prefab
  60. var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
  61. if (prefab == null)
  62. continue;
  63. // If it has a Tile component at the root, add it to the list for later processing
  64. if (prefab.TryGetComponent<Tile>(out var _))
  65. tilesToProcess.Add(path);
  66. }
  67. return paths;
  68. }
  69. }
  70. public class TileSaveProcessor : AssetPostprocessor
  71. {
  72. static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
  73. {
  74. // Nothing to process
  75. if (!isProcessing || tilesToProcess.Count == 0)
  76. return;
  77. try
  78. {
  79. // Combine imported and moved assets into one array
  80. string[] allAssets = new string[importedAssets.Length + movedAssets.Length];
  81. importedAssets.CopyTo(allAssets, 0);
  82. movedAssets.CopyTo(allAssets, importedAssets.Length);
  83. foreach (var path in tilesToProcess)
  84. {
  85. var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
  86. if (prefab == null)
  87. continue;
  88. if (prefab.TryGetComponent<Tile>(out var tile))
  89. {
  90. bool boundsChanged = tile.RecalculateBounds();
  91. // If the prefab is open in a prefab stage, we need to copy the new bounds to it as well
  92. if (boundsChanged)
  93. {
  94. #if UNITY_2021_2_OR_NEWER
  95. var prefabStage = UnityEditor.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
  96. #else
  97. var prefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
  98. #endif
  99. if (prefabStage != null && prefabStage.assetPath == path)
  100. {
  101. if (prefabStage.prefabContentsRoot.TryGetComponent<Tile>(out var prefabTile))
  102. prefabTile.CopyBoundsFrom(tile);
  103. }
  104. }
  105. }
  106. EditorUtility.SetDirty(prefab);
  107. }
  108. AssetDatabase.SaveAssets();
  109. }
  110. finally
  111. {
  112. tilesToProcess.Clear();
  113. isProcessing = false;
  114. }
  115. }
  116. }
  117. }
  118. }