{"id":470,"date":"2019-10-23T20:29:55","date_gmt":"2019-10-23T12:29:55","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=470"},"modified":"2019-10-27T15:32:36","modified_gmt":"2019-10-27T07:32:36","slug":"%e8%87%aa%e5%8a%a8%e5%b0%86%e6%96%ad%e9%93%be%e7%9a%84prefab%e6%9b%bf%e6%8d%a2%e4%b8%ba%e7%9c%9fprefab","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=470","title":{"rendered":"[Unity]\u81ea\u52a8\u5c06\u65ad\u94fe\u7684Prefab\u66ff\u6362\u4e3a\u771fPrefab"},"content":{"rendered":"\n<p>\u73af\u5883\uff1aUnity2018.4<\/p>\n\n\n\n<p>\u8d77\u56e0\uff1a\u7f8e\u672f\u5728Runtime\u73af\u5883\uff0c\u62c9\u51fa\u7684Prefab\u662f\u5b9e\u4f8b\u5316\u4e4b\u540e\u7684\uff08\u65ad\u94fe\u4e86\uff09<br>\u73b0\u5728\u8981\u5c06\u539f\u6765\u7684Perfab\u66ff\u6362\u4e3a\u771f\u7684Prefab\uff08\u770b\u8d77\u6765\u662f\u84dd\u8272\u7684\uff09<\/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 EditorPrefabToolV2\n{\n    static private readonly string[] CFindPaths = new string[]\n    {\n        \"Assets\/Arts\/\",\n    };\n\n    private static bool InFindPath(string assetPath)\n    {\n        foreach(var cfp in CFindPaths)\n        {\n            if (assetPath.StartsWith(cfp))\n            {\n                return true;\n            }\n        }\n        return false;\n    }\n\n    private static string FindAsset(string assetName)\n    {\n        var allAssetPaths = AssetDatabase.GetAllAssetPaths();\n        foreach(var assetPath in allAssetPaths)\n        {\n            if ( System.IO.Path.GetExtension(assetPath).ToLower() != \".prefab\")\n            {\n                continue;\n            }\n            \n            if (!InFindPath(assetPath))\n            {\n                continue;\n            }\n\n            string fname = System.IO.Path.GetFileNameWithoutExtension(assetPath).ToLower().Trim();\n            if (fname == assetName)\n            {\n                return assetPath;\n            }\n        }\n\n        return string.Empty;\n    }\n\n    private static GameObject ReplaceGo(GameObject sobj)\n    {  \n        var pos = sobj.transform.position;\n        var rot = sobj.transform.rotation;\n        var parentObj = sobj.transform.parent;\n        if (parentObj == null)\n        {\n            return null;\n        }\n\n        var sindex = sobj.transform.GetSiblingIndex();\n        var sname = sobj.name.Trim();\n        for(int i=30; i>=0; i--)\n        {\n            sname = sname.Replace(\"(\" + i.ToString() + \")\", \"\");\n        }\n        sname = sname.ToLower().Trim();\n\n        var findAssetPath = FindAsset(sname);\n        if (string.IsNullOrEmpty(findAssetPath))\n        {\n            Log.Warning(\"\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u8d44\u6e90\uff1a{0}\", sname);\n            return null;\n        }\n\n        var prefabTemplate = AssetDatabase.LoadAssetAtPath&lt;GameObject>(findAssetPath);\n        if (prefabTemplate == null)\n        {\n            Log.Warning(\"\u8f7d\u5165\u5931\u8d251:{0}\", findAssetPath);\n            return null;\n        }\n\n        var newObj = PrefabUtility.InstantiatePrefab(prefabTemplate) as GameObject;\n        if (newObj == null)\n        {\n            Log.Warning(\"\u8f7d\u5165\u5931\u8d252:{0}\", findAssetPath);\n            return null;\n        }\n\n        newObj.transform.SetParent(parentObj, false);\n        newObj.transform.position = pos;\n        newObj.transform.rotation = rot;\n        newObj.transform.SetSiblingIndex(sindex);\n\n        Undo.RegisterCreatedObjectUndo(newObj, \"created rig\");\n\n        Log.Logic(\"&lt;color=blue>\u66ff\u6362\u6210\u529f: {0} -> {1}&lt;\/color>\", sname, findAssetPath);\n        return newObj;\n    }\n\n\n    [MenuItem(\"GameObject\/UwMapEditor\/\u66ff\u6362\u4e3aPrefab %#V\")]\n    static public void ReplacePrefab()\n    {\n        var sobj = Selection.gameObjects;\n        if ( sobj == null || sobj.Length == 0)\n        {\n            return;\n        }\n\n        List&lt;GameObject> delObjs = new List&lt;GameObject>();\n        List&lt;GameObject> addObjs = new List&lt;GameObject>();\n        for(int i=0; i&lt;sobj.Length; ++i)\n        {\n            var addObj = ReplaceGo(sobj[i]);\n            if (addObj != null)\n            {\n                addObjs.Add(addObj);\n                delObjs.Add(sobj[i]);\n            }\n        }\n\n        foreach(var obj in delObjs)\n        {\n            Undo.DestroyObjectImmediate(obj);\n        }\n\n        Selection.objects = addObjs.ToArray();\n\n        foreach(var obj in addObjs)\n        {\n            UnityEditor.EditorUtility.SetDirty(obj);\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u73af\u5883\uff1aUnity2018.4 \u8d77\u56e0\uff1a\u7f8e\u672f\u5728Runtime\u73af\u5883\uff0c\u62c9\u51fa\u7684Prefab\u662f\u5b9e\u4f8b\u5316\u4e4b\u540e\u7684\uff08\u65ad\u94fe\u4e86\uff09\u73b0\u5728\u8981 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","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\/470"}],"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=470"}],"version-history":[{"count":3,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/470\/revisions"}],"predecessor-version":[{"id":558,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/470\/revisions\/558"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}