{"id":576,"date":"2011-03-16T16:57:32","date_gmt":"2011-03-16T16:57:32","guid":{"rendered":""},"modified":"2013-02-27T12:05:11","modified_gmt":"2013-02-27T04:05:11","slug":"576","status":"publish","type":"post","link":"https:\/\/1vr.cn\/?p=576","title":{"rendered":"Unity\u4e2d\u4ece\u76ee\u524d\u9009\u62e9\u5bf9\u8c61\u521b\u5efa\u9884\u8bbe(Prefab)\u811a\u672c"},"content":{"rendered":"<p>\u5728Unity\u4e2d\u9009\u62e9\u4e00\u4e2a\u6e38\u620f\u5bf9\u8c61,\u7136\u540e\u901a\u8fc7\u8fd9\u4e2a\u811a\u672c\u6765\u5feb\u901f\u751f\u6210\u4e00\u4e2aPrefab\u9884\u8bbe.<br \/>\u5c06\u811a\u672c\u653e\u5728Editor\u6587\u4ef6\u5939\u4e2d,\u9009\u62e9\u6e38\u620f\u5bf9\u8c61,\u652f\u6301\u591a\u9009,\u7136\u540e\u9009\u62e9\u83dc\u5355GameObject &gt; Create Prefab From Selection,\u5c31\u4f1a\u5728\u6839\u76ee\u5f55\u751f\u6210\u4e00\u4e2a\u9884\u8bbe,\u540d\u5b57\u548c\u9009\u62e9\u7684\u6e38\u620f\u5bf9\u8c61\u4e00\u6837.\u5f53\u5df2\u7ecf\u5b58\u5728\u540c\u540d\u7684\u9884\u8bbe,\u5c06\u4f1a\u63d0\u793a\u662f\u5426\u8986\u76d6.<\/p>\n<p>\u811a\u672c\u540d:CreatePrefabFromSelected.cs<\/p>\n<p><code>using UnityEditor;<br \/>using UnityEngine;<br \/>using System.Collections;<\/p>\n<p>class CreatePrefabFromSelected : ScriptableObject<br \/>{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;const string menuTitle = \"GameObject\/Create Prefab From Selected\";<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/\/ &lt;summary&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/\/ Creates a prefab from the selected game object.<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/\/ &lt;\/summary&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;[MenuItem(menuTitle)]<br \/>&nbsp;&nbsp;&nbsp;&nbsp;static void CreatePrefab()<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GameObject[] obj = Selection.gameObjects;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach (GameObject go in obj)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string name = go.name;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string localPath = \"Assets\/\" + name + \".prefab\";<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject)))<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (EditorUtility.DisplayDialog(\"Are you sure?\", \"The prefab already exists. Do you want to overwrite it?\", \"Yes\", \"No\"))<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;createNew(go, localPath);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;createNew(go, localPath);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;static void createNew(GameObject obj, string localPath)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object prefab = EditorUtility.CreateEmptyPrefab(localPath);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EditorUtility.ReplacePrefab(obj, prefab);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AssetDatabase.Refresh();<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DestroyImmediate(obj);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GameObject clone = EditorUtility.InstantiatePrefab(prefab) as GameObject; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/\/ &lt;summary&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/\/ Validates the menu.<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/\/ &lt;\/summary&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/\/ &lt;remarks&gt;The item will be disabled if no game object is selected.&lt;\/remarks&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;[MenuItem(menuTitle, true)]<br \/>&nbsp;&nbsp;&nbsp;&nbsp;static bool ValidateCreatePrefab()<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return Selection.activeGameObject != null;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>}<br \/><\/code><\/p>\n<p>\u539f\u6587:<a href=\"http:\/\/www.unifycommunity.com\/wiki\/index.php?title=Cr\" target=\"_blank\" rel=\"external\">http:\/\/www.unifycommunity.com\/wiki\/index.php?title=Cr<\/a>&#101;atePrefabFromSelected<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Unity\u4e2d\u9009\u62e9\u4e00\u4e2a\u6e38\u620f\u5bf9\u8c61,\u7136\u540e\u901a\u8fc7\u8fd9\u4e2a\u811a\u672c\u6765\u5feb\u901f\u751f\u6210\u4e00\u4e2aPrefab\u9884\u8bbe.\u5c06\u811a\u672c\u653e\u5728Editor\u6587\u4ef6\u5939\u4e2d &hellip; <a href=\"https:\/\/1vr.cn\/?p=576\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Unity\u4e2d\u4ece\u76ee\u524d\u9009\u62e9\u5bf9\u8c61\u521b\u5efa\u9884\u8bbe(Prefab)\u811a\u672c<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-576","post","type-post","status-publish","format-standard","hentry","without-featured-image"],"_links":{"self":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts\/576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=576"}],"version-history":[{"count":1,"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts\/576\/revisions"}],"predecessor-version":[{"id":694,"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts\/576\/revisions\/694"}],"wp:attachment":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}