可用于任何预制件相关操作的实用程序类。
// 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); } }
prefabInstanceUpdated | 在场景中的预制件实例完成更新后调用。 |
ApplyAddedComponent | 将添加的组件应用于给定资源路径上的预制件资源。 |
ApplyAddedGameObject | 将添加的游戏对象应用于给定资源路径上的预制件资源。 |
ApplyObjectOverride | 将预制件实例组件或游戏对象上的所有重载属性应用于给定资源路径上的预制件资源。 |
ApplyPrefabInstance | 将预制件实例上的所有重载应用于其预制件资源。 |
ApplyPropertyOverride | 将预制件实例上的单个重载属性应用于给定资源路径上的预制件资源。 |
ApplyRemovedComponent | 从含有组件的预制件资源中,删除该组件。 |
GetAddedComponents | 返回 PrefabUtility.AddedComponent 对象的列表;这些对象包含预制件实例上添加的组件重载的信息。 |
GetAddedGameObjects | 返回 PrefabUtility.AddedGameObject 对象的列表;这些对象包含预制件实例上添加的游戏对象的信息。 |
GetCorrespondingObjectFromOriginalSource | 返回给定对象的原始对象。 |
GetCorrespondingObjectFromSource | 返回 source 的相应资源对象,如果找不到,则返回 null。 |
GetCorrespondingObjectFromSourceAtPath | 从给定的预制件资源路径,返回给定对象的相应对象。 |
GetIconForGameObject | 返回给定游戏对象的图标。 |
GetNearestPrefabInstanceRoot | 返回游戏对象(它是对象所属的最近预制件实例的根)。 |
GetObjectOverrides | 返回对象的列表,其中包含预制件实例上对象重载的相关信息。 |
GetOutermostPrefabInstanceRoot | 返回游戏对象(它是对象所属的最外层预制件实例的根)。 |
GetPrefabAssetPathOfNearestInstanceRoot | 返回指定对象所属的最近预制件实例根的资源路径。 |
GetPrefabAssetType | 枚举值,指示预制件资源类型(例如常规预制件、模型预制件和预制件变体)。 |
GetPrefabInstanceHandle | 此函数将为提供的对象所属的最外层预制件实例提供 PrefabInstance 对象。 |
GetPrefabInstanceStatus | 枚举值,其中包含有关预制件实例是否已正确连接到其资源的状态。 |
GetPropertyModifications | 提取应用于预制件实例的所有修改(与父预制件相比)。 |
GetRemovedComponents | 返回对象的列表,其中包含预制件实例上已删除组件的相关信息。 |
HasPrefabInstanceAnyOverrides | 如果给定预制件实例有任何重载,则返回 true。 |
InstantiateAttachedAsset | 将预制件所引用的资源实例化,并在预制件实例上使用该资源。 |
InstantiatePrefab | 将给定场景中的给定预制件实例化。 |
IsAddedComponentOverride | 是否将该组件作为重载添加到预制件实例? |
IsAddedGameObjectOverride | 是否将此游戏对象作为子代添加到预制件实例? |
IsAnyPrefabInstanceRoot | 该游戏对象是任何预制件实例的根? |
IsDefaultOverride | 如果将给定修改视为默认重载,则返回 true。 |
IsDisconnectedFromPrefabAsset | 如果给定对象是缺失 PrefabInstance 对象的实例的一部分,但是给定对象具有有效的相应对象,则返回 true。 |
IsOutermostPrefabInstanceRoot | 该游戏对象是预制件实例(不包括嵌套预制件)的根? |
IsPartOfAnyPrefab | 如果给定对象是任何种类的预制件的一部分,则返回 true。 |
IsPartOfImmutablePrefab | 该对象是无法编辑的预制件的一部分? |
IsPartOfModelPrefab | 如果给定对象是模型预制件资源或模型预制件实例的一部分,则返回 true。 |
IsPartOfNonAssetPrefabInstance | 如果给定对象是预制件实例的一部分而不是资源的一部分,则返回 true。 |
IsPartOfPrefabAsset | 如果给定对象是预制件资源的一部分,则返回 true。 |
IsPartOfPrefabInstance | 如果给定对象是预制件实例的一部分,则返回 true。 |
IsPartOfPrefabThatCanBeAppliedTo | 该对象是无法应用于的预制件的一部分? |
IsPartOfRegularPrefab | 如果给定对象是常规预制件资源或预制件实例的一部分,则返回 true。 |
IsPartOfVariantPrefab | 如果给定对象是预制件变体资源或预制件变体实例的一部分,则返回 true。 |
IsPrefabAssetMissing | 如果给定对象是预制件实例的一部分,但是源资源缺失,则返回 true。 |
LoadPrefabContents | 将给定路径上的预制件资源加载到孤立场景中,并返回预制件的根游戏对象。 |
LoadPrefabContentsIntoPreviewScene | 将给定路径上的预制件资源加载到给定预览场景中,并返回预制件的根游戏对象。 |
RecordPrefabInstancePropertyModifications | 指示记录对预制件实例所做的修改。 |
RevertAddedComponent | 删除预制件实例上添加的此组件。 |
RevertAddedGameObject | 从预制件实例删除所添加的此游戏对象。 |
RevertObjectOverride | 还原预制件实例组件或游戏对象上的所有重载属性。 |
RevertPrefabInstance | 还原预制件实例上的所有重载。 |
RevertPropertyOverride | 还原预制件实例上的单属性重载。 |
RevertRemovedComponent | 将删除的该组件添加回预制件实例。 |
SaveAsPrefabAsset | 使用该函数,在给定路径上,从给定的游戏对象创建一个预制件资源(包括场景中的任何子项),而不修改输入对象。 |
SaveAsPrefabAssetAndConnect | 使用该函数,在给定路径上,从给定的游戏对象创建一个预制件资源(包括场景中的任何子项),同时让给定游戏对象进入新预制件的实例。 |
SavePrefabAsset | 使用此函数将内存中存在的现有预制件资源版本保存回磁盘。 |
SetPropertyModifications | 指定应用于预制件实例的所有修改(与父预制件相比)。 |
UnloadPrefabContents | 从内存中释放以前随 LoadPrefabContents 加载的预制件的内容。 |
UnpackPrefabInstance | 将给定的预制件实例解压,以便将其替换为预制件资源的内容,同时保留所有重载值。 |
UnpackPrefabInstanceAndReturnNewOutermostRoots | 此函数将使用 unpackMode 指定的行为,对给定的预制件实例进行解压缩。 |
PrefabInstanceUpdated | 在场景中的预制件实例完成更新后所调用的方法的委托。 |
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.