using System.Collections.Generic; using UnityEditor; namespace UnityAtoms { /// /// Postprocessor that enables assigning icons to assets after importing them. /// You will need to add processor(s) or assigner(s) (to the CommonIconAssigmentProcessor) /// in order to acutally assign icons. Processors specifies an icon searh filter, its /// own settings path and assigners. Assigners specifies a function called SelectIcon /// that receives the asset and a list of icons (from assets filtered by the processors filter) /// and returns an icon for that asset if it should be applied. /// public partial class IconAssignmentPostprocessor : AssetPostprocessor { /// /// Add a processor to the postprocessor. /// /// The processor to add. public static void AddProcessor(IconAssignmentProcessor processor) { IconAssignmentProcessors.Add(processor); } /// /// Remove a processor to the postprocessor. /// /// The processor to remove. public static void RemoveProcessor(IconAssignmentProcessor processor) { IconAssignmentProcessors.Remove(processor); } /// /// Remove the common assignment postprocessor. /// /// The processor to remove. public static void RemoveCommonIconAssignmentProcessor() { var index = IconAssignmentProcessors.FindIndex((processor) => processor.GetType() == typeof(CommonIconAssignmentProcessor)); if (index != -1) { IconAssignmentProcessors.RemoveAt(index); } } /// /// Add an assigner. /// /// The assigner to add. public static void AddCommonAssigner(IIconAssigner iAssigner) { var commonProcessor = IconAssignmentProcessors[0]; commonProcessor.AddAssigner(iAssigner); } /// /// Remove an assigner. /// /// The assigner to remove. public static void RemoveCommonAssigner(IIconAssigner iAssigner) { var commonProcessor = IconAssignmentProcessors[0]; commonProcessor.RemoveAssigner(iAssigner); } static List IconAssignmentProcessors { get { if (_iconAssignmentProcessors == null) { _iconAssignmentProcessors = new List(); } return _iconAssignmentProcessors; } } static List _iconAssignmentProcessors; /* Create partial function to be able to apply configuration * (add / remove processor(s) and / or assigner(s)) before first time processing icons. */ static partial void Configure(); static bool hasAppliedConfiguration = false; static IconAssignmentPostprocessor() { if (IconAssignmentProcessors.Count == 0) { IconAssignmentProcessors.Add(new CommonIconAssignmentProcessor()); } if (!hasAppliedConfiguration) { Configure(); hasAppliedConfiguration = true; } foreach (var processor in IconAssignmentProcessors) { processor.ReloadIconsFromSettings(); } } static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { foreach (var processor in IconAssignmentProcessors) { processor.Process(importedAssets); } } } }