using System.IO; using UnityEditor; using UnityEngine; /// /// 画报资源诊断工具 /// public class PictorialResourceDiagnostic : EditorWindow { [MenuItem("Tools/*美术资源工具/画报资源诊断")] public static void ShowWindow() { GetWindow("画报资源诊断"); } private string testPath = "Assets/Art_Out/UI/Texture/Activity/Activity_Level/BG_Activity_Level_04.png"; private Vector2 scrollPosition; private void OnGUI() { GUILayout.Label("画报资源诊断工具", EditorStyles.boldLabel); GUILayout.Space(10); EditorGUILayout.HelpBox( "此工具用于诊断 Art_Out 目录下的资源是否可以被正确加载。\n" + "检查文件、meta文件、GUID、纹理设置等。", MessageType.Info ); GUILayout.Space(10); // 测试路径 GUILayout.Label("测试资源路径:", EditorStyles.boldLabel); testPath = EditorGUILayout.TextField(testPath); GUILayout.Space(10); // 执行按钮 EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("诊断资源", GUILayout.Height(40))) { DiagnoseResource(); } if (GUILayout.Button("强制刷新AssetDatabase", GUILayout.Height(40))) { ForceRefreshAssetDatabase(); } EditorGUILayout.EndHorizontal(); GUILayout.Space(10); // 说明 scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); GUILayout.Label("功能说明:", EditorStyles.boldLabel); GUILayout.Label("• 检查文件是否存在"); GUILayout.Label("• 检查 meta 文件是否存在"); GUILayout.Label("• 验证 GUID 格式"); GUILayout.Label("• 测试 AssetDatabase 加载"); GUILayout.Label("• 检查纹理导入设置"); GUILayout.Label("• 提供修复建议"); EditorGUILayout.EndScrollView(); } private void DiagnoseResource() { Debug.Log("========== 开始诊断资源 =========="); Debug.Log($"测试路径: {testPath}"); bool hasIssues = false; // 1. 检查文件是否存在 if (!File.Exists(testPath)) { Debug.LogError($"✗ 文件不存在: {testPath}"); hasIssues = true; } else { Debug.Log($"✓ 文件存在: {testPath}"); FileInfo fileInfo = new FileInfo(testPath); Debug.Log($" 文件大小: {fileInfo.Length} bytes"); } // 2. 检查 meta 文件 string metaPath = testPath + ".meta"; if (!File.Exists(metaPath)) { Debug.LogError($"✗ Meta文件不存在: {metaPath}"); Debug.LogWarning("建议: 重新运行导出工具或手动刷新AssetDatabase"); hasIssues = true; } else { Debug.Log($"✓ Meta文件存在: {metaPath}"); // 读取并验证 meta 文件内容 string metaContent = File.ReadAllText(metaPath); // 检查 GUID var guidMatch = System.Text.RegularExpressions.Regex.Match(metaContent, @"guid: ([a-f0-9]{32})"); if (guidMatch.Success) { Debug.Log($"✓ GUID: {guidMatch.Groups[1].Value}"); } else { Debug.LogError("✗ Meta文件中未找到有效的GUID"); hasIssues = true; } // 检查 TextureImporter 设置 if (metaContent.Contains("TextureImporter")) { Debug.Log(" 类型: TextureImporter"); } else { Debug.LogWarning(" 警告: 不是 TextureImporter 类型"); } } // 3. 测试 AssetDatabase 加载 Debug.Log("\n--- AssetDatabase 加载测试 ---"); Sprite sprite = AssetDatabase.LoadAssetAtPath(testPath); if (sprite != null) { Debug.Log($"✓ AssetDatabase.LoadAssetAtPath 成功加载 Sprite"); Debug.Log($" Sprite名称: {sprite.name}"); Debug.Log($" 纹理尺寸: {sprite.texture.width}x{sprite.texture.height}"); } else { Debug.LogError("✗ AssetDatabase.LoadAssetAtPath 返回 null"); Debug.LogWarning("建议: 检查资源导入设置或重新导入资源"); hasIssues = true; } Texture2D texture = AssetDatabase.LoadAssetAtPath(testPath); if (texture != null) { Debug.Log($"✓ AssetDatabase.LoadAssetAtPath 成功加载 Texture2D"); } else { Debug.LogWarning(" Texture2D 加载失败(可能是 Sprite 类型)"); } // 4. 检查 TextureImporter 设置 Debug.Log("\n--- TextureImporter 设置 ---"); TextureImporter importer = AssetImporter.GetAtPath(testPath) as TextureImporter; if (importer != null) { Debug.Log($" TextureType: {importer.textureType}"); Debug.Log($" Readable: {importer.isReadable}"); Debug.Log($" mipmapEnabled: {importer.mipmapEnabled}"); Debug.Log($" textureFormat: {importer.textureCompression}"); if (importer.textureType != TextureImporterType.Sprite) { Debug.LogWarning(" 警告: TextureType 不是 Sprite"); Debug.LogWarning(" 建议: 设置为 Sprite (2D and UI)"); } } else { Debug.LogError("✗ 无法获取 TextureImporter"); hasIssues = true; } // 5. 检查是否被 YooAsset 收集 Debug.Log("\n--- YooAsset 配置检查 ---"); if (testPath.StartsWith("Assets/Art_Out/")) { Debug.Log("✓ 路径在 Art_Out 目录下,应该被 YooAsset 收集"); } else { Debug.LogWarning("警告: 路径不在 Art_Out 目录下"); } // 总结 Debug.Log("\n========== 诊断完成 =========="); if (hasIssues) { Debug.LogWarning("发现问题,请根据上述建议修复"); } else { Debug.Log("未发现明显问题,资源应该可以正常加载"); } EditorUtility.DisplayDialog( "诊断完成", hasIssues ? "发现问题,请查看控制台日志" : "未发现明显问题,请查看控制台日志了解详情", "确定" ); } private void ForceRefreshAssetDatabase() { Debug.Log("========== 强制刷新 AssetDatabase =========="); EditorUtility.DisplayProgressBar("刷新资源", "正在刷新 AssetDatabase...", 0.5f); AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); EditorUtility.ClearProgressBar(); Debug.Log("AssetDatabase 刷新完成"); EditorUtility.DisplayDialog("刷新完成", "AssetDatabase 已强制刷新", "确定"); } }