Unity Editor 查找指定类型的文件数量
2026/7/23 21:49:00 网站建设 项目流程

时间: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 Dictionary<string, int> fileTypeCounts = new Dictionary<string, int>(); [MenuItem("Tools/Finder Appoint Files")] public static void ShowWindow() { GetWindow<FinderAppointFiles>("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特殊类型的统计 CountSpecificType<Texture2D>("Texture2D"); CountSpecificType<Material>("Material"); CountSpecificType<GameObject>("Prefab"); CountSpecificType<Shader>("Shader"); CountSpecificType<MonoScript>("Script"); CountSpecificType<AnimationClip>("AnimationClip"); CountSpecificType<AudioClip>("AudioClip"); } void CountSpecificType<T>(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; } }

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询