Maze / Library /PackageCache /com.unity.settings-manager@1.0.3 /Samples~ /PackageWithProjectAndUserSettings /SettingsExamples.cs
| using System; | |
| using UnityEngine; | |
| namespace UnityEditor.SettingsManagement.Examples | |
| { | |
| [] | |
| class FooClass | |
| { | |
| public int intValue; | |
| public string stringValue; | |
| public FooClass() | |
| { | |
| intValue = 42; | |
| stringValue = "I'm some text"; | |
| } | |
| } | |
| class MySettingsExamples : EditorWindow | |
| { | |
| // [UserSetting] attribute registers this setting with the UserSettingsProvider so that it can be automatically | |
| // shown in the UI. | |
| [] | |
| static MySetting<int> s_NumberOfDaysWithoutIncident = new MySetting<int>("general.daysWithoutIncident", 0, SettingsScope.User); | |
| [] | |
| static MySetting<Color> s_FavoriteColor = new MySetting<Color>("general.favoriteColor", Color.magenta); | |
| [] | |
| static MySetting<Vector2> s_Vector2Value = new MySetting<Vector2>("general.vector2Value", new Vector2(2f, 4f)); | |
| [] | |
| static MySetting<StaticEditorFlags> s_EditorFlags = new MySetting<StaticEditorFlags>("general.editorFlags", StaticEditorFlags.BatchingStatic); | |
| // [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]. | |
| [] | |
| static MySetting<FooClass> s_Foo = new MySetting<FooClass>("general.foo", new FooClass(), SettingsScope.Project); | |
| [] | |
| 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. | |
| [] | |
| 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; | |
| [] | |
| 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); | |
| } | |
| } | |
| } | |