class in UnityEditor
/
Implemented in:UnityEditor
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseUtility 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. |
ConnectGameObjectToPrefab | Connects the source prefab to the game object. |
CreateEmptyPrefab | Creates an empty prefab at given path. |
CreatePrefab | Creates a prefab from a game object hierarchy. |
DisconnectPrefabInstance | Disconnects the prefab instance from its parent prefab. |
FindPrefabRoot | Helper function to find the prefab root of an object (used for picking niceness). |
FindRootGameObjectWithSameParentPrefab | Returns the topmost game object that has the same prefab parent as target. |
FindValidUploadPrefabInstanceRoot | Returns root game object of the prefab instance if that root prefab instance is a parent of the prefab. |
GetCorrespondingObjectFromSource | Returns the corresponding asset object of source, or null if it can't be found. |
GetPrefabObject | Retrieves the enclosing prefab for any object contained within. |
GetPrefabType | Given an object, returns its prefab type (None, if it's not a prefab). |
GetPropertyModifications | Extract all modifications that are applied to the prefab instance compared to the parent prefab. |
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. |
MergeAllPrefabInstances | Force re-merging all prefab instances of this prefab. |
ReconnectToLastPrefab | Connects the game object to the prefab that it was last connected to. |
RecordPrefabInstancePropertyModifications | Causes modifications made to the Prefab instance to be recorded. |
ReplacePrefab | Replaces the targetPrefab with a copy of the game object hierarchy go. |
ResetToPrefabState | Resets the properties of the component or game object to the parent prefab state. |
RevertPrefabInstance | Resets the properties of all objects in the prefab, including child game objects and components that were added to the prefab instance. |
SetPropertyModifications | Assigns all modifications that are applied to the prefab instance compared to the parent prefab. |
PrefabInstanceUpdated | Delegate for method that is called after prefab instances in the scene have been updated. |
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:
Thanks for helping to make the Unity documentation better!