using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; using UnityEngine; namespace Stary.Evo.StoryEditor.Editor { public static class GraphEditorTools { public static int Popup(string content, int currentIndex, string[] options) { EditorGUILayout.LabelField( "请选择剧本所在的Package:" , EditorStyles.boldLabel ) ; return EditorGUILayout.Popup( currentIndex , options ) ; } /// /// 获取所有Module Package /// public static List GetPackages() { // 查找Modules目录 List modules = new(); GetDirectoryPaths(Application.dataPath, modules, "Modules"); if (modules.Count == 0) { Debug.LogError("未找到任何Modules目录"); return new(); } // 查找package List packages = new(); GetDirectoryPaths(modules[0], packages, "com."); if (packages.Count == 0) { Debug.LogError("未找到任何Package"); return new(); } return packages; } /// /// 获取符合条件的目录 /// /// 查找起始目录 /// 查找结果 /// 筛选条件(title) /// 筛选条件(tail) private static void GetDirectoryPaths(string root, List result, string title = null, string tail = null) { foreach (var dir in Directory.GetDirectories(root)) { // ReSharper disable once ReplaceWithSingleAssignment.True var check = true; // 匹配文件头 if (!string.IsNullOrEmpty(title) && !Path.GetFileName(dir).StartsWith(title)) { check = false; } // 匹配文件尾 if (!string.IsNullOrEmpty(tail) && !Path.GetFileName(dir).EndsWith(tail)) { check = false; } if(check) result.Add(dir.Replace('\\', '/')); // 继续往下找 GetDirectoryPaths(dir, result, title); } } public static IEnumerable GetAllAssemblyNames() => AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetName().Name).OrderBy(n => n); /// /// 获取继承 IResource 的所有类 /// public static HashSet IResourceTypes(string assembly) { if (string.IsNullOrEmpty(assembly)) return new(); var asm = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == assembly); return asm == null ? new() : asm.GetTypes().Where(t => t.IsClass && !t.IsAbstract && t.GetInterfaces().Any(i => i.Name == nameof(IResource))).Select(t => t.Name).ToHashSet(); } } }