Version: 5.5 (switch to 5.6b)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

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

public static function CreateEmptyPrefab(path: string): Object;
public static Object CreateEmptyPrefab(string path);

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 from the selected GameObjects.
// If the prefab already exists it asks if you want to replace it.

#pragma strict

@MenuItem("Examples/Create Prefab From Selected") static function CreatePrefab() {

var objs = Selection.gameObjects;

for (var go : GameObject in objs) { var localPath : String = "Assets/" + go.name + ".prefab"; if (AssetDatabase.LoadAssetAtPath(localPath, GameObject)) { if (EditorUtility.DisplayDialog("Are you sure?", "The prefab already exists. Do you want to overwrite it?", "Yes", "No")) CreateNew(go, localPath); } else CreateNew(go, localPath); } } @MenuItem("Examples/Create Prefab From Selected", true) static function ValidateCreatePrefab() { return Selection.activeGameObject != null; }

static function CreateNew(obj : GameObject, localPath : String) { var prefab : Object = PrefabUtility.CreateEmptyPrefab(localPath); PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab); }
// Creates a prefab from the selected GameObjects.
// If the prefab already exists it asks if you want to replace it.
using UnityEngine;
using UnityEditor;

public class CreatePrefabExample : EditorWindow {

[MenuItem ("Examples/October/CreatePrefabExample")] static void CreatePrefab() { GameObject[] objs = Selection.gameObjects; if (objs.Length == 0) { Debug.Log("No GameObjects selected."); return; } foreach (GameObject go in objs) { Debug.Log("foreach: " + go.name); string localPath = "Assets/October/" + go.name + ".prefab"; if (AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject))) { Debug.Log("no idea, but something"); if (EditorUtility.DisplayDialog("Are you sure?", "The prefab already exists. Do you want to overwrite it?", "Yes", "No")) { Debug.Log("DisplayDialog took yes"); CreateNew(go, localPath); } } else { Debug.Log(go.name + " is not a prefab, will convert"); CreateNew(go, localPath); } } } static void CreateNew(GameObject obj, string localPath) { Debug.Log("CreateNew: " + obj.name + " : " + localPath); Object prefab = PrefabUtility.CreateEmptyPrefab(localPath); PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab); }

}