{"id":648,"date":"2019-11-01T21:00:21","date_gmt":"2019-11-01T13:00:21","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=648"},"modified":"2020-09-27T17:04:57","modified_gmt":"2020-09-27T09:04:57","slug":"unity%e6%89%ab%e6%8f%8f%e9%a1%b9%e7%9b%ae%e4%b8%ad%e6%97%a0%e6%95%88%e7%9a%84prefab","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=648","title":{"rendered":"[Unity]\u626b\u63cf\u9879\u76ee\u4e2d\u65e0\u6548\u7684Prefab"},"content":{"rendered":"\n<p>\u5982\u679cPrefab\u542b\u6709\u4e22\u5931\u7684Prefab\uff0c\u5728\u6253AssetBundles\u65f6\uff0cUnity\u6709\u673a\u7387Creash\uff0c\u6240\u4ee5\u6dfb\u52a0\u5de5\u5177\u52a0\u4ee5\u68c0\u6d4b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\nusing UnityEditor;\n\npublic static class EditorMissingPrefabScanTool\n{\n    static public void CheckMissingPrefab(string assetPath, GameObject go, ref HashSet&lt;string> output)\n    {\n        if (go.name.Contains(\"Missing Prefab\"))\n        {\n            output.Add(string.Format(\"{0} \u542b\u6709\u65e0\u6548\u7684Prefab\\n\", assetPath));\n            return;\n        }\n\n        for(int i=0; i&lt;go.transform.childCount; ++i)\n        {\n            CheckMissingPrefab( assetPath, go.transform.GetChild(i).gameObject, ref output);\n        }\n    }\n\n    static public string GetMissingPrefabLog()\n    {\n        HashSet&lt;string> allLog = new HashSet&lt;string>();\n        System.Text.StringBuilder slog = new System.Text.StringBuilder();\n        var allPrefabFiles = System.IO.Directory.GetFiles(\"Assets\/\", \"*.prefab\", System.IO.SearchOption.AllDirectories);\n        for(int i=0; i&lt;allPrefabFiles.Length; ++i)\n        {\n            EditorUtility.DisplayProgressBar(\"\u626b\u63cf\", string.Format(\"\u6b63\u5728\u626b\u63cf{0}\/{1}\", i, allPrefabFiles.Length), \n                (float)i \/ allPrefabFiles.Length);\n            var prefabFilename = allPrefabFiles[i];\n            var go = AssetDatabase.LoadAssetAtPath&lt;GameObject>(prefabFilename);\n            CheckMissingPrefab(prefabFilename, go, ref allLog);\n        }\n        EditorUtility.ClearProgressBar();\n\n        foreach(var str in allLog)\n        {\n            slog.Append(str);\n        }\n        return slog.ToString();\n    }\n\n    [MenuItem(\"Tool\/\u626b\u63cf\u65e0\u6548Prefab\")]\n    static public void Tool()\n    {\n        var str = GetMissingPrefabLog();\n        if (str.Length > 0)\n        {\n            Log.Error(str);\n            EditorUtility.DisplayDialog(\"\u7ed3\u679c\", \"\u626b\u63cf\u5230\u5f02\u5e38Prefab\uff0c\u8bf7\u67e5\u770bConsole\", \"OK\");\n        }\n        else\n        {\n            EditorUtility.DisplayDialog(\"\u7ed3\u679c\", \"\u6240\u6709Prefab\u5747\u6b63\u5e38\", \"OK\");\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679cPrefab\u542b\u6709\u4e22\u5931\u7684Prefab\uff0c\u5728\u6253AssetBundles\u65f6\uff0cUnity\u6709\u673a\u7387Creash\uff0c\u6240\u4ee5\u6dfb\u52a0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[18],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/648"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=648"}],"version-history":[{"count":2,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/648\/revisions"}],"predecessor-version":[{"id":650,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/648\/revisions\/650"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}