No Description
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.

GridPaintSortingAttribute.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using UnityEngine;
  5. namespace UnityEditor.Tilemaps
  6. {
  7. /// <summary>
  8. /// Use this attribute to add an option to customize the sorting of Active Targets in the Active Tilemap list of the Tile Palette window.
  9. /// </summary>
  10. /// <remarks>
  11. /// Append this attribute to a class which inherits from IComparer&lt;GameObject&gt; or to a method which creates an IComparer&lt;GameObject&gt;. The instance of IComparer generated with the attribute is used for comparing and sorting Active Target GameObjects in the Active Tilemaps list.
  12. /// </remarks>
  13. /// <example>
  14. /// <code lang="cs"><![CDATA[
  15. /// using System;
  16. /// using System.Collections.Generic;
  17. /// using UnityEngine;
  18. /// using UnityEditor;
  19. ///
  20. /// [GridPaintSorting]
  21. /// class Alphabetical : IComparer<GameObject>
  22. /// {
  23. /// public int Compare(GameObject go1, GameObject go2)
  24. /// {
  25. /// return String.Compare(go1.name, go2.name);
  26. /// }
  27. /// }
  28. ///
  29. /// class ReverseAlphabeticalComparer : IComparer<GameObject>
  30. /// {
  31. /// public int Compare(GameObject go1, GameObject go2)
  32. /// {
  33. /// return -String.Compare(go1.name, go2.name);
  34. /// }
  35. ///
  36. /// [GridPaintSorting]
  37. /// public static IComparer<GameObject> ReverseAlphabetical()
  38. /// {
  39. /// return new ReverseAlphabeticalComparer();
  40. /// }
  41. /// }
  42. /// ]]></code>
  43. /// </example>
  44. [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
  45. public class GridPaintSortingAttribute : Attribute
  46. {
  47. private static List<MethodInfo> m_SortingMethods;
  48. private static List<Type> m_SortingTypes;
  49. internal static List<MethodInfo> sortingMethods
  50. {
  51. get
  52. {
  53. if (m_SortingMethods == null)
  54. GetUserSortingComparers();
  55. return m_SortingMethods;
  56. }
  57. }
  58. internal static List<Type> sortingTypes
  59. {
  60. get
  61. {
  62. if (m_SortingTypes == null)
  63. GetUserSortingComparers();
  64. return m_SortingTypes;
  65. }
  66. }
  67. private static void GetUserSortingComparers()
  68. {
  69. m_SortingMethods = new List<MethodInfo>();
  70. foreach (var sortingMethod in TypeCache.GetMethodsWithAttribute<GridPaintSortingAttribute>())
  71. {
  72. if (!sortingMethod.ReturnType.IsAssignableFrom(typeof(IComparer<GameObject>)))
  73. continue;
  74. if (sortingMethod.GetGenericArguments().Length > 0)
  75. continue;
  76. m_SortingMethods.Add(sortingMethod);
  77. }
  78. m_SortingTypes = new List<Type>();
  79. foreach (var sortingType in TypeCache.GetTypesWithAttribute<GridPaintSortingAttribute>())
  80. {
  81. if (sortingType.IsAbstract)
  82. continue;
  83. m_SortingTypes.Add(sortingType);
  84. }
  85. }
  86. [GridPaintSorting]
  87. internal class Alphabetical : IComparer<GameObject>
  88. {
  89. public int Compare(GameObject go1, GameObject go2)
  90. {
  91. return String.Compare(go1.name, go2.name);
  92. }
  93. }
  94. [GridPaintSorting]
  95. internal class ReverseAlphabetical : IComparer<GameObject>
  96. {
  97. public int Compare(GameObject go1, GameObject go2)
  98. {
  99. return -String.Compare(go1.name, go2.name);
  100. }
  101. }
  102. }
  103. }