エディタースクリプトで、ObjectFactory クラスを使用して新しいゲームオブジェクト、コンポーネント、アセットを作成します。これらのアイテムを作成するとき、ObjectFactory クラスは自動的にデフォルトのプリセットを使用します。デフォルトの プリセット は ObjectFactory によって自動的に処理されるため、スクリプトで検索して適用する必要はありません。
デフォルトでプリセットをサポートし使用可能にするには、以下のいずれかからクラスを継承する必要があります。
プリセットのインスペクターはクラスの一時的なインスタンスを作成します。そのため、ユーザーはその値を変更できます。ですから、クラスが静的な値、プロジェクトアセット、シーンインスタンスなどの他のオブジェクトに影響を与えたり、依存しないようにしてください。
ヒント: CustomEditor 属性の使用は任意です。
カスタムの EditorWindow クラスでプリセットを使用できるよう設定するには、以下を行なってください。
ScriptableObject を使用して設定のコピーを保存します。CustomEditor 属性を持つこともできます。プリセットシステムがこのオブジェクトを処理します。
UI にプリセット設定を表示するには、常にこの一時的な ScriptableObject の Inspector を使用します。これにより、ユーザーは EditorWindow と同じ UI を表示でき、保存したプリセットを編集することができます。
プリセットボタンを公開し、独自の PresetSelectorReceiver を実装して、プリセットが Select Preset ウィンドウで選択されているときに EditorWindow 設定を最新の状態に保てるようにします。
以下のスクリプトサンプルは、簡単な EditorWindow にプリセット設定を追加する方法を示しています。
このスクリプトサンプルは、カスタムウィンドウの設定を保持して表示する ScriptableObject を示しています (Editor/MyWindowSettings.cs という名前のファイルに保存されます)。
using UnityEngine;
// Temporary ScriptableObject used by the Preset system
public class MyWindowSettings : ScriptableObject
{
[SerializeField]
string m_SomeSettings;
public void Init(MyEditorWindow window)
{
m_SomeSettings = window.someSettings;
}
public void ApplySettings(MyEditorWindow window)
{
window.someSettings = m_SomeSettings;
window.Repaint();
}
}
カスタムウィンドウで使用する ScriptableObject を更新する PresetSelectorReceiver のスクリプトサンプル (Editor/MySettingsReceiver.cs という名前のファイルに保存されます)。
using UnityEditor.Presets;
// PresetSelector receiver to update the EditorWindow with the selected values.
public class MySettingsReceiver : PresetSelectorReceiver
{
Preset initialValues;
MyWindowSettings currentSettings;
MyEditorWindow currentWindow;
public void Init(MyWindowSettings settings, MyEditorWindow window)
{
currentWindow = window;
currentSettings = settings;
initialValues = new Preset(currentSettings);
}
public override void OnSelectionChanged(Preset selection)
{
if (selection != null)
{
// Apply the selection to the temporary settings
selection.ApplyTo(currentSettings);
}
else
{
// None have been selected. Apply the Initial values back to the temporary selection.
initialValues.ApplyTo(currentSettings);
}
// Apply the new temporary settings to our manager instance
currentSettings.ApplySettings(currentWindow);
}
public override void OnSelectionClosed(Preset selection)
{
// Call selection change one last time to make sure you have the last selection values.
OnSelectionChanged(selection);
// Destroy the receiver here, so you don't need to keep a reference to it.
DestroyImmediate(this);
}
}
EditorWindow のスクリプトサンプル。一時的な ScriptableObject の Inspector とプリセットボタンを使用してカスタム設定を表示します (Editor/MyEditorWindow.cs という名前のファイルに保存されます)。
using UnityEngine;
using UnityEditor;
using UnityEditor.Presets;
public class MyEditorWindow : EditorWindow
{
// get the Preset icon and a style to display it
private static class Styles
{
public static GUIContent presetIcon = EditorGUIUtility.IconContent("Preset.Context");
public static GUIStyle iconButton = new GUIStyle("IconButton");
}
Editor m_SettingsEditor;
MyWindowSettings m_SerializedSettings;
public string someSettings
{
get { return EditorPrefs.GetString("MyEditorWindow_SomeSettings"); }
set { EditorPrefs.SetString("MyEditorWindow_SomeSettings", value); }
}
// Method to open the window
[MenuItem("Window/MyEditorWindow")]
static void OpenWindow()
{
GetWindow<MyEditorWindow>();
}
void OnEnable()
{
// Create your settings now and its associated Inspector
// that allows to create only one custom Inspector for the settings in the window and the Preset.
m_SerializedSettings = ScriptableObject.CreateInstance<MyWindowSettings>();
m_SerializedSettings.Init(this);
m_SerializedSettings.hideFlags = HideFlags.DontSave;
m_SettingsEditor = Editor.CreateEditor(m_SerializedSettings);
m_SettingsEditor.hideFlags = HideFlags.DontSave;
}
void OnDisable()
{
Object.DestroyImmediate(m_SerializedSettings);
Object.DestroyImmediate(m_SettingsEditor);
}
void OnGUI()
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("My custom settings", EditorStyles.boldLabel);
GUILayout.FlexibleSpace();
// create the Preset button at the end of the "MyManager Settings" line.
var buttonPosition = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight, Styles.iconButton);
if (EditorGUI.DropdownButton(buttonPosition, Styles.presetIcon, FocusType.Passive, Styles.iconButton))
{
// Create a receiver instance. This destroys itself when the window appears, so you don't need to keep a reference to it.
var presetReceiver = ScriptableObject.CreateInstance<MySettingsReceiver>();
presetReceiver.Init(m_SerializedSettings, this);
// Show the PresetSelector modal window. The presetReceiver updates your data.
PresetSelector.ShowSelector(m_SerializedSettings, null, true, presetReceiver);
}
EditorGUILayout.EndHorizontal();
// Draw the settings default Inspector and catch any change made to it.
EditorGUI.BeginChangeCheck();
m_SettingsEditor.OnInspectorGUI();
if (EditorGUI.EndChangeCheck())
{
// Apply changes made in the settings editor to our instance.
m_SerializedSettings.ApplySettings(this);
}
}
}
2017–03–27 2018.1 NewIn20181 の新機能