first commit
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using UnityEditor.SettingsManagement;
|
||||
|
||||
namespace UnityEditor.SettingsManagement.Examples
|
||||
{
|
||||
/// <summary>
|
||||
/// This class will act as a manager for the <see cref="Settings"/> singleton.
|
||||
/// </summary>
|
||||
static class MySettingsManager
|
||||
{
|
||||
// Replace this with your own package name. Project settings will be stored in a JSON file in a directory matching
|
||||
// this name.
|
||||
internal const string k_PackageName = "com.unity.settings-manager-examples";
|
||||
|
||||
static Settings s_Instance;
|
||||
|
||||
internal static Settings instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (s_Instance == null)
|
||||
s_Instance = new Settings(k_PackageName);
|
||||
|
||||
return s_Instance;
|
||||
}
|
||||
}
|
||||
|
||||
// The rest of this file is just forwarding the various setting methods to the instance.
|
||||
|
||||
public static void Save()
|
||||
{
|
||||
instance.Save();
|
||||
}
|
||||
|
||||
public static T Get<T>(string key, SettingsScope scope = SettingsScope.Project, T fallback = default(T))
|
||||
{
|
||||
return instance.Get<T>(key, scope, fallback);
|
||||
}
|
||||
|
||||
public static void Set<T>(string key, T value, SettingsScope scope = SettingsScope.Project)
|
||||
{
|
||||
instance.Set<T>(key, value, scope);
|
||||
}
|
||||
|
||||
public static bool ContainsKey<T>(string key, SettingsScope scope = SettingsScope.Project)
|
||||
{
|
||||
return instance.ContainsKey<T>(key, scope);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityEditor.SettingsManagement.Examples
|
||||
{
|
||||
/// <summary>
|
||||
/// To create an entry in the Preferences window, define a new SettingsProvider inheriting <see cref="UserSettingsProvider"/>.
|
||||
/// You can also choose to implement your own SettingsProvider and ignore this implementation. The benefit of using
|
||||
/// <see cref="UserSettingsProvider"/> is that all <see cref="UserSetting{T}"/> fields in the assembly are automatically
|
||||
/// populated within the preferences, with support for search and resetting default values.
|
||||
/// </summary>
|
||||
static class MySettingsProvider
|
||||
{
|
||||
const string k_PreferencesPath = "Preferences/Package With Project and User Settings";
|
||||
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
[SettingsProvider]
|
||||
static SettingsProvider CreateSettingsProvider()
|
||||
{
|
||||
var provider = new UserSettingsProvider(k_PreferencesPath,
|
||||
MySettingsManager.instance,
|
||||
new [] { typeof(MySettingsProvider).Assembly });
|
||||
|
||||
return provider;
|
||||
}
|
||||
#else
|
||||
|
||||
// For backwards compatibility it is possible to create an instance of UserSettingsProvider and invoke OnGUI manually.
|
||||
[NonSerialized]
|
||||
static UserSettingsProvider s_SettingsProvider;
|
||||
|
||||
[PreferenceItem("ProBuilder")]
|
||||
static void ProBuilderPreferencesGUI()
|
||||
{
|
||||
if (s_SettingsProvider == null)
|
||||
s_SettingsProvider = new UserSettingsProvider(MySettingsManager.instance, new[] { typeof(MySettingsProvider).Assembly });
|
||||
|
||||
s_SettingsProvider.OnGUI(null);
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
using UnityEditor.SettingsManagement;
|
||||
|
||||
namespace UnityEditor.SettingsManagement.Examples
|
||||
{
|
||||
// Usually you will only have a single Settings instance, so it is convenient to define a UserSetting<T> implementation
|
||||
// that points to your instance. In this way you avoid having to pass the Settings parameter in setting field definitions.
|
||||
class MySetting<T> : UserSetting<T>
|
||||
{
|
||||
public MySetting(string key, T value, SettingsScope scope = SettingsScope.Project)
|
||||
: base(MySettingsManager.instance, key, value, scope)
|
||||
{}
|
||||
|
||||
MySetting(Settings settings, string key, T value, SettingsScope scope = SettingsScope.Project)
|
||||
: base(settings, key, value, scope) { }
|
||||
}
|
||||
}
|
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityEditor.SettingsManagement.Examples
|
||||
{
|
||||
[Serializable]
|
||||
class FooClass
|
||||
{
|
||||
public int intValue;
|
||||
public string stringValue;
|
||||
|
||||
public FooClass()
|
||||
{
|
||||
intValue = 42;
|
||||
stringValue = "I'm some text";
|
||||
}
|
||||
}
|
||||
|
||||
class MySettingsExamples : EditorWindow
|
||||
{
|
||||
#pragma warning disable 414
|
||||
// [UserSetting] attribute registers this setting with the UserSettingsProvider so that it can be automatically
|
||||
// shown in the UI.
|
||||
[UserSetting("General Settings", "Days Without Incident")]
|
||||
static MySetting<int> s_NumberOfDaysWithoutIncident = new MySetting<int>("general.daysWithoutIncident", 0, SettingsScope.User);
|
||||
|
||||
[UserSetting("General Settings", "Favorite Color")]
|
||||
static MySetting<Color> s_FavoriteColor = new MySetting<Color>("general.favoriteColor", Color.magenta);
|
||||
|
||||
[UserSetting("General Settings", "Vector2 Field")]
|
||||
static MySetting<Vector2> s_Vector2Value = new MySetting<Vector2>("general.vector2Value", new Vector2(2f, 4f));
|
||||
|
||||
[UserSetting("General Settings", "Editor Flags")]
|
||||
static MySetting<StaticEditorFlags> s_EditorFlags = new MySetting<StaticEditorFlags>("general.editorFlags", StaticEditorFlags.BatchingStatic);
|
||||
#pragma warning restore 414
|
||||
|
||||
// [UserSetting] with no arguments simply registers the key with UserSettingsProvider so that it can be included
|
||||
// in debug views and reset with the options gizmo. Usually this is used in conjunction with [UserSettingsBlock].
|
||||
[UserSetting]
|
||||
static MySetting<FooClass> s_Foo = new MySetting<FooClass>("general.foo", new FooClass(), SettingsScope.Project);
|
||||
|
||||
[UserSetting]
|
||||
static MySetting<int> s_NumberWithSlider = new MySetting<int>("general.conditionalValue", 5, SettingsScope.Project);
|
||||
|
||||
// A UserSettingBlock is a callback invoked from the UserSettingsProvider. It allows you to draw more complicated
|
||||
// UI elements without the need to create a new SettingsProvider. Parameters are "category" and "search keywords."
|
||||
// For maximum compatibility, use `SettingsGUILayout` searchable and settings fields to get features like search
|
||||
// and per-setting reset with a context click.
|
||||
[UserSettingBlock("Custom GUI Settings")]
|
||||
static void ConditionalValueGUI(string searchContext)
|
||||
{
|
||||
EditorGUI.BeginChangeCheck();
|
||||
|
||||
s_NumberWithSlider.value = SettingsGUILayout.SettingsSlider("Number With Slider", s_NumberWithSlider, 0, 10, searchContext);
|
||||
|
||||
var foo = s_Foo.value;
|
||||
|
||||
using(new SettingsGUILayout.IndentedGroup("Foo Class"))
|
||||
{
|
||||
EditorGUI.BeginChangeCheck();
|
||||
|
||||
foo.intValue = SettingsGUILayout.SearchableIntField("Int Value", foo.intValue, searchContext);
|
||||
foo.stringValue = SettingsGUILayout.SearchableTextField("String Value", foo.stringValue, searchContext);
|
||||
|
||||
// Because FooClass is a reference type, we need to apply the changes to the backing repository (SetValue
|
||||
// would also work here).
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
s_Foo.ApplyModifiedProperties();
|
||||
}
|
||||
|
||||
SettingsGUILayout.DoResetContextMenuForLastRect(s_Foo);
|
||||
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
MySettingsManager.Save();
|
||||
}
|
||||
|
||||
const string k_ColorInstanceFieldKey = "MySettingsExamples.m_ColorField";
|
||||
|
||||
// It is also possible to forego the UserSetting<T> wrapper and use a settings instance directly. To register
|
||||
// a setting with the "Reset All" option of UserSettingsProvider, apply the [UserSetting] (or for instance fields [SettingsKey]) attribute.
|
||||
Color m_ColorField;
|
||||
|
||||
[MenuItem("Window/Show Settings Examples")]
|
||||
static void Init()
|
||||
{
|
||||
GetWindow<MySettingsExamples>();
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
m_ColorField = MySettingsManager.Get<Color>(k_ColorInstanceFieldKey);
|
||||
}
|
||||
|
||||
void OnGUI()
|
||||
{
|
||||
EditorGUI.BeginChangeCheck();
|
||||
|
||||
m_ColorField = EditorGUILayout.ColorField("Color", m_ColorField);
|
||||
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
MySettingsManager.Set<Color>(k_ColorInstanceFieldKey, m_ColorField);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user