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