Version: 2023.2

PrefabUtility

class in UnityEditor

切换到手册

描述

可用于任何预制件相关操作的实用程序类。

// 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 is placed in the root Assets 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.SaveAsPrefabAssetAndConnect(gameObject, localPath, InteractionMode.UserAction, 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); } }

静态变量

prefabInstanceUpdatedUnity calls this method automatically when Prefab instances in the Scene have been updated.

静态函数

ApplyAddedComponent将添加的组件应用于给定资源路径上的预制件资源。
ApplyAddedGameObject将添加的游戏对象应用于给定资源路径上的预制件资源。
ApplyAddedGameObjectsApplies the added GameObjects to the Prefab Asset at the given asset path.
ApplyObjectOverride将预制件实例组件或游戏对象上的所有重载属性应用于给定资源路径上的预制件资源。
ApplyPrefabInstance将预制件实例上的所有重载应用于其预制件资源。
ApplyPrefabInstancesApplies all overrides from a list of Prefab instances to their Prefab Assets.
ApplyPropertyOverride将预制件实例上的单个重载属性应用于给定资源路径上的预制件资源。
ApplyRemovedComponent从含有组件的预制件资源中,删除该组件。
ApplyRemovedGameObjectRemoves the GameObject from the source Prefab Asset.
ConvertToPrefabInstanceConvert the plain GameObject to a Prefab instance using the provided Prefab Asset root object.
ConvertToPrefabInstancesConvert an array of GameObjects to Prefab instances of the given Prefab Asset.
FindAllInstancesOfPrefabRetrieves the root GameObjects for all instances of the Prefab asset with root prefabRoot found in all currently loaded scenes. If prefabRoot is not a valid Prefab asset root GameObject, an ArgumentException is thrown.
GetAddedComponentsRetrieves a list of PrefabUtility.AddedComponent objects which contain information about added component overrides on the Prefab instance.
GetAddedGameObjectsRetrieves a list of PrefabUtility.AddedGameObject objects which contain information about added GameObjects on the Prefab instance.
GetCorrespondingObjectFromOriginalSourceRetrieves the object of origin for the given object.
GetCorrespondingObjectFromSourceRetrieves the corresponding asset object of source, or null if it can't be found.
GetCorrespondingObjectFromSourceAtPathRetrieves the corresponding object of the given object from a given Prefab Asset path.
GetIconForGameObjectRetrieves the icon for the given GameObject.
GetNearestPrefabInstanceRootRetrieves the GameObject that is the root of the nearest Prefab instance the object is part of.
GetObjectOverridesRetrieves a list of objects with information about object overrides on the Prefab instance.
GetOriginalSourceRootWhereGameObjectIsAddedUse this method to find the Prefab Asset root where a Prefab instance or Prefab Asset object was added originally.
GetOutermostPrefabInstanceRootRetrieves the GameObject that is the root of the outermost Prefab instance the object is part of.
GetPrefabAssetPathOfNearestInstanceRootRetrieves the asset path of the nearest Prefab instance root the specified object is part of.
GetPrefabAssetTypeRetrieves an enum value indicating the type of Prefab Asset, such as Regular Prefab, Model Prefab and Prefab Variant.
GetPrefabInstanceHandleRetrieves the PrefabInstance object for the outermost Prefab instance the provided object is part of.
GetPrefabInstanceStatusDetermines whether a Prefab instance is properly connected to its asset.
GetPropertyModificationsReturns all modifications that have been applied to a particular Prefab instance in a Scene or modifications for a Prefab instance in an Asset.See SetPropertyModifications for details about the fields of the returned PropertyModification objects.An alternative approach to getting property overrides information for a Prefab instance is to use the GetObjectOverrides API which also has Apply and Revert functionality.When using GetPropertyModifications bear in mind that: it will return both default and non-default overridesIt can return overrides for all child GameObjects and their Componentsit can return overrides that are no longer valid.
GetRemovedComponents返回对象的列表,其中包含预制件实例上已删除组件的相关信息。
GetRemovedGameObjectsReturns a list of objects with information about removed GameObject overrides on the Prefab instance.
HasManagedReferencesWithMissingTypesDetermines whether the object Prefab asset contains any MonoBehaviours with missing SerializeReference types.
HasPrefabInstanceAnyOverrides如果给定预制件实例有任何重载,则返回 true。
InstantiatePrefab将给定场景中的给定预制件实例化。
IsAddedComponentOverride是否将该组件作为重载添加到预制件实例?
IsAddedGameObjectOverride是否将此游戏对象作为子代添加到预制件实例?
IsAnyPrefabInstanceRoot该游戏对象是任何预制件实例的根?
IsDefaultOverride如果将给定修改视为默认重载,则返回 true。
IsOutermostPrefabInstanceRoot该游戏对象是预制件实例(不包括嵌套预制件)的根?
IsPartOfAnyPrefab如果给定对象是任何种类的预制件的一部分,则返回 true。
IsPartOfImmutablePrefab该对象是无法编辑的预制件的一部分?
IsPartOfModelPrefab如果给定对象是模型预制件资源或模型预制件实例的一部分,则返回 true。
IsPartOfNonAssetPrefabInstance如果给定对象是预制件实例的一部分而不是资源的一部分,则返回 true。
IsPartOfPrefabAsset如果给定对象是预制件资源的一部分,则返回 true。
IsPartOfPrefabInstance如果给定对象是预制件实例的一部分,则返回 true。
IsPartOfPrefabThatCanBeAppliedTo该对象是无法应用于的预制件的一部分?
IsPartOfRegularPrefab如果给定对象是常规预制件资源或预制件实例的一部分,则返回 true。
IsPartOfVariantPrefab如果给定对象是预制件变体资源或预制件变体实例的一部分,则返回 true。
IsPrefabAssetMissing如果给定对象是预制件实例的一部分,但是源资源缺失,则返回 true。
LoadPrefabContents将给定路径上的预制件资源加载到孤立场景中,并返回预制件的根游戏对象。
LoadPrefabContentsIntoPreviewScene将给定路径上的预制件资源加载到给定预览场景中,并返回预制件的根游戏对象。
MergePrefabInstanceForces a Prefab instance to merge with changes from the Prefab Asset.
RecordPrefabInstancePropertyModifications指示记录对预制件实例所做的修改。
RemoveUnusedOverridesThis method identifies and removes all unused overrides from a list of Prefab Instance roots. See the manual Unused Overides for more detail.
ReplacePrefabAssetOfPrefabInstanceReplace the Prefab Asset for a Prefab instance that exists in a Scene or for a nested Prefab instance inside another Prefab.
ReplacePrefabAssetOfPrefabInstancesReplace the Prefab Asset for an array of Prefab instances that exists in Scenes or for nested Prefab instances inside another Prefab.
RevertAddedComponent删除预制件实例上添加的此组件。
RevertAddedGameObject从预制件实例删除所添加的此游戏对象。
RevertObjectOverride还原预制件实例组件或游戏对象上的所有重载属性。
RevertPrefabInstance还原预制件实例上的所有重载。
RevertPropertyOverride还原预制件实例上的单属性重载。
RevertRemovedComponent将删除的该组件添加回预制件实例。
RevertRemovedGameObjectAdds this removed GameObject back on the Prefab instance.
SaveAsPrefabAsset使用该函数,在给定路径上,从给定的游戏对象创建一个预制件资源(包括场景中的任何子项),而不修改输入对象。
SaveAsPrefabAssetAndConnectUse this function to create a Prefab Asset at the given path from the given GameObject, including any children in the Scene and at the same time make the given GameObject into an instance of the new Prefab.
SavePrefabAsset使用此函数将内存中存在的现有预制件资源版本保存回磁盘。
SetPropertyModificationsAssigns a set of PropertyModification objects to a target Prefab instance relative to its source Prefab Asset.
UnloadPrefabContents从内存中释放以前随 LoadPrefabContents 加载的预制件的内容。
UnpackAllInstancesOfPrefabUnpacks all instances of a given Prefab Asset root GameObject in all open scenes so that all instances are replaced with the contents of the Prefab Asset while retaining all override values.
UnpackPrefabInstance将给定的预制件实例解压,以便将其替换为预制件资源的内容,同时保留所有重载值。
UnpackPrefabInstanceAndReturnNewOutermostRoots此函数将使用 unpackMode 指定的行为,对给定的预制件实例进行解压缩。

Events

prefabInstanceRevertedUnity calls this method automatically after a Prefab instance has been reverted.
prefabInstanceRevertingUnity calls this method automatically before a Prefab instance is reverted.
prefabInstanceUnpackedUnity calls this method automatically after a Prefab instance is unpacked.
prefabInstanceUnpackingUnity calls this method automatically before a Prefab instance is unpacked.

委托

PrefabInstanceUpdated在场景中的预制件实例完成更新后所调用的方法的委托。