ドキュメント

Unity Engine


User Manual

Script Reference

Unity Engine


Add preset support to custom types

Add preset support for your own custom C# types.
読み終わるまでの所要時間 4 分最終更新 6日前

Use the
ObjectFactory
class in Editor scripts to create new GameObjects, components, and assets that support and inherit presets by default. The
ObjectFactory
class automatically applies default presets to these items.
To support and enable presets by default for your custom classes, they must inherit from one of the following:
The preset Inspector window creates a temporary instance of your class, so that users can modify its values. Therefore, make sure your class doesn't affect or rely on other objects such as static values, project assets, or scene instances.

Preset settings in a custom Editor window

This example demonstrates how to add preset settings to a custom
EditorWindow
class.
EditorWindow
doesn't inherit from any of the previously listed types, so it can't support presets directly. The example works around this as follows:
  • Uses a
    ScriptableObject
    as a temporary stand-in, because the preset system can't apply presets to the
    EditorWindow
    itself. You can optionally add a
    CustomEditor
    attribute to it.
  • Reuses this temporary
    ScriptableObject
    's Inspector to display the preset settings inside the window.
  • Exposes a Preset button that opens the Select Preset window, and uses the
    PresetSelector
    class's
    ShowSelector
    callbacks to keep the
    EditorWindow
    settings up-to-date when the user selects a preset.
To use these examples, you need to create both a
PresetWindowSettings.cs
file and a
PresetEditorWindow.cs
file and place them in an
Editor
folder
.
The
PresetWindowSettings
example demonstrates a
ScriptableObject
that stores and displays settings in a custom window:
using UnityEngine; // Store the window's settings in a ScriptableObject, so that Unity can // apply a preset to them. public class PresetWindowSettings : ScriptableObject { // Unity captures only serialized properties in a Preset. [SerializeField] string m_PresetSettings; // Copy the window's current values into this object. public void Init(PresetEditorWindow window) { m_PresetSettings = window.presetSettings; } // Copy this object's values back into the window, and redraw it. public void ApplySettings(PresetEditorWindow window) { window.presetSettings = m_PresetSettings; window.Repaint(); } }
The
PresetEditorWindow
example creates an
EditorWindow
that displays custom settings using the temporary
PresetWindowSettings
ScriptableObject and its Preset button:
using UnityEngine; using UnityEditor; using UnityEditor.Presets; public class PresetEditorWindow : EditorWindow { // Store the preset icon and the button style that displays it. private class Styles { public GUIContent presetIcon = EditorGUIUtility.IconContent("Preset.Context"); public GUIStyle iconButton = new GUIStyle("IconButton"); } Styles m_Styles; Editor m_SettingsEditor; PresetWindowSettings m_SerializedSettings; public string presetSettings { get { return EditorPrefs.GetString("PresetEditorWindow_PresetSettings"); } set { EditorPrefs.SetString("PresetEditorWindow_PresetSettings", value); } } // Add a menu item that opens the window. [MenuItem("Window/PresetEditorWindow")] static void OpenWindow() { GetWindow<PresetEditorWindow>(); } void OnEnable() { // Create a temporary object to hold the window's settings. // HideFlags.DontSave stops Unity from saving it to a scene or an asset. m_SerializedSettings = ScriptableObject.CreateInstance<PresetWindowSettings>(); m_SerializedSettings.Init(this); m_SerializedSettings.hideFlags = HideFlags.DontSave; // Create one Inspector for the settings object, so that the window // and the preset selector display the same custom Inspector. m_SettingsEditor = Editor.CreateEditor(m_SerializedSettings); m_SettingsEditor.hideFlags = HideFlags.DontSave; } void OnDisable() { // Destroy the temporary objects, so that they don't leak // when you close the window. DestroyImmediate(m_SerializedSettings); DestroyImmediate(m_SettingsEditor); } void OnGUI() { // Create the styles the first time the window draws. // The Editor skin isn't available any earlier. m_Styles ??= new Styles(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Custom settings", EditorStyles.boldLabel); GUILayout.FlexibleSpace(); // Reserve space for the preset button at the end of the row. Rect buttonPosition = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight, m_Styles.iconButton); if (EditorGUI.DropdownButton(buttonPosition, m_Styles.presetIcon, FocusType.Passive, m_Styles.iconButton)) { // Open the Select Preset window. Unity applies the selected preset // to the settings object, and then calls the callback methods. PresetSelector.ShowSelector(new Object[] { m_SerializedSettings }, null, true, OnPresetSelectionChanged, OnPresetSelectionClosed); } EditorGUILayout.EndHorizontal(); // Draw the Inspector for the settings object, and detect // whether the user changes any value in it. EditorGUI.BeginChangeCheck(); m_SettingsEditor.OnInspectorGUI(); if (EditorGUI.EndChangeCheck()) { // Copy the changed values into the window. m_SerializedSettings.ApplySettings(this); } } void OnPresetSelectionChanged(Preset selection) { // Unity applies or reverts the preset before it calls this method. // Copy the resulting values into the window. m_SerializedSettings.ApplySettings(this); } void OnPresetSelectionClosed(Preset selection, bool canceled) { // Copy the values one last time, because the user might cancel the selection. OnPresetSelectionChanged(selection); } }

Additional resources