SettingsExamples.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.SettingsManagement.Examples
  4. {
  5. [Serializable]
  6. class FooClass
  7. {
  8. public int intValue;
  9. public string stringValue;
  10. public FooClass()
  11. {
  12. intValue = 42;
  13. stringValue = "I'm some text";
  14. }
  15. }
  16. class MySettingsExamples : EditorWindow
  17. {
  18. #pragma warning disable 414
  19. // [UserSetting] attribute registers this setting with the UserSettingsProvider so that it can be automatically
  20. // shown in the UI.
  21. [UserSetting("General Settings", "Days Without Incident")]
  22. static MySetting<int> s_NumberOfDaysWithoutIncident = new MySetting<int>("general.daysWithoutIncident", 0, SettingsScope.User);
  23. [UserSetting("General Settings", "Favorite Color")]
  24. static MySetting<Color> s_FavoriteColor = new MySetting<Color>("general.favoriteColor", Color.magenta);
  25. [UserSetting("General Settings", "Vector2 Field")]
  26. static MySetting<Vector2> s_Vector2Value = new MySetting<Vector2>("general.vector2Value", new Vector2(2f, 4f));
  27. [UserSetting("General Settings", "Editor Flags")]
  28. static MySetting<StaticEditorFlags> s_EditorFlags = new MySetting<StaticEditorFlags>("general.editorFlags", StaticEditorFlags.BatchingStatic);
  29. #pragma warning restore 414
  30. // [UserSetting] with no arguments simply registers the key with UserSettingsProvider so that it can be included
  31. // in debug views and reset with the options gizmo. Usually this is used in conjunction with [UserSettingsBlock].
  32. [UserSetting]
  33. static MySetting<FooClass> s_Foo = new MySetting<FooClass>("general.foo", new FooClass(), SettingsScope.Project);
  34. [UserSetting]
  35. static MySetting<int> s_NumberWithSlider = new MySetting<int>("general.conditionalValue", 5, SettingsScope.Project);
  36. // A UserSettingBlock is a callback invoked from the UserSettingsProvider. It allows you to draw more complicated
  37. // UI elements without the need to create a new SettingsProvider. Parameters are "category" and "search keywords."
  38. // For maximum compatibility, use `SettingsGUILayout` searchable and settings fields to get features like search
  39. // and per-setting reset with a context click.
  40. [UserSettingBlock("Custom GUI Settings")]
  41. static void ConditionalValueGUI(string searchContext)
  42. {
  43. EditorGUI.BeginChangeCheck();
  44. s_NumberWithSlider.value = SettingsGUILayout.SettingsSlider("Number With Slider", s_NumberWithSlider, 0, 10, searchContext);
  45. var foo = s_Foo.value;
  46. using(new SettingsGUILayout.IndentedGroup("Foo Class"))
  47. {
  48. EditorGUI.BeginChangeCheck();
  49. foo.intValue = SettingsGUILayout.SearchableIntField("Int Value", foo.intValue, searchContext);
  50. foo.stringValue = SettingsGUILayout.SearchableTextField("String Value", foo.stringValue, searchContext);
  51. // Because FooClass is a reference type, we need to apply the changes to the backing repository (SetValue
  52. // would also work here).
  53. if (EditorGUI.EndChangeCheck())
  54. s_Foo.ApplyModifiedProperties();
  55. }
  56. SettingsGUILayout.DoResetContextMenuForLastRect(s_Foo);
  57. if (EditorGUI.EndChangeCheck())
  58. MySettingsManager.Save();
  59. }
  60. const string k_ColorInstanceFieldKey = "MySettingsExamples.m_ColorField";
  61. // It is also possible to forego the UserSetting<T> wrapper and use a settings instance directly. To register
  62. // a setting with the "Reset All" option of UserSettingsProvider, apply the [UserSetting] (or for instance fields [SettingsKey]) attribute.
  63. Color m_ColorField;
  64. [MenuItem("Window/Show Settings Examples")]
  65. static void Init()
  66. {
  67. GetWindow<MySettingsExamples>();
  68. }
  69. void OnEnable()
  70. {
  71. m_ColorField = MySettingsManager.Get<Color>(k_ColorInstanceFieldKey);
  72. }
  73. void OnGUI()
  74. {
  75. EditorGUI.BeginChangeCheck();
  76. m_ColorField = EditorGUILayout.ColorField("Color", m_ColorField);
  77. if (EditorGUI.EndChangeCheck())
  78. MySettingsManager.Set<Color>(k_ColorInstanceFieldKey, m_ColorField);
  79. }
  80. }
  81. }