Version: 2018.3 (switch to 2019.1a or 2017.4)
LanguageEnglish
  • C#
Method group is Obsolete

PrefabUtility.CreateEmptyPrefab

Suggest a change

Success!

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.

Close

Submission failed

For 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.

Close

Cancel

Obsolete public static Object CreateEmptyPrefab(string path);

Parameters

pathThe asset path to use for the new empty Prefab.

Returns

Object A reference to the new Prefab Asset.

Description

Creates an empty Prefab at given path.

If a Prefab at the path already exists it will be deleted and replaced with an empty Prefab. Returns a reference to the Prefab.

// Creates a Prefab at the given path.
// If a Prefab already exists it asks if you want to replace it

using UnityEngine; using UnityEditor;

public class CreateEmptyExample : EditorWindow { [MenuItem("Examples/Create Empty Prefab")] static void CreatePrefab() { GameObject[] objs = Selection.gameObjects;

foreach (GameObject go in objs) { string localPath = "Assets/" + go.name + ".prefab"; if (AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject))) { if (EditorUtility.DisplayDialog("Are you sure?", "The Prefab already exists. Do you want to overwrite it?", "Yes", "No")) { CreateNew(go, localPath); } } else { Debug.Log(go.name + " is not a Prefab, will convert"); CreateNew(go, localPath); } } }

// Disable the menu item if no selection is in place [MenuItem("Examples/Create Empty Prefab", true)] static bool ValidateCreatePrefab() { return Selection.activeGameObject != null; }

static void CreateNew(GameObject obj, string localPath) { Object prefab = PrefabUtility.CreateEmptyPrefab(localPath); PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab); } }

Did you find this page useful? Please give it a rating: