
源码-调试与诊断工具篇章04-源码深度-编辑器模块阅读时间约 25 分钟前置知识了解清单与报告一、引言本章将深入解析 YooAsset 调试与诊断工具的源码实现。YooAsset 提供了多个调试和诊断工具包括 AssetBundle Debugger、Asset Scanner、Runtime Debugger 等。这些工具帮助开发者诊断资源管理问题、分析 Bundle 结构、排查性能瓶颈。本章将从源码层面深入解析这些调试诊断工具的设计和实现。二、AssetBundle Debugger2.1 Debugger 的作用AssetBundle Debugger 是 YooAsset 的核心调试工具功能查看 Bundle 列表查看 Bundle 详细信息查看 Bundle 之间的依赖关系查看资源地址映射模拟加载资源使用场景构建后查看 Bundle 结构分析 Bundle 依赖问题优化资源打包排查资源加载问题2.2 Debugger 的实现public class AssetBundleDebugger { private PackageManifest _manifest; public void LoadManifest(string manifestPath) { _manifest PackageManifest.LoadFromFile(manifestPath); } public ListPackageBundle GetAllBundles() { return _manifest?.BundleList.Bundles; } public PackageBundle GetBundle(string bundleName) { return _manifest?.BundleList.Bundles.FirstOrDefault(b b.BundleName bundleName); } public ListPackageBundle GetBundleDependencies(string bundleName) { var bundle GetBundle(bundleName); if (bundle null) return new ListPackageBundle(); return bundle.DependIDs .Select(id _manifest.BundleList.Bundles.FirstOrDefault(b b.BundleID id)) .Where(b b ! null) .ToList(); } public ListPackageBundle GetBundleDependents(string bundleName) { var bundle GetBundle(bundleName); if (bundle null) return new ListPackageBundle(); return _manifest.BundleList.Bundles .Where(b b.DependIDs.Contains(bundle.BundleID)) .ToList(); } }加载 Manifest 详解Debugger 需要先加载 PackageManifest然后基于 PackageManifest 提供调试功能。获取 Bundle 列表详解获取所有 Bundle 的列表并显示 Bundle 的详细信息。获取依赖关系详解根据 Bundle 的依赖关系获取依赖的 Bundle 和被依赖的 Bundle。2.3 Debugger WindowAssetBundleDebuggerWindow 是 Debugger 的 UI 窗口public class AssetBundleDebuggerWindow : EditorWindow { [MenuItem(YooAsset/AssetBundle Debugger)] public static void Open() { AssetBundleDebuggerWindow window GetWindowAssetBundleDebuggerWindow(); window.titleContent new GUIContent(AssetBundle Debugger); window.Show(); } private void OnGUI() { // 绘制 UI DrawToolbar(); DrawBundleList(); DrawBundleDetails(); } }打开窗口详解通过菜单项YooAsset/AssetBundle Debugger打开调试窗口。绘制 UI 详解窗口主要分为三个区域工具栏、Bundle 列表、Bundle 详情。工具栏详解工具栏包括加载 Manifest、刷新、搜索等功能。Bundle 列表详解Bundle 列表显示所有 Bundle 的概要信息包括名称、大小、Hash 等。Bundle 详情详解Bundle 详情显示选中 Bundle 的详细信息包括资源列表、依赖关系等。2.4 Debugger 的高级功能模拟加载public class AssetBundleDebugger { public AssetBundle SimulateLoadBundle(string bundleName) { // 模拟加载 Bundle不实际加载文件 return null; } }模拟加载功能允许开发者在不实际加载资源的情况下模拟加载流程便于调试。依赖分析public class AssetBundleDebugger { public DependencyGraph AnalyzeDependencies() { var graph new DependencyGraph(); foreach (var bundle in _manifest.BundleList.Bundles) { graph.AddNode(bundle.BundleName); foreach (var depId in bundle.DependIDs) { var depBundle _manifest.BundleList.Bundles.FirstOrDefault(b b.BundleID depId); if (depBundle ! null) { graph.AddEdge(bundle.BundleName, depBundle.BundleName); } } } return graph; } }依赖分析功能分析所有 Bundle 的依赖关系生成依赖图。三、Asset Scanner3.1 Scanner 的作用Asset Scanner 是 YooAsset 的资源扫描器功能扫描项目中所有资源分析资源依赖关系检测未使用的资源检测资源冗余检测大文件使用场景定期清理未使用资源优化资源打包减小包体大小提高资源管理效率3.2 Scanner 的实现public class AssetScanner { public ScanReport Scan(string folderPath) { ScanReport report new ScanReport(); // 1. 扫描所有资源 var assets GetAllAssets(folderPath); // 2. 分析每个资源 foreach (var asset in assets) { var assetInfo new AssetInfo(); assetInfo.Path asset; assetInfo.Dependencies AssetDatabase.GetDependencies(asset, true).ToList(); assetInfo.FileSize new FileInfo(asset).Length; report.Assets.Add(assetInfo); } // 3. 检测未使用的资源 report.UnusedAssets DetectUnusedAssets(report.Assets); // 4. 检测冗余资源 report.RedundantAssets DetectRedundantAssets(report.Assets); // 5. 检测大文件 report.LargeAssets DetectLargeAssets(report.Assets, 1024 * 1024); return report; } private Liststring GetAllAssets(string folderPath) { var guids AssetDatabase.FindAssets(string.Empty, new[] { folderPath }); return guids.Select(AssetDatabase.GUIDToAssetPath).ToList(); } }扫描资源详解使用 AssetDatabase.FindAssets 扫描指定文件夹下的所有资源。分析资源详解为每个资源创建 AssetInfo包含资源路径、依赖关系、文件大小等。检测未使用资源详解使用反射 API 扫描所有引用关系找出未被引用的资源。检测冗余资源详解找出被多次打包的资源提示开发者提取为共享资源。检测大文件详解找出超过指定大小的资源提示开发者优化或压缩。3.3 Scanner WindowAssetScannerWindow 是 Scanner 的 UI 窗口public class AssetScannerWindow : EditorWindow { [MenuItem(YooAsset/Asset Scanner)] public static void Open() { AssetScannerWindow window GetWindowAssetScannerWindow(); window.titleContent new GUIContent(Asset Scanner); window.Show(); } private void OnGUI() { DrawFolderSelection(); DrawScanOptions(); DrawScanButton(); DrawScanResults(); } }打开窗口详解通过菜单项YooAsset/Asset Scanner打开扫描窗口。绘制 UI 详解窗口包括文件夹选择、扫描选项、扫描按钮、扫描结果等区域。扫描选项详解扫描选项包括检测未使用资源、检测冗余资源、检测大文件等。3.4 扫描结果展示扫描结果通过 ScanReport 展示public class ScanReport { public ListAssetInfo Assets new ListAssetInfo(); public Liststring UnusedAssets new Liststring(); public Liststring RedundantAssets new Liststring(); public Liststring LargeAssets new Liststring(); public ScanStatistics Statistics new ScanStatistics(); } public class ScanStatistics { public int TotalAssetCount; public long TotalFileSize; public int UnusedAssetCount; public long UnusedFileSize; public int RedundantAssetCount; public long RedundantFileSize; }统计信息详解统计信息包括总资源数、总文件大小、未使用资源数、未使用文件大小等。未使用资源详解未使用资源列表显示未被引用的资源。冗余资源详解冗余资源列表显示被多次打包的资源。大文件详解大文件列表显示超过指定大小的资源。四、Runtime Debugger4.1 Runtime Debugger 的作用Runtime Debugger 是 YooAsset 的运行时调试器功能查看运行时已加载的 Bundle查看运行时已加载的资源查看运行时缓存信息查看运行时下载信息监控资源加载性能使用场景运行时调试性能分析内存分析问题排查4.2 Runtime Debugger 的实现Runtime Debugger 通过 Runtime 端的接口实现public class RuntimeDebugger { public static DebugInfo GetDebugInfo(ResourcePackage package) { DebugInfo info new DebugInfo(); // 1. 获取 Provider 信息 var providers package.ResourceManager.ProviderCache; foreach (var kvp in providers) { info.LoadedProviders.Add(CreateProviderInfo(kvp.Value)); } // 2. 获取 Bundle 信息 var bundleLoaders package.ResourceManager.BundleLoaders; foreach (var kvp in bundleLoaders) { info.LoadedBundles.Add(CreateBundleInfo(kvp.Value)); } // 3. 获取缓存信息 info.CacheInfo package.CacheSystem.GetDebugInfo(); // 4. 获取下载信息 info.DownloadInfo package.DownloadManager.GetDebugInfo(); return info; } }Provider 信息详解获取所有已加载的 Provider 信息包括 Provider 状态、引用计数、加载进度等。Bundle 信息详解获取所有已加载的 Bundle 信息包括 Bundle 状态、文件大小、引用计数等。缓存信息详解获取缓存系统的信息包括缓存文件、缓存大小、缓存命中率等。下载信息详解获取下载管理器的信息包括下载任务、下载进度、下载速度等。4.3 Runtime Debugger WindowRuntimeDebuggerWindow 是 Runtime Debugger 的 UI 窗口public class RuntimeDebuggerWindow : EditorWindow { [MenuItem(YooAsset/Runtime Debugger)] public static void Open() { RuntimeDebuggerWindow window GetWindowRuntimeDebuggerWindow(); window.titleContent new GUIContent(Runtime Debugger); window.Show(); } private void OnGUI() { DrawPackageSelection(); DrawProviderList(); DrawBundleList(); DrawCacheInfo(); DrawDownloadInfo(); } }打开窗口详解通过菜单项YooAsset/Runtime Debugger打开运行时调试窗口。绘制 UI 详解窗口包括 Package 选择、Provider 列表、Bundle 列表、缓存信息、下载信息等。4.4 实时监控Runtime Debugger 支持实时监控public class RuntimeDebuggerWindow { private void OnEnable() { // 启动定时刷新 EditorApplication.update OnEditorUpdate; } private void OnDisable() { EditorApplication.update - OnEditorUpdate; } private void OnEditorUpdate() { if (EditorApplication.timeSinceStartup - _lastUpdateTime _refreshInterval) { Repaint(); _lastUpdateTime EditorApplication.timeSinceStartup; } } }定时刷新详解使用 EditorApplication.update 定时刷新窗口显示最新的运行时信息。刷新间隔详解刷新间隔默认为 1 秒可根据需要调整。五、性能分析工具5.1 性能分析的重要性性能分析是调试诊断的重要部分找出资源加载的性能瓶颈优化资源加载流程提高游戏性能5.2 性能分析的实现public class PerformanceProfiler { private Dictionarystring, Listfloat _loadTimes new Dictionarystring, Listfloat(); public void RecordLoadTime(string assetPath, float loadTime) { if (!_loadTimes.ContainsKey(assetPath)) _loadTimes[assetPath] new Listfloat(); _loadTimes[assetPath].Add(loadTime); } public PerformanceReport GenerateReport() { var report new PerformanceReport(); foreach (var kvp in _loadTimes) { var stats new LoadTimeStats(); stats.AssetPath kvp.Key; stats.AverageLoadTime kvp.Value.Average(); stats.MaxLoadTime kvp.Value.Max(); stats.MinLoadTime kvp.Value.Min(); stats.LoadCount kvp.Value.Count; report.LoadTimeStats.Add(stats); } // 按平均加载时间排序 report.LoadTimeStats report.LoadTimeStats .OrderByDescending(s s.AverageLoadTime) .ToList(); return report; } }加载时间记录详解记录每个资源的加载时间统计平均、最大、最小加载时间。性能报告生成详解生成性能报告按平均加载时间排序便于找出加载慢的资源。5.3 Unity Profiler 集成YooAsset 集成了 Unity Profilerpublic class AssetProfiler { public static void BeginSample(string name) { UnityEngine.Profiling.Profiler.BeginSample(name); } public static void EndSample() { UnityEngine.Profiling.Profiler.EndSample(); } }Profiler 详解使用 Unity 的 Profiler API 进行性能采样。自定义 Profiler 详解开发者可以自定义 Profiler 标签便于在 Unity Profiler 中识别 YooAsset 的耗时。5.4 Memory Profiler 集成YooAsset 支持 Memory Profiler 集成public class MemoryProfiler { public static long GetMemoryUsage() { return UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong(); } public static long GetBundleMemoryUsage() { long total 0; // 累计所有 Bundle 的内存使用 return total; } }内存使用详解通过 Unity Profiler API 获取内存使用情况。Bundle 内存详解累计所有已加载 Bundle 的内存使用。六、总结本章深入解析了 YooAsset 调试与诊断工具的源码实现包括AssetBundle Debugger查看 Bundle 信息、分析依赖关系Asset Scanner扫描资源、检测未使用资源、检测冗余资源Runtime Debugger查看运行时信息、实时监控性能分析工具性能采样、Profiler 集成、Memory Profiler 集成通过这些调试诊断工具开发者可以方便地诊断和解决 YooAsset 使用过程中的问题。上一篇清单与报告下一篇运行时入口与初始化