ThemeCheck.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using XCharts.Runtime;
  5. namespace XCharts.Editor
  6. {
  7. internal static class ThemeCheck
  8. {
  9. public class ThemeAssetPostprocessor : AssetPostprocessor
  10. {
  11. static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets,
  12. string[] movedFromAssetsPaths)
  13. {
  14. foreach (var assetPath in importedAssets)
  15. {
  16. CheckAddedAsset(assetPath);
  17. }
  18. foreach (var assetPath in deletedAssets)
  19. {
  20. CheckDeletedAsset(assetPath);
  21. }
  22. }
  23. }
  24. public static void CheckAddedAsset(string assetPath)
  25. {
  26. var fileName = Path.GetFileName(assetPath);
  27. if (fileName.Equals("XCSettings.asset"))
  28. {
  29. XCThemeMgr.ReloadThemeList();
  30. return;
  31. }
  32. if (!IsThemeAsset(assetPath)) return;
  33. var theme = AssetDatabase.LoadAssetAtPath<Theme>(assetPath);
  34. if (XCSettings.AddCustomTheme(theme))
  35. {
  36. XCThemeMgr.ReloadThemeList();
  37. }
  38. }
  39. public static void CheckDeletedAsset(string assetPath)
  40. {
  41. if (!IsThemeAsset(assetPath)) return;
  42. if (XCSettings.Instance == null) return;
  43. var themes = XCSettings.customThemes;
  44. var changed = false;
  45. for (int i = themes.Count - 1; i >= 0; i--)
  46. {
  47. if (themes[i] == null)
  48. {
  49. themes.RemoveAt(i);
  50. changed = true;
  51. }
  52. }
  53. if (changed)
  54. {
  55. XCThemeMgr.ReloadThemeList();
  56. }
  57. }
  58. private static bool IsThemeAsset(string assetPath)
  59. {
  60. if (!assetPath.EndsWith(".asset")) return false;
  61. var assetName = Path.GetFileNameWithoutExtension(assetPath);
  62. if (!assetName.StartsWith(XCSettings.THEME_ASSET_NAME_PREFIX)) return false;
  63. return true;
  64. }
  65. }
  66. }