using System; using System.Collections.Generic; using System.Reflection; using UnityEngine; namespace UnityEditor.Tilemaps { /// /// Use this attribute to add an option to customize the sorting of Active Targets in the Active Tilemap list of the Tile Palette window. /// /// /// Append this attribute to a class which inherits from IComparer<GameObject> or to a method which creates an IComparer<GameObject>. The instance of IComparer generated with the attribute is used for comparing and sorting Active Target GameObjects in the Active Tilemaps list. /// /// /// /// { /// public int Compare(GameObject go1, GameObject go2) /// { /// return String.Compare(go1.name, go2.name); /// } /// } /// /// class ReverseAlphabeticalComparer : IComparer /// { /// public int Compare(GameObject go1, GameObject go2) /// { /// return -String.Compare(go1.name, go2.name); /// } /// /// [GridPaintSorting] /// public static IComparer ReverseAlphabetical() /// { /// return new ReverseAlphabeticalComparer(); /// } /// } /// ]]> /// [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] public class GridPaintSortingAttribute : Attribute { private static List m_SortingMethods; private static List m_SortingTypes; internal static List sortingMethods { get { if (m_SortingMethods == null) GetUserSortingComparers(); return m_SortingMethods; } } internal static List sortingTypes { get { if (m_SortingTypes == null) GetUserSortingComparers(); return m_SortingTypes; } } private static void GetUserSortingComparers() { m_SortingMethods = new List(); foreach (var sortingMethod in TypeCache.GetMethodsWithAttribute()) { if (!sortingMethod.ReturnType.IsAssignableFrom(typeof(IComparer))) continue; if (sortingMethod.GetGenericArguments().Length > 0) continue; m_SortingMethods.Add(sortingMethod); } m_SortingTypes = new List(); foreach (var sortingType in TypeCache.GetTypesWithAttribute()) { if (sortingType.IsAbstract) continue; m_SortingTypes.Add(sortingType); } } [GridPaintSorting] internal class Alphabetical : IComparer { public int Compare(GameObject go1, GameObject go2) { return String.Compare(go1.name, go2.name); } } [GridPaintSorting] internal class ReverseAlphabetical : IComparer { public int Compare(GameObject go1, GameObject go2) { return -String.Compare(go1.name, go2.name); } } } }