Geen omschrijving
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.

GridPalettes.cs 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace UnityEditor.Tilemaps
  5. {
  6. internal class GridPalettes : ScriptableSingleton<GridPalettes>
  7. {
  8. private List<GameObject> m_PalettesCache;
  9. internal static Action palettesChanged;
  10. internal static List<GameObject> palettes
  11. {
  12. get
  13. {
  14. if (instance.m_PalettesCache == null
  15. || (instance.m_PalettesCache.Count > 0 && instance.m_PalettesCache[0] == null))
  16. {
  17. instance.RefreshPalettesCache();
  18. }
  19. return instance.m_PalettesCache;
  20. }
  21. }
  22. private void OnEnable()
  23. {
  24. CleanCache();
  25. }
  26. private void OnDisable()
  27. {
  28. CleanCache();
  29. }
  30. private void RefreshPalettesCache()
  31. {
  32. if (m_PalettesCache == null)
  33. m_PalettesCache = new List<GameObject>();
  34. m_PalettesCache.Clear();
  35. string[] guids = AssetDatabase.FindAssets("t:GridPalette");
  36. foreach (string guid in guids)
  37. {
  38. string path = AssetDatabase.GUIDToAssetPath(guid);
  39. GridPalette paletteAsset = AssetDatabase.LoadAssetAtPath(path, typeof(GridPalette)) as GridPalette;
  40. if (paletteAsset != null)
  41. {
  42. string assetPath = AssetDatabase.GetAssetPath(paletteAsset);
  43. GameObject palette = AssetDatabase.LoadMainAssetAtPath(assetPath) as GameObject;
  44. if (palette != null)
  45. {
  46. m_PalettesCache.Add(palette);
  47. }
  48. }
  49. }
  50. m_PalettesCache.Sort((x, y) => String.Compare(x.name, y.name, StringComparison.OrdinalIgnoreCase));
  51. palettesChanged?.Invoke();
  52. }
  53. private class AssetProcessor : AssetPostprocessor
  54. {
  55. public override int GetPostprocessOrder()
  56. {
  57. return 1;
  58. }
  59. private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromPath)
  60. {
  61. if (!GridPaintingState.savingPalette)
  62. CleanCache();
  63. }
  64. }
  65. internal static void CleanCache()
  66. {
  67. instance.m_PalettesCache = null;
  68. }
  69. }
  70. }