在您的编辑器脚本一段代码,允许您创建自己的组件,触发游戏事件,随时间修改组件属性并以您喜欢的任何方式响应用户输入。更多信息
查看 词汇表中,使用ObjectFactory 类来创建新的游戏对象Unity 场景中的基本对象,可以代表角色、道具、场景、摄像机、路标等等。游戏对象的的功能由附加到它的组件定义。更多信息
查看 词汇表、组件和资源。当创建这些项目时,ObjectFactory
类会自动使用默认预设。您的脚本不必搜索和应用默认预设,因为 ObjectFactory
会为您处理。
为了默认支持和启用预设,您的类必须继承自以下之一
预设检查器一个 Unity 窗口,显示当前选定游戏对象、资源或项目设置的信息,允许您检查和编辑这些值。更多信息
查看 词汇表会创建您类的一个临时实例,以便用户可以修改它的值,因此请确保您的类不会影响或依赖于其他对象,例如静态值、项目资源或场景一个场景包含游戏环境和菜单。可以将每个独特的场景文件视为一个独特的关卡。在每个场景中,您放置环境、障碍物和装饰物,本质上是设计和构建游戏的各个部分。更多信息
查看 词汇表实例。
提示:使用 CustomEditor 属性是可选的。
当设置一个带有可以使用预设的设置的自定义 EditorWindow 类时
使用 ScriptableObject 来存储您的设置副本。它也可以有一个 CustomEditor 属性。预设系统会处理这个对象。
始终使用这个临时的 ScriptableObject
检查器来显示您UI(用户界面) 允许用户与您的应用程序交互。Unity 目前支持三种 UI 系统。更多信息
查看 词汇表中的预设设置。这使您的用户能够在 EditorWindow
中以及编辑保存的预设时具有相同的 UI。
公开一个预设按钮,并使用您自己的 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();
}
}
PresetSelectorReceiver
的脚本示例,它更新自定义窗口中使用的 ScriptableObject
(保存在名为 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 检查器及其预设按钮来显示自定义设置(保存在名为 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