Utility class for any Prefab related operations.
//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 and a new menu item in the Editor window // Use the new menu item to create a Prefab at the given path. If a Prefab already exists it asks if you want to replace it //Click on a GameObject in your Hierarchy, then go to Examples>Create Prefab to see it in action.
using UnityEngine; using UnityEditor;
public class Example : EditorWindow { //Creates a new menu (Examples) with a menu item (Create Prefab) [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";
//Check if the Prefab and/or name already exists at the path if (AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject))) { //Create dialog to ask if User is sure they want to overwrite existing Prefab if (EditorUtility.DisplayDialog("Are you sure?", "The Prefab already exists. Do you want to overwrite it?", "Yes", "No")) //If the user presses the yes button, create the Prefab { CreateNew(gameObject, localPath); } } //If the name doesn't exist, create the new Prefab else { Debug.Log(gameObject.name + " is not a Prefab, will convert"); CreateNew(gameObject, localPath); } } }
// Disable the menu item if no selection is in place [MenuItem("Examples/Create Prefab", true)] static bool ValidateCreatePrefab() { return Selection.activeGameObject != null; }
static void CreateNew(GameObject obj, string localPath) { //Create a new Prefab at the path given Object prefab = PrefabUtility.CreatePrefab(localPath, obj); PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab); } }
prefabInstanceUpdated | Called after Prefab instances in the Scene have been updated. |
ApplyAddedComponent | Applies the added component to the Prefab Asset at the given asset path. |
ApplyAddedGameObject | Applies the added GameObject to the Prefab Asset at the given asset path. |
ApplyObjectOverride | Applies all overridden properties on a Prefab instance component or GameObject to the Prefab Asset at the given asset path. |
ApplyPrefabInstance | Applies all overrides on a Prefab instance to its Prefab Asset. |
ApplyPropertyOverride | Applies a single overridden property on a Prefab instance to the Prefab Asset at the given asset path. |
ApplyRemovedComponent | Removes the component from the Prefab Asset which has the component on it. |
GetAddedComponents | Returns a list of PrefabUtility.AddedComponent objects which contain information about added component overrides on the Prefab instance. |
GetAddedGameObjects | Returns a list of PrefabUtility.AddedGameObject objects which contain information about added GameObjects on the Prefab instance. |
GetCorrespondingObjectFromOriginalSource | Returns the object of origin for the given object. |
GetCorrespondingObjectFromSource | Returns the corresponding asset object of source, or null if it can't be found. |
GetCorrespondingObjectFromSourceAtPath | Returns the corresponding object of the given object from a given Prefab Asset path. |
GetIconForGameObject | Returns the icon for the given GameObject. |
GetNearestPrefabInstanceRoot | Return the GameObject that is the root of the nearest Prefab instance the object is part of. |
GetObjectOverrides | Returns a list of objects with information about object overrides on the Prefab instance. |
GetOutermostPrefabInstanceRoot | Returns the GameObject that is the root of the outermost Prefab instance the object is part of. |
GetPrefabAssetPathOfNearestInstanceRoot | Returns the asset path of the nearest Prefab instance root the specified object is part of. |
GetPrefabAssetType | Enum value indicating the type of Prefab Asset, such as Regular Prefab, Model Prefab and Prefab Variant. |
GetPrefabInstanceHandle | This function will give you the PrefabInstance object for the outermost Prefab instance the provided object is part of. |
GetPrefabInstanceStatus | Enum value with status about whether a Prefab instance is properly connected to its asset. |
GetPropertyModifications | Extract all modifications that are applied to the Prefab instance compared to the parent Prefab. |
GetRemovedComponents | Returns a list of objects with information about removed component overrides on the Prefab instance. |
InstantiateAttachedAsset | Instantiate an asset that is referenced by a Prefab and use it on the Prefab instance. |
InstantiatePrefab | Instantiates the given Prefab in a given Scene. |
IsAddedComponentOverride | Is this component added to a Prefab instance as an override? |
IsAddedGameObjectOverride | Is this GameObject added as a child to a Prefab instance as an override? |
IsAnyPrefabInstanceRoot | Is the GameObject the root of any Prefab instance? |
IsDefaultOverride | Returns true if the given modification is considered a default override. |
IsDisconnectedFromPrefabAsset | Returns true if the given object is part of an instance where the PrefabInstance object is missing but the given object has a valid corresponding object. |
IsOutermostPrefabInstanceRoot | Is the GameObject the root of a Prefab instance, excluding nested Prefabs? |
IsPartOfAnyPrefab | Returns true if the given object is part of any kind of Prefab. |
IsPartOfImmutablePrefab | Is this object part of a Prefab that cannot be edited? |
IsPartOfModelPrefab | Returns true if the given object is part of a Model Prefab Asset or Model Prefab instance. |
IsPartOfNonAssetPrefabInstance | Returns true if the given object is part of a Prefab instance and not part of an asset. |
IsPartOfPrefabAsset | Returns true if the given object is part of a Prefab Asset. |
IsPartOfPrefabInstance | Returns true if the given object is part of a Prefab instance. |
IsPartOfPrefabThatCanBeAppliedTo | Is this object part of a Prefab that cannot be applied to? |
IsPartOfRegularPrefab | Returns true if the given object is part of a regular Prefab instance or Prefab Asset. |
IsPartOfVariantPrefab | Returns true if the given object is part of a Prefab Variant Asset or Prefab Variant instance. |
IsPrefabAssetMissing | Returns true if the given object is part of a Prefab instance but the source asset is missing. |
LoadPrefabContents | Loads a Prefab Asset at a given path into an isolated Scene and returns the root GameObject of the Prefab. |
LoadPrefabContentsIntoPreviewScene | Loads a Prefab Asset at a given path into a given preview Scene and returns the root GameObject of the Prefab. |
RecordPrefabInstancePropertyModifications | Causes modifications made to the Prefab instance to be recorded. |
RevertAddedComponent | Removes this added component on a Prefab instance. |
RevertAddedGameObject | Removes this added GameObject from a Prefab instance. |
RevertObjectOverride | Reverts all overridden properties on a Prefab instance component or GameObject. |
RevertPrefabInstance | Reverts all overrides on a Prefab instance. |
RevertPropertyOverride | Revert a single property override on a Prefab instance. |
RevertRemovedComponent | Adds this removed component back on the Prefab instance. |
SaveAsPrefabAsset | Use this function to create a Prefab Asset at the given path from the given GameObject, including any childen in the Scene without modifying the input objects. |
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 | Use this function to save the version of an existing Prefab Asset that exists in memory back to disk. |
SetPropertyModifications | Assigns all modifications that are applied to the Prefab instance compared to the parent Prefab. |
UnloadPrefabContents | Releases the content from a Prefab previously loaded with LoadPrefabContents from memory. |
UnpackPrefabInstance | Unpacks a given Prefab instance so that it is replaced with the contents of the Prefab Asset while retaining all override values. |
UnpackPrefabInstanceAndReturnNewOutermostRoots | This function will unpack the given Prefab instance using the behaviour specified by unpackMode. |
PrefabInstanceUpdated | Delegate for method that is called after Prefab instances in the Scene have been updated. |