using System; using System.Collections.Generic; using System.Linq; using UnityEditor; using UnityEngine; using Object = UnityEngine.Object; namespace TyphoonFontTool { public class CollectCharactersSetting : ScriptableObject { public enum Mode { Include, //包含 Exclude, //排除 } //包含模式 public Mode RunMode = Mode.Include; [HideInInspector] //资源项 public List Includes = new List(); [HideInInspector] //排除项 public List Excludes = new List(); public void AddAssets(params Object[] assets) { AddAssets(RunMode, assets); } public void AddAssets(Mode mode, params Object[] assets) { switch (mode) { case Mode.Include: AddIncludeAssets(assets); break; case Mode.Exclude: AddExcludeAssets(assets); break; default: throw new ArgumentOutOfRangeException(); } } private void AddIncludeAssets(params Object[] assets) { Includes = AddAssets(Includes, assets); UpdateSort(ref Includes); EditorUtility.SetDirty(this); Save(); } private void AddExcludeAssets(params Object[] assets) { Excludes = AddAssets(Excludes, assets); UpdateSort(ref Excludes); Save(); } //加入缺失的资源 private List AddAssets(List list, params Object[] assets) { var valid = list.Where(e => !string.IsNullOrEmpty(e)).ToHashSet(); foreach (var item in assets) { var guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(item)); if (string.IsNullOrEmpty(guid)) { continue; } valid.Add(guid); } return valid.ToList(); } //更新排序 private void UpdateSort(ref List list) { var infos = list.ToDictionary(e => e, e => AssetDatabase.GUIDToAssetPath(e)); //按路径排序 list.Sort((a, b) => { return String.Compare(infos[a], infos[b], StringComparison.Ordinal); }); } //重载 public void Reload() { //剔除无效路径 Includes.RemoveAll(string.IsNullOrEmpty); Excludes.RemoveAll(string.IsNullOrEmpty); //重新排序 UpdateSort(ref Includes); UpdateSort(ref Excludes); } public void RemoveAssets(params string[] guids) { switch (RunMode) { case Mode.Include: { var hashSet = Includes.ToHashSet(); foreach (var guid in guids) { hashSet.Remove(guid); } Includes = hashSet.ToList(); Reload(); } break; case Mode.Exclude: { var hashSet = Excludes.ToHashSet(); foreach (var guid in guids) { hashSet.Remove(guid); } Excludes = hashSet.ToList(); Reload(); } break; default: throw new ArgumentOutOfRangeException(); } EditorUtility.SetDirty(this); Save(); } public void Save() { #if UNITY_EDITOR EditorUtility.SetDirty(this); AssetDatabase.SaveAssets(); #endif } } }