A game about forced loneliness, made by TACStudios
at master 7.5 kB view raw
1using UnityEngine; 2using System.Collections.Generic; 3using UnityEngine.Serialization; 4using UnityEngine.UIElements; 5 6namespace UnityEditor.ShaderGraph 7{ 8 [FilePath("ProjectSettings/ShaderGraphSettings.asset", FilePathAttribute.Location.ProjectFolder)] 9 internal class ShaderGraphProjectSettings : ScriptableSingleton<ShaderGraphProjectSettings> 10 { 11 [SerializeField] 12 internal int shaderVariantLimit = 2048; 13 [SerializeField] 14 internal int customInterpolatorErrorThreshold = 32; 15 [SerializeField] 16 internal int customInterpolatorWarningThreshold = 16; 17 [SerializeField] 18 internal ShaderGraphHeatmapValues customHeatmapValues; 19 20 internal SerializedObject GetSerializedObject() { return new SerializedObject(this); } 21 internal void Save() { Save(true); } 22 private void OnDisable() { Save(); } 23 24 public ShaderGraphHeatmapValues GetHeatValues() 25 { 26 return customHeatmapValues != null ? customHeatmapValues : ShaderGraphHeatmapValues.GetPackageDefault(); 27 } 28 } 29 30 class ShaderGraphProjectSettingsProvider : SettingsProvider 31 { 32 private static int kMaxChannelThreshold = 32; 33 private static int kMinChannelThreshold = 8; 34 private static string kCustomInterpolatorHelpBox = "Unity uses these options to help ShaderGraph users maintain known compatibilities with target platform(s) when using Custom Interpolators."; 35 private static string kCustomInterpolatorDocumentationURL = UnityEngine.Rendering.ShaderGraph.Documentation.GetPageLink("Custom-Interpolators"); 36 37 private class Styles 38 { 39 public static readonly GUIContent shaderVariantLimitLabel = L10n.TextContent("Shader Variant Limit", ""); 40 public static readonly GUIContent CustomInterpLabel = L10n.TextContent("Custom Interpolator Channel Settings", ""); 41 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."); 42 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}."); 43 public static readonly GUIContent ReadMore = L10n.TextContent("Read more"); 44 45 public static readonly GUIContent HeatmapSectionLabel = L10n.TextContent("Heatmap Color Mode Settings", ""); 46 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."); 47 } 48 49 SerializedObject m_SerializedObject; 50 SerializedProperty m_shaderVariantLimit; 51 SerializedProperty m_customInterpWarn; 52 SerializedProperty m_customInterpError; 53 SerializedProperty m_HeatValues; 54 55 public ShaderGraphProjectSettingsProvider(string path, SettingsScope scopes, IEnumerable<string> keywords = null) : base(path, scopes, keywords) 56 { 57 guiHandler = OnGUIHandler; 58 } 59 60 public override void OnActivate(string searchContext, VisualElement rootElement) 61 { 62 ShaderGraphProjectSettings.instance.Save(); 63 m_SerializedObject = ShaderGraphProjectSettings.instance.GetSerializedObject(); 64 m_shaderVariantLimit = m_SerializedObject.FindProperty("shaderVariantLimit"); 65 m_customInterpWarn = m_SerializedObject.FindProperty("customInterpolatorWarningThreshold"); 66 m_customInterpError = m_SerializedObject.FindProperty("customInterpolatorErrorThreshold"); 67 m_HeatValues = m_SerializedObject.FindProperty(nameof(ShaderGraphProjectSettings.customHeatmapValues)); 68 } 69 70 int oldWarningThreshold; 71 void OnGUIHandler(string searchContext) 72 { 73 m_SerializedObject.Update(); 74 75 EditorGUI.BeginChangeCheck(); 76 77 var newVariantLimitValue = EditorGUILayout.DelayedIntField(Styles.shaderVariantLimitLabel, m_shaderVariantLimit.intValue); 78 newVariantLimitValue = Mathf.Max(0, newVariantLimitValue); 79 if (newVariantLimitValue != m_shaderVariantLimit.intValue) 80 { 81 m_shaderVariantLimit.intValue = newVariantLimitValue; 82 if (ShaderGraphPreferences.onVariantLimitChanged != null) 83 ShaderGraphPreferences.onVariantLimitChanged(); 84 } 85 86 EditorGUILayout.LabelField(Styles.CustomInterpLabel, EditorStyles.boldLabel); 87 EditorGUI.indentLevel++; 88 89 int newError = EditorGUILayout.IntField(Styles.CustomInterpErrorThresholdLabel, m_customInterpError.intValue); 90 m_customInterpError.intValue = Mathf.Clamp(newError, kMinChannelThreshold, kMaxChannelThreshold); 91 92 int oldWarn = m_customInterpWarn.intValue; 93 int newWarn = EditorGUILayout.IntField(Styles.CustomInterpWarnThresholdLabel, m_customInterpWarn.intValue); 94 95 // If the user did not modify the warning field, restore their previous input and reclamp against the new error threshold. 96 if (oldWarn == newWarn) 97 newWarn = oldWarningThreshold; 98 else 99 oldWarningThreshold = newWarn; 100 101 m_customInterpWarn.intValue = Mathf.Clamp(newWarn, kMinChannelThreshold, m_customInterpError.intValue); 102 103 GUILayout.BeginHorizontal(EditorStyles.helpBox); 104 GUILayout.Label(EditorGUIUtility.IconContent("console.infoicon"), GUILayout.ExpandWidth(true)); 105 GUILayout.Box(kCustomInterpolatorHelpBox, EditorStyles.wordWrappedLabel); 106 if (EditorGUILayout.LinkButton(Styles.ReadMore)) 107 { 108 System.Diagnostics.Process.Start(kCustomInterpolatorDocumentationURL); 109 } 110 GUILayout.EndHorizontal(); 111 EditorGUI.indentLevel--; 112 113 EditorGUILayout.LabelField(Styles.HeatmapSectionLabel, EditorStyles.boldLabel); 114 EditorGUI.indentLevel++; 115 var oldHeatValues = (ShaderGraphHeatmapValues) m_HeatValues.objectReferenceValue; 116 var newHeatValues = EditorGUILayout.ObjectField(Styles.HeatmapAssetLabel, oldHeatValues, typeof(ShaderGraphHeatmapValues), false); 117 if (oldHeatValues != newHeatValues) 118 { 119 m_HeatValues.objectReferenceValue = newHeatValues; 120 } 121 122 EditorGUI.indentLevel--; 123 124 if (EditorGUI.EndChangeCheck()) 125 { 126 m_SerializedObject.ApplyModifiedProperties(); 127 ShaderGraphProjectSettings.instance.Save(); 128 129 if (oldHeatValues != newHeatValues) 130 { 131 ShaderGraphHeatmapValuesEditor.UpdateShaderGraphWindows(); 132 } 133 } 134 } 135 136 [SettingsProvider] 137 public static SettingsProvider CreateShaderGraphProjectSettingsProvider() 138 { 139 var provider = new ShaderGraphProjectSettingsProvider("Project/ShaderGraph", SettingsScope.Project); 140 return provider; 141 } 142 } 143}