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.
#pragma strict
// Creates a prefab at the given path. // If a prefab already exists it asks if you want to replace it
@MenuItem("Examples/Create Empty Prefab") 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 Empty Prefab", true) static function ValidateCreatePrefab() { return Selection.activeGameObject != null; }
static function CreateNew(obj : GameObject, localPath : String) { var prefab : UnityEngine.Object = PrefabUtility.CreateEmptyPrefab(localPath); PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab); }
// 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: