GlobalSettings.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using UnityEngine;
  2. using UnityEngine.Rendering.PostProcessing;
  3. namespace UnityEditor.Rendering.PostProcessing
  4. {
  5. static class GlobalSettings
  6. {
  7. static class Keys
  8. {
  9. internal const string trackballSensitivity = "PostProcessing.Trackball.Sensitivity";
  10. internal const string volumeGizmoColor = "PostProcessing.Volume.GizmoColor";
  11. internal const string currentChannelMixer = "PostProcessing.ChannelMixer.CurrentChannel";
  12. internal const string currentCurve = "PostProcessing.Curve.Current";
  13. }
  14. static bool m_Loaded = false;
  15. static float m_TrackballSensitivity = 0.2f;
  16. internal static float trackballSensitivity
  17. {
  18. get { return m_TrackballSensitivity; }
  19. set { TrySave(ref m_TrackballSensitivity, value, Keys.trackballSensitivity); }
  20. }
  21. static Color m_VolumeGizmoColor = new Color(0.2f, 0.8f, 0.1f, 0.5f);
  22. internal static Color volumeGizmoColor
  23. {
  24. get { return m_VolumeGizmoColor; }
  25. set { TrySave(ref m_VolumeGizmoColor, value, Keys.volumeGizmoColor); }
  26. }
  27. static int m_CurrentChannelMixer = 0;
  28. internal static int currentChannelMixer
  29. {
  30. get { return m_CurrentChannelMixer; }
  31. set { TrySave(ref m_CurrentChannelMixer, value, Keys.currentChannelMixer); }
  32. }
  33. static int m_CurrentCurve = 0;
  34. internal static int currentCurve
  35. {
  36. get { return m_CurrentCurve; }
  37. set { TrySave(ref m_CurrentCurve, value, Keys.currentCurve); }
  38. }
  39. static GlobalSettings()
  40. {
  41. Load();
  42. }
  43. #if UNITY_2018_3_OR_NEWER
  44. [SettingsProvider]
  45. static SettingsProvider PreferenceGUI()
  46. {
  47. return new SettingsProvider("Preferences/Post-processing", SettingsScope.User)
  48. {
  49. guiHandler = searchContext => OpenGUI()
  50. };
  51. }
  52. #else
  53. [PreferenceItem("Post-processing")]
  54. static void PreferenceGUI()
  55. {
  56. OpenGUI();
  57. }
  58. #endif
  59. static void OpenGUI()
  60. {
  61. if (!m_Loaded)
  62. Load();
  63. EditorGUILayout.Space();
  64. trackballSensitivity = EditorGUILayout.Slider("Trackballs Sensitivity", trackballSensitivity, 0.05f, 1f);
  65. volumeGizmoColor = EditorGUILayout.ColorField("Volume Gizmo Color", volumeGizmoColor);
  66. }
  67. static void Load()
  68. {
  69. m_TrackballSensitivity = EditorPrefs.GetFloat(Keys.trackballSensitivity, 0.2f);
  70. m_VolumeGizmoColor = GetColor(Keys.volumeGizmoColor, new Color(0.2f, 0.8f, 0.1f, 0.5f));
  71. m_CurrentChannelMixer = EditorPrefs.GetInt(Keys.currentChannelMixer, 0);
  72. m_CurrentCurve = EditorPrefs.GetInt(Keys.currentCurve, 0);
  73. m_Loaded = true;
  74. }
  75. static Color GetColor(string key, Color defaultValue)
  76. {
  77. int value = EditorPrefs.GetInt(key, (int)ColorUtilities.ToHex(defaultValue));
  78. return ColorUtilities.ToRGBA((uint)value);
  79. }
  80. static void TrySave<T>(ref T field, T newValue, string key)
  81. {
  82. if (field.Equals(newValue))
  83. return;
  84. if (typeof(T) == typeof(float))
  85. EditorPrefs.SetFloat(key, (float)(object)newValue);
  86. else if (typeof(T) == typeof(int))
  87. EditorPrefs.SetInt(key, (int)(object)newValue);
  88. else if (typeof(T) == typeof(bool))
  89. EditorPrefs.SetBool(key, (bool)(object)newValue);
  90. else if (typeof(T) == typeof(string))
  91. EditorPrefs.SetString(key, (string)(object)newValue);
  92. else if (typeof(T) == typeof(Color))
  93. EditorPrefs.SetInt(key, (int)ColorUtilities.ToHex((Color)(object)newValue));
  94. field = newValue;
  95. }
  96. }
  97. }