instanceRoot | 要保存为预制件并进入预制件实例的游戏对象。 |
assetPath | 要在其中保存预制件的路径。 |
action | 用于此操作的交互模式。 |
success | 保存操作的结果(成功或不成功)。将其与控制台日志一起使用,从而进一步了解保存过程。 |
GameObject 已保存的预制件资源(如果可用)的根游戏对象。
使用该函数,在给定路径上,从给定的游戏对象创建一个预制件资源(包括场景中的任何子项),同时让给定游戏对象进入新预制件的实例。
如果某些子项是预制件实例,则会自动将这些子项嵌套在新预制件内。
输入对象必须是普通游戏对象或预制件实例的最外层根。它不能是预制件实例中的子项。
如果输入对象是预制件实例根,则生成的预制件将是预制件变体。反之,如果要新建唯一的预制件,需要首先解压该预制件实例。
返回对象是已保存的预制件资源(如果可用)的根游戏对象。如果编辑器当前正在进行资源编辑批处理操作(使用 AssetDatabase.StartAssetEditing 和 AssetDatabase.StopAssetEditing 控制),则不会在保存时立即导入资源。在此情况下,SaveAsPrefabAsset 会返回 null,即使保存成功也是如此,因为保存的预制件资源尚未重新导入,从而尚不可用。
如果要对现有预制件进行保存,Unity 会尝试保留对预制件本身的引用以及预制件的各个部分(如子游戏对象和组件)。要执行此操作,它在新保存的预制件与现有预制件之间匹配游戏对象的名称。
注意:因为仅按名称进行此匹配,所以如果预制件的层级视图中有多个同名的游戏对象,则无法预测会匹配哪个游戏对象。因此,如果需要确保在对现有预制件进行保存时保留引用,则必须确保预制件中的所有游戏对象都具有唯一名称。
另请注意:当预制件中的单个游戏对象附加了多个相同组件类型时,如果在对现有预制件进行保存时保留对现有组件的引用,则可能会遇到相似问题。在此情况下,无法预测哪个组件会与现有引用匹配。
另请参阅:SaveAsPrefabAsset、UnpackPrefabInstance。
// Create a folder (right click in the Assets directory, click Create>New Folder) // and name it “Editor” if one doesn’t exist already. Place this script in that folder.
// This script creates a new menu item Examples>Create Prefab in the main menu. // Use it to create Prefab(s) from the selected GameObject(s). // It will be placed in the root Assets folder.
using UnityEngine; using UnityEditor;
public class Example { // Creates a new menu item 'Examples > Create Prefab' in the main menu. [MenuItem("Examples/Create Prefab")] static void CreatePrefab() { // Keep track of the currently selected GameObject(s) GameObject[] objectArray = Selection.gameObjects;
// Loop through every GameObject in the array above foreach (GameObject gameObject in objectArray) { // Set the path as within the Assets folder, // and name it as the GameObject's name with the .Prefab format string localPath = "Assets/" + gameObject.name + ".prefab";
// Make sure the file name is unique, in case an existing Prefab has the same name. localPath = AssetDatabase.GenerateUniqueAssetPath(localPath);
// Create the new Prefab. PrefabUtility.SaveAsPrefabAssetAndConnect(gameObject, localPath, InteractionMode.UserAction); } }
// Disable the menu item if no selection is in place. [MenuItem("Examples/Create Prefab", true)] static bool ValidateCreatePrefab() { return Selection.activeGameObject != null && !EditorUtility.IsPersistent(Selection.activeGameObject); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.