ShaderGraphPreferences.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using UnityEngine;
  2. namespace UnityEditor.ShaderGraph
  3. {
  4. static class ShaderGraphPreferences
  5. {
  6. static class Keys
  7. {
  8. internal const string variantLimit = "UnityEditor.ShaderGraph.VariantLimit";
  9. }
  10. static bool m_Loaded = false;
  11. internal delegate void PreferenceChangedDelegate();
  12. internal static PreferenceChangedDelegate onVariantLimitChanged;
  13. static int m_VariantLimit = 128;
  14. internal static int variantLimit
  15. {
  16. get { return m_VariantLimit; }
  17. set
  18. {
  19. if(onVariantLimitChanged != null)
  20. onVariantLimitChanged();
  21. TrySave(ref m_VariantLimit, value, Keys.variantLimit);
  22. }
  23. }
  24. static ShaderGraphPreferences()
  25. {
  26. Load();
  27. }
  28. [SettingsProvider]
  29. static SettingsProvider PreferenceGUI()
  30. {
  31. return new SettingsProvider("Preferences/Shader Graph", SettingsScope.User)
  32. {
  33. guiHandler = searchContext => OpenGUI()
  34. };
  35. }
  36. static void OpenGUI()
  37. {
  38. if (!m_Loaded)
  39. Load();
  40. EditorGUILayout.Space();
  41. EditorGUI.BeginChangeCheck ();
  42. var variantLimitValue = EditorGUILayout.DelayedIntField("Shader Variant Limit", variantLimit);
  43. if (EditorGUI.EndChangeCheck ())
  44. {
  45. variantLimit = variantLimitValue;
  46. }
  47. }
  48. static void Load()
  49. {
  50. m_VariantLimit = EditorPrefs.GetInt(Keys.variantLimit, 128);
  51. m_Loaded = true;
  52. }
  53. static void TrySave<T>(ref T field, T newValue, string key)
  54. {
  55. if (field.Equals(newValue))
  56. return;
  57. if (typeof(T) == typeof(float))
  58. EditorPrefs.SetFloat(key, (float)(object)newValue);
  59. else if (typeof(T) == typeof(int))
  60. EditorPrefs.SetInt(key, (int)(object)newValue);
  61. else if (typeof(T) == typeof(bool))
  62. EditorPrefs.SetBool(key, (bool)(object)newValue);
  63. else if (typeof(T) == typeof(string))
  64. EditorPrefs.SetString(key, (string)(object)newValue);
  65. field = newValue;
  66. }
  67. }
  68. }