Version: 5.5
public static Object CreateEmptyPrefab (string path);

説明

指定されたパスに空のプレハブを作成します

パスにプレハブがすでに存在する場合、すでにあるものは削除されて空のプレハブに置き換えられます。 プレハブへリファレンスを返します。

// 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); } }