Version: 2021.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将预制件实例上的所有重载应用于其预制件资源。
ApplyPropertyOverride将预制件实例上的单个重载属性应用于给定资源路径上的预制件资源。
ApplyRemovedComponent从含有组件的预制件资源中,删除该组件。
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.
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.
GetPropertyModificationsExtracts all modifications that are applied to the Prefab instance compared to the parent Prefab.
GetRemovedComponents返回对象的列表,其中包含预制件实例上已删除组件的相关信息。
HasManagedReferencesWithMissingTypesDetermines whether the object Prefab asset contains any MonoBehaviours with missing SerializeReference types.
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将给定路径上的预制件资源加载到给定预览场景中,并返回预制件的根游戏对象。
MergePrefabInstanceForces a Prefab instance to merge with changes from the Prefab Asset.
RecordPrefabInstancePropertyModifications指示记录对预制件实例所做的修改。
RevertAddedComponent删除预制件实例上添加的此组件。
RevertAddedGameObject从预制件实例删除所添加的此游戏对象。
RevertObjectOverride还原预制件实例组件或游戏对象上的所有重载属性。
RevertPrefabInstance还原预制件实例上的所有重载。
RevertPropertyOverride还原预制件实例上的单属性重载。
RevertRemovedComponent将删除的该组件添加回预制件实例。
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使用此函数将内存中存在的现有预制件资源版本保存回磁盘。
SetPropertyModifications指定应用于预制件实例的所有修改(与父预制件相比)。
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 指定的行为,对给定的预制件实例进行解压缩。

委托

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