在编辑器脚本中,使用 ObjectFactory 类可创建新的游戏对象、组件和资源。创建这些项时,ObjectFactory 类自动使用默认预设。脚本不必搜索并应用默认预设,因为 ObjectFactory 会处理此问题。
要默认支持和启用预设,您的类必须继承自以下之一:
Preset Inspector 会创建类的临时实例,便于用户修改其值,因此请确保您的类不会影响或依赖其他对象,如静态值、项目资源或场景实例。
提示:可选择是否使用 CustomEditor 属性。
使用可能使用预设的设置来设置自定义 EditorWindow 类时:
使用 ScriptableObject 来存储设置的副本。它也可以具有 CustomEditor 属性。预设系统会处理此对象。
始终使用此临时 ScriptableObject 检视面板 (Inspector) 在__ UI__(即用户界面,User Interface)让用户能够与您的应用程序进行交互。Unity 目前支持三种 UI 系统。更多信息
See in Glossary 中显示预设设置。这样,您的用户在 EditorWindow 中和编辑保存的预设时会看到相同的 UI。
在__选择预设 (Select Preset)__ 窗口中选择预设时,显示预设 (Preset) 按钮并使用您自己的 PresetSelectorReceiver 实现使 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);
}
}
使用临时 ScriptableObject Inspector 及其预设按钮显示自定义设置的 EditorWindow 的脚本示例(保存到名为 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