Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BackgroundImport.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Threading;
  4. using UnityEditor.U2D.Aseprite.Common;
  5. using UnityEngine;
  6. namespace UnityEditor.U2D.Aseprite
  7. {
  8. [InitializeOnLoad]
  9. internal static class BackgroundImport
  10. {
  11. static readonly string k_AssetsPath = "Assets/";
  12. static readonly List<string> s_AssetsFullPath = new List<string>();
  13. static bool s_HasChange = false;
  14. static bool s_LastSettingsValue = false;
  15. static FileSystemWatcher s_WatcherAse = null;
  16. static FileSystemWatcher s_WatcherAseprite = null;
  17. [InitializeOnLoadMethod]
  18. static void Setup()
  19. {
  20. Cleanup();
  21. EditorApplication.update += OnUpdate;
  22. if (ImportSettings.backgroundImport)
  23. SetupWatcher();
  24. s_LastSettingsValue = ImportSettings.backgroundImport;
  25. }
  26. static void Cleanup()
  27. {
  28. EditorApplication.update -= OnUpdate;
  29. StopWatchers();
  30. }
  31. static void OnUpdate()
  32. {
  33. if (EditorApplication.isCompiling)
  34. return;
  35. if (EditorApplication.isUpdating)
  36. return;
  37. CheckForSettingsUpdate();
  38. CheckForChange();
  39. }
  40. static void CheckForSettingsUpdate()
  41. {
  42. if (ImportSettings.backgroundImport == s_LastSettingsValue)
  43. return;
  44. if (ImportSettings.backgroundImport)
  45. SetupWatcher();
  46. else
  47. StopWatchers();
  48. s_LastSettingsValue = ImportSettings.backgroundImport;
  49. }
  50. static void SetupWatcher()
  51. {
  52. if (Application.isBatchMode)
  53. return;
  54. ThreadPool.QueueUserWorkItem(MonitorDirectory, k_AssetsPath);
  55. }
  56. static void MonitorDirectory(object obj)
  57. {
  58. var path = (string)obj;
  59. s_WatcherAse = SetupWatcher(path, "*.ase");
  60. s_WatcherAseprite = SetupWatcher(path, "*.aseprite");
  61. }
  62. static FileSystemWatcher SetupWatcher(string path, string filter)
  63. {
  64. var watcher = new FileSystemWatcher()
  65. {
  66. Filter = filter,
  67. Path = path,
  68. IncludeSubdirectories = true,
  69. EnableRaisingEvents = true
  70. };
  71. watcher.Changed += OnChangeDetected;
  72. watcher.Created += OnChangeDetected;
  73. watcher.Renamed += OnChangeDetected;
  74. return watcher;
  75. }
  76. static void OnChangeDetected(object sender, FileSystemEventArgs e)
  77. {
  78. var extension = Path.GetExtension(e.FullPath);
  79. if (extension != ".aseprite" && extension != ".ase")
  80. return;
  81. s_AssetsFullPath.Add(e.FullPath);
  82. s_HasChange = true;
  83. }
  84. static void StopWatchers()
  85. {
  86. if (s_WatcherAse != null)
  87. {
  88. s_WatcherAse.Dispose();
  89. s_WatcherAse = null;
  90. }
  91. if (s_WatcherAseprite != null)
  92. {
  93. s_WatcherAseprite.Dispose();
  94. s_WatcherAseprite = null;
  95. }
  96. }
  97. static void CheckForChange()
  98. {
  99. if (!s_HasChange)
  100. return;
  101. // If the editor is already focused, skip forced import.
  102. if (UnityEditorInternal.InternalEditorUtility.isApplicationActive)
  103. {
  104. s_HasChange = false;
  105. return;
  106. }
  107. if (Application.isPlaying)
  108. return;
  109. AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport & ImportAssetOptions.ForceUpdate);
  110. var relativePaths = new List<string>(s_AssetsFullPath.Count);
  111. for (var i = 0; i < s_AssetsFullPath.Count; ++i)
  112. relativePaths.Add(FileUtil.GetProjectRelativePath(s_AssetsFullPath[i]));
  113. AssetDatabase.ForceReserializeAssets(relativePaths);
  114. InternalEditorBridge.RefreshInspectors();
  115. s_AssetsFullPath.Clear();
  116. s_HasChange = false;
  117. }
  118. }
  119. }