可用于任何预制件相关操作的实用程序类。
// 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); } }
prefabInstanceUpdated | Unity calls this method automatically when Prefab instances in the Scene have been updated. |
ApplyAddedComponent | 将添加的组件应用于给定资源路径上的预制件资源。 |
ApplyAddedGameObject | 将添加的游戏对象应用于给定资源路径上的预制件资源。 |
ApplyAddedGameObjects | Applies the added GameObjects to the Prefab Asset at the given asset path. |
ApplyObjectOverride | 将预制件实例组件或游戏对象上的所有重载属性应用于给定资源路径上的预制件资源。 |
ApplyPrefabInstance | 将预制件实例上的所有重载应用于其预制件资源。 |
ApplyPrefabInstances | Applies all overrides from a list of Prefab instances to their Prefab Assets. |
ApplyPropertyOverride | 将预制件实例上的单个重载属性应用于给定资源路径上的预制件资源。 |
ApplyRemovedComponent | 从含有组件的预制件资源中,删除该组件。 |
ApplyRemovedGameObject | Removes the GameObject from the source Prefab Asset. |
ConvertToPrefabInstance | Convert the plain GameObject to a Prefab instance using the provided Prefab Asset root object. |
ConvertToPrefabInstances | Convert an array of GameObjects to Prefab instances of the given Prefab Asset. |
FindAllInstancesOfPrefab | Retrieves 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. |
GetAddedComponents | Retrieves a list of PrefabUtility.AddedComponent objects which contain information about added component overrides on the Prefab instance. |
GetAddedGameObjects | Retrieves a list of PrefabUtility.AddedGameObject objects which contain information about added GameObjects on the Prefab instance. |
GetCorrespondingObjectFromOriginalSource | Retrieves the object of origin for the given object. |
GetCorrespondingObjectFromSource | Retrieves the corresponding asset object of source, or null if it can't be found. |
GetCorrespondingObjectFromSourceAtPath | Retrieves the corresponding object of the given object from a given Prefab Asset path. |
GetIconForGameObject | Retrieves the icon for the given GameObject. |
GetNearestPrefabInstanceRoot | Retrieves the GameObject that is the root of the nearest Prefab instance the object is part of. |
GetObjectOverrides | Retrieves a list of objects with information about object overrides on the Prefab instance. |
GetOriginalSourceRootWhereGameObjectIsAdded | Use this method to find the Prefab Asset root where a Prefab instance or Prefab Asset object was added originally. |
GetOutermostPrefabInstanceRoot | Retrieves the GameObject that is the root of the outermost Prefab instance the object is part of. |
GetPrefabAssetPathOfNearestInstanceRoot | Retrieves the asset path of the nearest Prefab instance root the specified object is part of. |
GetPrefabAssetType | Retrieves an enum value indicating the type of Prefab Asset, such as Regular Prefab, Model Prefab and Prefab Variant. |
GetPrefabInstanceHandle | Retrieves the PrefabInstance object for the outermost Prefab instance the provided object is part of. |
GetPrefabInstanceStatus | Determines whether a Prefab instance is properly connected to its asset. |
GetPropertyModifications | Extracts all modifications that are applied to the Prefab instance compared to the parent Prefab. |
GetRemovedComponents | 返回对象的列表,其中包含预制件实例上已删除组件的相关信息。 |
GetRemovedGameObjects | Returns a list of objects with information about removed GameObject overrides on the Prefab instance. |
HasManagedReferencesWithMissingTypes | Determines whether the object Prefab asset contains any MonoBehaviours with missing SerializeReference types. |
HasPrefabInstanceAnyOverrides | 如果给定预制件实例有任何重载,则返回 true。 |
InstantiateAttachedAsset | 将预制件所引用的资源实例化,并在预制件实例上使用该资源。 |
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 | 将给定路径上的预制件资源加载到给定预览场景中,并返回预制件的根游戏对象。 |
MergePrefabInstance | Forces a Prefab instance to merge with changes from the Prefab Asset. |
RecordPrefabInstancePropertyModifications | 指示记录对预制件实例所做的修改。 |
ReplacePrefabAssetOfPrefabInstance | Replace the Prefab Asset for a Prefab instance that exists in a Scene or for a nested Prefab instance inside another Prefab. |
ReplacePrefabAssetOfPrefabInstances | Replace 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 | 将删除的该组件添加回预制件实例。 |
RevertRemovedGameObject | Adds this removed GameObject back on the Prefab instance. |
SaveAsPrefabAsset | 使用该函数,在给定路径上,从给定的游戏对象创建一个预制件资源(包括场景中的任何子项),而不修改输入对象。 |
SaveAsPrefabAssetAndConnect | Use 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 加载的预制件的内容。 |
UnpackAllInstancesOfPrefab | Unpacks 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 指定的行为,对给定的预制件实例进行解压缩。 |
prefabInstanceUnpacked | Unity calls this method automatically after a Prefab instance is unpacked. |
prefabInstanceUnpacking | Unity calls this method automatically before a Prefab instance is unpacked. |
PrefabInstanceUpdated | 在场景中的预制件实例完成更新后所调用的方法的委托。 |