DependencyBuilderTriggers.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Linq;
  2. using UnityEditor;
  3. using UnityEditor.Compilation;
  4. namespace IntelligentProjectAnalyzer.Editor
  5. {
  6. /// <summary>
  7. /// Hooks into Unity's editor events to automatically trigger the dependency build process.
  8. /// </summary>
  9. [InitializeOnLoad]
  10. public static class DependencyBuilderTriggers
  11. {
  12. [MenuItem("Tools/Analyzer/Build Dependency Store")]
  13. public static void TriggerBuild() => DependencyStoreBuilder.BuildStore();
  14. // Unity calls static constructor when the editor loads.
  15. static DependencyBuilderTriggers()
  16. {
  17. // Trigger a build after scripts have finished compiling.
  18. CompilationPipeline.compilationFinished += OnCompilationFinished;
  19. }
  20. /// <summary>
  21. /// Callback for when script compilation is finished.
  22. /// </summary>
  23. private static void OnCompilationFinished(object context)
  24. {
  25. // We can add a delay here if needed, but for now, let's trigger it directly.
  26. // DependencyStoreBuilder.BuildStore(); // Temporarily disabled
  27. }
  28. }
  29. /// <summary>
  30. /// AssetPostprocessor to trigger the dependency build on any relevant asset changes.
  31. /// </summary>
  32. internal class DependencyBuilderAssetPostprocessor : AssetPostprocessor
  33. {
  34. /// <summary>
  35. /// Unity calls this method after any assets are imported, deleted, or moved.
  36. /// </summary>
  37. private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
  38. {
  39. // Check if any of the changed assets are scripts, as this is our primary concern.
  40. // This prevents running the full analysis for simple texture or material changes.
  41. var scriptsChanged = importedAssets.Any(path => path.EndsWith(".cs")) ||
  42. deletedAssets.Any(path => path.EndsWith(".cs")) ||
  43. movedAssets.Any(path => path.EndsWith(".cs"));
  44. if (scriptsChanged)
  45. {
  46. // A script has changed, so we trigger a rebuild of the dependency store.
  47. // DependencyStoreBuilder.BuildStore(); // Temporarily disabled
  48. }
  49. }
  50. }
  51. }