에디터 스크립트에서 ObjectFactory 클래스를 사용하여 새 게임 오브젝트, 컴포넌트와 에셋을 생성하십시오. 이러한 항목을 생성할 때 ObjectFactory 클래스는 자동으로 기본 프리셋을 사용합니다. ObjectFactory가 이 작업을 처리하므로 스크립트는 기본 프리셋을 검색하여 적용할 필요가 없습니다.
기본적으로 프리셋을 지원하고 활성화하려면 클래스가 다음 중 하나에서 상속받아야 합니다.
프리셋 인스펙터는 사용자가 값을 수정할 수 있도록 클래스의 임시 인스턴스를 만듭니다. 따라서 클래스가 정적 값, 프로젝트 에셋 또는 씬 인스턴스 등 다른 오브젝트에 영향을 주거나 기반하지 않는지 확인하십시오.
팁: CustomEditor 속성 사용은 선택 사항입니다.
프리셋을 사용할 수 있는 설정으로 커스텀 EditorWindow 클래스를 설정하는 경우 다음을 수행하십시오.
ScriptableObject를 사용하여 설정의 복사본을 저장합니다. CustomEditor 속성도 가질 수 있습니다. 프리셋 시스템이 이 오브젝트를 처리합니다.
항상 임시 ScriptableObject 인스펙터를 사용하여 프리셋 설정을 UI에 표시합니다. 이렇게 하면 EditorWindow에서 그리고 저장된 프리셋을 편집할 때 사용자에게 동일한 UI가 표시됩니다.
Preset 버튼을 노출하고 자체 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);
}
}
임시 ScriptableObject 인스펙터와 해당 프리셋 버튼을 사용하여 커스텀 설정을 보여주는 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_SettingsEditor = Editor.CreateEditor(m_SerializedSettings);
}
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