Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShaderGraphPreferences.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.ShaderGraph
  4. {
  5. static class ShaderGraphPreferences
  6. {
  7. static class Keys
  8. {
  9. internal const string variantLimit = "UnityEditor.ShaderGraph.VariantLimit";
  10. internal const string autoAddRemoveBlocks = "UnityEditor.ShaderGraph.AutoAddRemoveBlocks";
  11. internal const string allowDeprecatedBehaviors = "UnityEditor.ShaderGraph.AllowDeprecatedBehaviors";
  12. internal const string zoomStepSize = "UnityEditor.ShaderGraph.ZoomStepSize";
  13. }
  14. static bool m_Loaded = false;
  15. internal delegate void PreferenceChangedDelegate();
  16. internal static PreferenceChangedDelegate onVariantLimitChanged;
  17. static int m_PreviewVariantLimit = 128;
  18. internal static int previewVariantLimit
  19. {
  20. get { return m_PreviewVariantLimit; }
  21. set
  22. {
  23. if (onVariantLimitChanged != null)
  24. onVariantLimitChanged();
  25. TrySave(ref m_PreviewVariantLimit, value, Keys.variantLimit);
  26. }
  27. }
  28. static bool m_AutoAddRemoveBlocks = true;
  29. internal static bool autoAddRemoveBlocks
  30. {
  31. get => m_AutoAddRemoveBlocks;
  32. set => TrySave(ref m_AutoAddRemoveBlocks, value, Keys.autoAddRemoveBlocks);
  33. }
  34. internal static PreferenceChangedDelegate onAllowDeprecatedChanged;
  35. static bool m_AllowDeprecatedBehaviors = false;
  36. internal static bool allowDeprecatedBehaviors
  37. {
  38. get => m_AllowDeprecatedBehaviors;
  39. set
  40. {
  41. TrySave(ref m_AllowDeprecatedBehaviors, value, Keys.allowDeprecatedBehaviors);
  42. if (onAllowDeprecatedChanged != null)
  43. {
  44. onAllowDeprecatedChanged();
  45. }
  46. }
  47. }
  48. internal static PreferenceChangedDelegate onZoomStepSizeChanged;
  49. const float defaultZoomStepSize = 0.5f;
  50. static float m_ZoomStepSize = defaultZoomStepSize;
  51. internal static float zoomStepSize
  52. {
  53. get => m_ZoomStepSize;
  54. set
  55. {
  56. TrySave(ref m_ZoomStepSize, value, Keys.zoomStepSize);
  57. if (onZoomStepSizeChanged != null)
  58. {
  59. onZoomStepSizeChanged();
  60. }
  61. }
  62. }
  63. static ShaderGraphPreferences()
  64. {
  65. Load();
  66. }
  67. [SettingsProvider]
  68. static SettingsProvider PreferenceGUI()
  69. {
  70. return new SettingsProvider("Preferences/Shader Graph", SettingsScope.User)
  71. {
  72. guiHandler = searchContext => OpenGUI()
  73. };
  74. }
  75. static void OpenGUI()
  76. {
  77. if (!m_Loaded)
  78. Load();
  79. var previousLabelWidth = EditorGUIUtility.labelWidth;
  80. EditorGUIUtility.labelWidth = 256;
  81. EditorGUILayout.Space();
  82. EditorGUI.BeginChangeCheck();
  83. var actualLimit = ShaderGraphProjectSettings.instance.shaderVariantLimit;
  84. var willPreviewVariantBeIgnored = ShaderGraphPreferences.previewVariantLimit > actualLimit;
  85. var variantLimitLabel = willPreviewVariantBeIgnored
  86. ? new GUIContent("Preview Variant Limit", EditorGUIUtility.IconContent("console.infoicon").image, $"The Preview Variant Limit is higher than the Shader Variant Limit in Project Settings: {actualLimit}. The Preview Variant Limit will be ignored.")
  87. : new GUIContent("Preview Variant Limit");
  88. var variantLimitValue = EditorGUILayout.DelayedIntField(variantLimitLabel, previewVariantLimit);
  89. if (EditorGUI.EndChangeCheck())
  90. {
  91. previewVariantLimit = variantLimitValue;
  92. }
  93. EditorGUI.BeginChangeCheck();
  94. var autoAddRemoveBlocksValue = EditorGUILayout.Toggle("Automatically Add and Remove Block Nodes", autoAddRemoveBlocks);
  95. if (EditorGUI.EndChangeCheck())
  96. {
  97. autoAddRemoveBlocks = autoAddRemoveBlocksValue;
  98. }
  99. EditorGUI.BeginChangeCheck();
  100. var allowDeprecatedBehaviorsValue = EditorGUILayout.Toggle("Enable Deprecated Nodes", allowDeprecatedBehaviors);
  101. if (EditorGUI.EndChangeCheck())
  102. {
  103. allowDeprecatedBehaviors = allowDeprecatedBehaviorsValue;
  104. }
  105. EditorGUI.BeginChangeCheck();
  106. var zoomStepSizeValue = EditorGUILayout.Slider(new GUIContent("Zoom Step Size", $"Default is 0.5"), zoomStepSize, 0.0f, 1f);
  107. if (EditorGUI.EndChangeCheck())
  108. {
  109. zoomStepSize = zoomStepSizeValue;
  110. }
  111. EditorGUIUtility.labelWidth = previousLabelWidth;
  112. }
  113. static void Load()
  114. {
  115. m_PreviewVariantLimit = EditorPrefs.GetInt(Keys.variantLimit, 128);
  116. m_AutoAddRemoveBlocks = EditorPrefs.GetBool(Keys.autoAddRemoveBlocks, true);
  117. m_AllowDeprecatedBehaviors = EditorPrefs.GetBool(Keys.allowDeprecatedBehaviors, false);
  118. m_ZoomStepSize = EditorPrefs.GetFloat(Keys.zoomStepSize, defaultZoomStepSize);
  119. m_Loaded = true;
  120. }
  121. static void TrySave<T>(ref T field, T newValue, string key)
  122. {
  123. if (field.Equals(newValue))
  124. return;
  125. if (typeof(T) == typeof(float))
  126. EditorPrefs.SetFloat(key, (float)(object)newValue);
  127. else if (typeof(T) == typeof(int))
  128. EditorPrefs.SetInt(key, (int)(object)newValue);
  129. else if (typeof(T) == typeof(bool))
  130. EditorPrefs.SetBool(key, (bool)(object)newValue);
  131. else if (typeof(T) == typeof(string))
  132. EditorPrefs.SetString(key, (string)(object)newValue);
  133. field = newValue;
  134. }
  135. }
  136. }