Version: 2018.1

PrefabUtility

class in UnityEditor

マニュアルに切り替える

説明

プレハブのためのユーティリティクラスの関連操作。

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

Static 変数

prefabInstanceUpdatedシーンのプレハブのインスタンスが更新された後に呼び出します

Static 関数

ConnectGameObjectToPrefabソースプレハブにゲームオブジェクトを接続します
CreateEmptyPrefab指定されたパスに空のプレハブを作成します
CreatePrefabゲームオブジェクトのヒエラルキーからプレハブを作成します
DisconnectPrefabInstanceプレハブのインスタンスを親プレハブから切断します
FindPrefabRootヘルパー関数は(ピッキングに使用した)オブジェクトのプレハブルートを検索します
FindRootGameObjectWithSameParentPrefab target と同じプレハブ親を持つ最上位のゲームオブジェクトを返します
FindValidUploadPrefabInstanceRootそのルートプレハブインスタンスがプレハブの親である場合は、プレハブインスタンスのルートゲームオブジェクトを返します
GetPrefabObject内部に含む任意のオブジェクトを包括するプレハブを取得します
GetPrefabParentソースの親アセットオブジェクトを返し、見つからない場合は null を返します
GetPrefabTypeオブジェクトが与えらると、そのプレハブの type を返します(プレハブでない場合は何も返しません)
GetPropertyModifications親プレハブと比較して、指定したプレハブのインスタンスに適用されたすべての修正を抽出します
InstantiateAttachedAssetプレハブによって参照されているアセットをインスタンス化し、プレハブのインスタンスで使用します
InstantiatePrefab指定したシーン内に指定したプレハブをインスタンス化します
MergeAllPrefabInstancesこのプレハブのすべてのプレハブのインスタンスを強制的に再マージします
ReconnectToLastPrefab最後に接続されたプレハブにゲームオブジェクトを接続します
RecordPrefabInstancePropertyModificationsCauses modifications made to the Prefab instance to be recorded.
ReplacePrefabゲームオブジェクトのヒエラルキーにある go のコピーと targetPrefab を置き換えます
ResetToPrefabStateコンポーネントやゲームオブジェクトのプロパティーを親プレハブの状態にリセットします
RevertPrefabInstanceプレハブのインスタンスに追加された子ゲームオブジェクトとコンポーネントを含プレハブ内のすべてのオブジェクトのプロパティーをリセットします
SetPropertyModifications親プレハブと比較し、プレハブのインスタンスに適用されるすべての修正を適用します

デリゲート

PrefabInstanceUpdatedシーンが更新された際、プレハブのインスタンス後に呼び出されるメソッドをデリゲート