公司动态
Unity Editor 查找指定类型的文件数量
时间2026.07版本Unity 6000.3.19f1说明该脚本文件放到Editor文件夹下using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; using UnityEngine; public class FinderAppointFiles : EditorWindow { private Vector2 scrollPosition; private string searchFilter ; private string searchFolder Assets; private Dictionarystring, int fileTypeCounts new Dictionarystring, int(); [MenuItem(Tools/Finder Appoint Files)] public static void ShowWindow() { GetWindowFinderAppointFiles(Finder Appoint Files); } void OnGUI() { EditorGUILayout.HelpBox(统计项目中各种类型文件的数量, MessageType.Info); EditorGUILayout.Space(); // 输入要搜索的文件夹路径 EditorGUILayout.BeginHorizontal(); { EditorGUILayout.LabelField(搜索文件夹:, GUILayout.Width(80)); var height GUILayout.Height(EditorGUIUtility.singleLineHeight); EditorGUILayout.SelectableLabel(searchFolder, EditorStyles.textField, height); HandleDragAndDrop(); // 处理拖拽事件通过拖拽指定文件夹 SelctionFolder(); // 选择文件夹按钮事件 } EditorGUILayout.EndHorizontal(); // 搜索过滤 EditorGUILayout.BeginHorizontal(); { EditorGUILayout.LabelField(过滤:, GUILayout.Width(40)); searchFilter EditorGUILayout.TextField(searchFilter); if (GUILayout.Button(刷新统计, GUILayout.Width(80))) { if (string.IsNullOrEmpty(searchFolder)) searchFolder Assets; CountFileTypes(); } } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); // 结果显示 if (fileTypeCounts.Count 0) { EditorGUILayout.LabelField($在文件夹【{searchFolder}】中共发现 {fileTypeCounts.Values.Sum()} 个文件{fileTypeCounts.Count} 种类型:, EditorStyles.boldLabel); scrollPosition EditorGUILayout.BeginScrollView(scrollPosition); // 按键名排序升序 var sortedItems fileTypeCounts.OrderBy(pair pair.Key); //// 按数量排序降序 //var sortedItems fileTypeCounts.OrderByDescending(pair pair.Value); foreach (var item in sortedItems) { if (!string.IsNullOrEmpty(searchFilter) !item.Key.ToLower().Contains(searchFilter.ToLower())) { continue; } EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(item.Key, GUILayout.Width(200)); EditorGUILayout.LabelField(item.Value.ToString(), EditorStyles.boldLabel); EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndScrollView(); } else { EditorGUILayout.LabelField(没有找到文件统计数据); } } void OnFocus() { CountFileTypes(); } private void HandleDragAndDrop() { Rect dropArea GUILayoutUtility.GetLastRect(); if (Event.current.type EventType.DragUpdated dropArea.Contains(Event.current.mousePosition)) { DragAndDrop.visualMode DragAndDropVisualMode.Copy; Event.current.Use(); } else if (Event.current.type EventType.DragPerform dropArea.Contains(Event.current.mousePosition)) { DragAndDrop.AcceptDrag(); if (DragAndDrop.paths ! null DragAndDrop.paths.Length 0) { string path DragAndDrop.paths[0]; if (Directory.Exists(path)) { // 转换为Assets相对路径 if (path.StartsWith(Application.dataPath)) { searchFolder Assets path.Substring(Application.dataPath.Length); } else { searchFolder path; } CountFileTypes(); } } Event.current.Use(); } } private void SelctionFolder() { if (GUILayout.Button(选择文件夹, GUILayout.Width(100))) { string folderPath EditorUtility.OpenFolderPanel(选择文件夹, Assets, ); if (!string.IsNullOrEmpty(folderPath)) { // 转换为相对路径 if (folderPath.StartsWith(Application.dataPath)) { searchFolder Assets folderPath.Substring(Application.dataPath.Length); CountFileTypes(); } } } } void CountFileTypes() { fileTypeCounts.Clear(); // 获取项目中所有资源路径 string[] allAssetPaths AssetDatabase.GetAllAssetPaths(); foreach (string path in allAssetPaths) { // 跳过目录和特殊文件 if (path.StartsWith(searchFolder) !Directory.Exists(path)) { string extension Path.GetExtension(path).ToLower(); if (string.IsNullOrEmpty(extension)) { extension [无扩展名]; } if (fileTypeCounts.ContainsKey(extension)) { fileTypeCounts[extension]; } else { fileTypeCounts.Add(extension, 1); } } } // 添加一些Unity特殊类型的统计 CountSpecificTypeTexture2D(Texture2D); CountSpecificTypeMaterial(Material); CountSpecificTypeGameObject(Prefab); CountSpecificTypeShader(Shader); CountSpecificTypeMonoScript(Script); CountSpecificTypeAnimationClip(AnimationClip); CountSpecificTypeAudioClip(AudioClip); } void CountSpecificTypeT(string typeName) where T : Object { string filter $t:{typeof(T).Name}; string[] searchInFolders new string[] { searchFolder }; string[] guids AssetDatabase.FindAssets(filter, searchInFolders); if (guids.Length 0) // 数量为0的文件类型不统计数量 fileTypeCounts[$[{typeName}]] guids.Length; } }