first commit
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"displayName":"User Settings Example",
|
||||
"description": "Example code showing use of the Settings Manager attributes.",
|
||||
"createSeparatePackage": false
|
||||
}
|
@@ -0,0 +1,94 @@
|
||||
using UnityEditor.EditorTools;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityEditor.SettingsManagement.Examples
|
||||
{
|
||||
/// <summary>
|
||||
/// This example shows how to access multiple project setting repositories without making use of
|
||||
/// <see cref="UserSetting{T}"/>.
|
||||
/// </summary>
|
||||
[EditorTool("Editor Tool Settings Example")]
|
||||
class PerPlatformSettingsTool : EditorTool
|
||||
{
|
||||
#if !UNITY_2019_2_OR_NEWER
|
||||
public override GUIContent toolbarIcon
|
||||
{
|
||||
get { return new GUIContent("Settings Example Tool", "Settings Manager Example Tool"); }
|
||||
}
|
||||
#endif
|
||||
|
||||
// This example creates two project settings repositories, A and B.
|
||||
static readonly string[] k_ProjectRepositories = new[]
|
||||
{
|
||||
"Settings A",
|
||||
"Settings B"
|
||||
};
|
||||
|
||||
// The settings manager.
|
||||
static Settings s_Settings;
|
||||
|
||||
// This is the key that is used to store the color setting.
|
||||
const string k_ToolColorSetting = "ToolColor";
|
||||
|
||||
// Current tool color
|
||||
Color m_ToolColor;
|
||||
|
||||
// The repository that color is read from and written to.
|
||||
int m_Repository;
|
||||
|
||||
Vector3 m_HandlePosition;
|
||||
|
||||
// Get the color value from a repository, setting a default value if the key does not already exist. This is
|
||||
// handled for you if using UserSetting{T}.
|
||||
Color GetToolColor(string repository, Color defaultColor)
|
||||
{
|
||||
if (!s_Settings.ContainsKey<Color>(k_ToolColorSetting, repository))
|
||||
s_Settings.Set<Color>(k_ToolColorSetting, defaultColor, repository);
|
||||
|
||||
return s_Settings.Get<Color>(k_ToolColorSetting, k_ProjectRepositories[m_Repository]);
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
s_Settings = new Settings(new ISettingsRepository[]
|
||||
{
|
||||
new UserSettingsRepository(),
|
||||
new PackageSettingsRepository("com.unity.settings-manager-examples", k_ProjectRepositories[0]),
|
||||
new PackageSettingsRepository("com.unity.settings-manager-examples", k_ProjectRepositories[1])
|
||||
});
|
||||
|
||||
m_Repository = s_Settings.Get<int>("ToolColorRepositoryName", SettingsScope.User);
|
||||
|
||||
m_ToolColor = GetToolColor(k_ProjectRepositories[m_Repository], Color.blue);
|
||||
}
|
||||
|
||||
public override void OnToolGUI(EditorWindow window)
|
||||
{
|
||||
Handles.BeginGUI();
|
||||
|
||||
GUILayout.BeginVertical(GUILayout.MaxWidth(300));
|
||||
|
||||
EditorGUI.BeginChangeCheck();
|
||||
m_Repository = EditorGUILayout.IntPopup(m_Repository, k_ProjectRepositories, new int[] { 0, 1 });
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
m_ToolColor = GetToolColor(k_ProjectRepositories[m_Repository], Color.blue);
|
||||
|
||||
EditorGUI.BeginChangeCheck();
|
||||
m_ToolColor = EditorGUILayout.ColorField(m_ToolColor);
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
s_Settings.Set<Color>(k_ToolColorSetting, m_ToolColor, k_ProjectRepositories[m_Repository]);
|
||||
s_Settings.Save();
|
||||
}
|
||||
|
||||
GUILayout.EndVertical();
|
||||
|
||||
Handles.EndGUI();
|
||||
|
||||
using (new Handles.DrawingScope(m_ToolColor))
|
||||
{
|
||||
m_HandlePosition = Handles.Slider(m_HandlePosition, Vector3.right);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "Unity.Settings.Example",
|
||||
"references": [
|
||||
"Unity.Settings.Editor"
|
||||
],
|
||||
"optionalUnityReferences": [],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": false,
|
||||
"defineConstraints": []
|
||||
}
|
Reference in New Issue
Block a user