Ingen beskrivning
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.

ShaderGraphProjectSettings.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using UnityEngine.Serialization;
  4. using UnityEngine.UIElements;
  5. namespace UnityEditor.ShaderGraph
  6. {
  7. [FilePath("ProjectSettings/ShaderGraphSettings.asset", FilePathAttribute.Location.ProjectFolder)]
  8. internal class ShaderGraphProjectSettings : ScriptableSingleton<ShaderGraphProjectSettings>
  9. {
  10. [SerializeField]
  11. internal int shaderVariantLimit = 128;
  12. [SerializeField]
  13. internal int customInterpolatorErrorThreshold = 32;
  14. [SerializeField]
  15. internal int customInterpolatorWarningThreshold = 16;
  16. [SerializeField]
  17. internal ShaderGraphHeatmapValues customHeatmapValues;
  18. internal SerializedObject GetSerializedObject() { return new SerializedObject(this); }
  19. internal void Save() { Save(true); }
  20. private void OnDisable() { Save(); }
  21. public ShaderGraphHeatmapValues GetHeatValues()
  22. {
  23. return customHeatmapValues != null ? customHeatmapValues : ShaderGraphHeatmapValues.GetPackageDefault();
  24. }
  25. }
  26. class ShaderGraphProjectSettingsProvider : SettingsProvider
  27. {
  28. private static int kMaxChannelThreshold = 32;
  29. private static int kMinChannelThreshold = 8;
  30. private static string kCustomInterpolatorHelpBox = "Unity uses these options to help ShaderGraph users maintain known compatibilities with target platform(s) when using Custom Interpolators.";
  31. private static string kCustomInterpolatorDocumentationURL = UnityEngine.Rendering.ShaderGraph.Documentation.GetPageLink("Custom-Interpolators");
  32. private class Styles
  33. {
  34. public static readonly GUIContent shaderVariantLimitLabel = L10n.TextContent("Shader Variant Limit", "");
  35. public static readonly GUIContent CustomInterpLabel = L10n.TextContent("Custom Interpolator Channel Settings", "");
  36. public static readonly GUIContent CustomInterpWarnThresholdLabel = L10n.TextContent("Warning Threshold", $"ShaderGraph displays a warning when the user creates more custom interpolators than permitted by this setting. The number of interpolators that trigger this warning must be between {kMinChannelThreshold} and the Error Threshold.");
  37. public static readonly GUIContent CustomInterpErrorThresholdLabel = L10n.TextContent("Error Threshold", $"ShaderGraph displays an error message when the user tries to create more custom interpolators than permitted by this setting. The number of interpolators that trigger this error must be between {kMinChannelThreshold} and {kMaxChannelThreshold}.");
  38. public static readonly GUIContent ReadMore = L10n.TextContent("Read more");
  39. public static readonly GUIContent HeatmapSectionLabel = L10n.TextContent("Heatmap Color Mode Settings", "");
  40. public static readonly GUIContent HeatmapAssetLabel = L10n.TextContent("Custom Values", "Specifies a custom Heatmap Values asset with data to display in the Heatmap color mode. If empty, a set of default values will be used.");
  41. }
  42. SerializedObject m_SerializedObject;
  43. SerializedProperty m_shaderVariantLimit;
  44. SerializedProperty m_customInterpWarn;
  45. SerializedProperty m_customInterpError;
  46. SerializedProperty m_HeatValues;
  47. public ShaderGraphProjectSettingsProvider(string path, SettingsScope scopes, IEnumerable<string> keywords = null) : base(path, scopes, keywords)
  48. {
  49. guiHandler = OnGUIHandler;
  50. }
  51. public override void OnActivate(string searchContext, VisualElement rootElement)
  52. {
  53. ShaderGraphProjectSettings.instance.Save();
  54. m_SerializedObject = ShaderGraphProjectSettings.instance.GetSerializedObject();
  55. m_shaderVariantLimit = m_SerializedObject.FindProperty("shaderVariantLimit");
  56. m_customInterpWarn = m_SerializedObject.FindProperty("customInterpolatorWarningThreshold");
  57. m_customInterpError = m_SerializedObject.FindProperty("customInterpolatorErrorThreshold");
  58. m_HeatValues = m_SerializedObject.FindProperty(nameof(ShaderGraphProjectSettings.customHeatmapValues));
  59. }
  60. int oldWarningThreshold;
  61. void OnGUIHandler(string searchContext)
  62. {
  63. m_SerializedObject.Update();
  64. EditorGUI.BeginChangeCheck();
  65. var newValue = EditorGUILayout.DelayedIntField(Styles.shaderVariantLimitLabel, m_shaderVariantLimit.intValue);
  66. if (newValue != m_shaderVariantLimit.intValue)
  67. {
  68. m_shaderVariantLimit.intValue = newValue;
  69. ShaderGraphPreferences.onVariantLimitChanged();
  70. }
  71. EditorGUILayout.LabelField(Styles.CustomInterpLabel, EditorStyles.boldLabel);
  72. EditorGUI.indentLevel++;
  73. int newError = EditorGUILayout.IntField(Styles.CustomInterpErrorThresholdLabel, m_customInterpError.intValue);
  74. m_customInterpError.intValue = Mathf.Clamp(newError, kMinChannelThreshold, kMaxChannelThreshold);
  75. int oldWarn = m_customInterpWarn.intValue;
  76. int newWarn = EditorGUILayout.IntField(Styles.CustomInterpWarnThresholdLabel, m_customInterpWarn.intValue);
  77. // If the user did not modify the warning field, restore their previous input and reclamp against the new error threshold.
  78. if (oldWarn == newWarn)
  79. newWarn = oldWarningThreshold;
  80. else
  81. oldWarningThreshold = newWarn;
  82. m_customInterpWarn.intValue = Mathf.Clamp(newWarn, kMinChannelThreshold, m_customInterpError.intValue);
  83. GUILayout.BeginHorizontal(EditorStyles.helpBox);
  84. GUILayout.Label(EditorGUIUtility.IconContent("console.infoicon"), GUILayout.ExpandWidth(true));
  85. GUILayout.Box(kCustomInterpolatorHelpBox, EditorStyles.wordWrappedLabel);
  86. if (EditorGUILayout.LinkButton(Styles.ReadMore))
  87. {
  88. System.Diagnostics.Process.Start(kCustomInterpolatorDocumentationURL);
  89. }
  90. GUILayout.EndHorizontal();
  91. EditorGUI.indentLevel--;
  92. EditorGUILayout.LabelField(Styles.HeatmapSectionLabel, EditorStyles.boldLabel);
  93. EditorGUI.indentLevel++;
  94. var oldHeatValues = (ShaderGraphHeatmapValues) m_HeatValues.objectReferenceValue;
  95. var newHeatValues = EditorGUILayout.ObjectField(Styles.HeatmapAssetLabel, oldHeatValues, typeof(ShaderGraphHeatmapValues), false);
  96. if (oldHeatValues != newHeatValues)
  97. {
  98. m_HeatValues.objectReferenceValue = newHeatValues;
  99. }
  100. EditorGUI.indentLevel--;
  101. if (EditorGUI.EndChangeCheck())
  102. {
  103. m_SerializedObject.ApplyModifiedProperties();
  104. ShaderGraphProjectSettings.instance.Save();
  105. if (oldHeatValues != newHeatValues)
  106. {
  107. ShaderGraphHeatmapValuesEditor.UpdateShaderGraphWindows();
  108. }
  109. }
  110. }
  111. [SettingsProvider]
  112. public static SettingsProvider CreateShaderGraphProjectSettingsProvider()
  113. {
  114. var provider = new ShaderGraphProjectSettingsProvider("Project/ShaderGraph", SettingsScope.Project);
  115. return provider;
  116. }
  117. }
  118. }