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