instanceRoot | 要保存为预制资源的 GameObject。 |
assetPath | 保存预制资源的路径。 |
success | 保存操作的结果,成功或失败。将其与控制台日志结合使用,以更深入地了解保存过程。 |
GameObject 保存的预制资源的根 GameObject(如果可用)。
根据给定的 GameObject 在给定路径下创建预制资源,包括场景中的任何子对象,而不会修改输入对象。
如果某些子对象是预制实例,它们将自动嵌套在新预制资源中。
输入对象必须是普通 GameObject 或预制实例的最外层根。它不能是预制实例内的子对象。
如果输入对象是预制实例根,则生成的预制将是预制变体。如果您想创建一个新的唯一预制,则需要先解包预制实例。
返回的对象是保存的预制资源的根 GameObject(如果可用)。如果编辑器当前处于资产编辑批处理操作的中间,由AssetDatabase.StartAssetEditing和AssetDatabase.StopAssetEditing控制,则保存后不会立即导入资产。在这种情况下,即使保存成功,SaveAsPrefabAsset 也会返回 null,因为保存的预制资源尚未重新导入,因此尚不可用。
如果您要覆盖现有的预制,Unity 会尝试保留对预制本身以及预制各个部分(如子 GameObject 和组件)的引用。为此,它会匹配新保存的预制和现有预制之间 GameObject 的名称。
注意:由于此匹配仅通过名称进行,如果预制层次结构中有多个具有相同名称的 GameObject,则无法预测将匹配哪个。因此,如果您需要确保在覆盖现有预制时保留引用,则必须确保预制中的所有 GameObject 具有唯一的名称。
另请注意:如果您在覆盖现有预制时保留对现有组件的引用,也可能会遇到类似的问题,如果预制中的单个 GameObject 附加了多个相同类型的组件。在这种情况下,您无法预测哪些组件将与现有引用匹配。
其他资源:PrefabUtility.SaveAsPrefabAssetAndConnect 和 解包预制实例。
// 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). // Prefab(s) are placed in the "Prefabs" folder. using System.IO; 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) { // Create folder Prefabs and set the path as within the Prefabs folder, // and name it as the GameObject's name with the .Prefab format if (!Directory.Exists("Assets/Prefabs")) AssetDatabase.CreateFolder("Assets", "Prefabs"); string localPath = "Assets/Prefabs/" + 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 and log whether Prefab was saved successfully. bool prefabSuccess; PrefabUtility.SaveAsPrefabAsset(gameObject, localPath, out prefabSuccess); if (prefabSuccess == true) Debug.Log("Prefab was saved successfully"); else Debug.Log("Prefab failed to save" + prefabSuccess); } }
// 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.