|
1 |
| -#if UNITY_EDITOR |
2 |
| - |
3 |
| -using UnityEditor; |
4 |
| -using UnityEngine; |
5 |
| - |
6 |
| -namespace ScriptableObjectArchitecture |
7 |
| -{ |
8 |
| - /// <summary> |
9 |
| - /// An editor class for managing project and user preferences for the SOArchitecture library. This is kept |
10 |
| - /// in the runtime assembly for the purpose of enabling editor-only additional features when playing such as |
11 |
| - /// gizmos and debugging. |
12 |
| - /// </summary> |
13 |
| - public static class SOArchitecturePreferences |
14 |
| - { |
15 |
| - /// <summary> |
16 |
| - /// Returns true if debug features should be enabled, otherwise false. |
17 |
| - /// </summary> |
18 |
| - public static bool IsDebugEnabled |
19 |
| - { |
20 |
| - get { return GetBoolPref(ENABLE_DEBUG_PREF, ENABLE_DEBUG_DEFAULT); } |
21 |
| - } |
22 |
| - |
23 |
| - /// <summary> |
24 |
| - /// Returns true if Gizmos should be enabled, otherwise false. |
25 |
| - /// </summary> |
26 |
| - public static bool AreGizmosEnabled |
27 |
| - { |
28 |
| - get { return GetBoolPref(DRAW_EVENT_GIZMOS_PREF, DRAW_EVENT_GIZMOS_DEFAULT); } |
29 |
| - } |
30 |
| - |
31 |
| - // UI |
32 |
| - private const string PREFERENCES_TITLE_PATH = "Preferences/SOArchitecture"; |
33 |
| - private const string PROJECT_TITLE_PATH = "Project/SOArchitecture"; |
34 |
| - |
35 |
| - private const string USER_PREFERENCES_HEADER = "User Preferences"; |
36 |
| - private const string PROJECT_REFERENCES_HEADER = "Project Preferences"; |
37 |
| - |
38 |
| - private const string CODE_GEN_DIRECTORY_LABEL = "Code Generation Output Directory"; |
39 |
| - private const string CODE_GEN_DIRECTORY_DESCRIPTION |
40 |
| - = "The directory where the output of code generation will write to."; |
41 |
| - |
42 |
| - private const string ALLOW_OVERWRITE_LABEL = "Allow Code Generation to Overwrite"; |
43 |
| - private const string ALLOW_OVERWRITE_DESCRIPTION = |
44 |
| - "Allow newly generated code files to overwrite existing ones."; |
45 |
| - |
46 |
| - private const string ASSET_MENU_ORDER_LABEL = "Create Asset Menu Order"; |
47 |
| - private const string ASSET_MENU_ORDER_DESCRIPTION = |
48 |
| - "This determines the order in which the CreateAsset Context Menu will be placed into."; |
49 |
| - |
50 |
| - private static readonly GUILayoutOption MAX_WIDTH; |
51 |
| - |
52 |
| -#if UNITY_2018_3_OR_NEWER |
53 |
| - // Searchable Fields |
54 |
| - private static readonly string[] KEYWORDS = |
55 |
| - { |
56 |
| - "Scriptable", |
57 |
| - "Architecture" |
| 1 | +#if UNITY_EDITOR |
| 2 | + |
| 3 | +using UnityEditor; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +namespace ScriptableObjectArchitecture |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// An editor class for managing project and user preferences for the SOArchitecture library. This is kept |
| 10 | + /// in the runtime assembly for the purpose of enabling editor-only additional features when playing such as |
| 11 | + /// gizmos and debugging. |
| 12 | + /// </summary> |
| 13 | + public static class SOArchitecturePreferences |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Returns true if debug features should be enabled, otherwise false. |
| 17 | + /// </summary> |
| 18 | + public static bool IsDebugEnabled |
| 19 | + { |
| 20 | + get { return GetBoolPref(ENABLE_DEBUG_PREF, ENABLE_DEBUG_DEFAULT); } |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Returns true if Gizmos should be enabled, otherwise false. |
| 25 | + /// </summary> |
| 26 | + public static bool AreGizmosEnabled |
| 27 | + { |
| 28 | + get { return GetBoolPref(DRAW_EVENT_GIZMOS_PREF, DRAW_EVENT_GIZMOS_DEFAULT); } |
| 29 | + } |
| 30 | + |
| 31 | + // UI |
| 32 | + private const string PREFERENCES_TITLE_PATH = "Preferences/SOArchitecture"; |
| 33 | + private const string PROJECT_TITLE_PATH = "Project/SOArchitecture"; |
| 34 | + |
| 35 | + private const string USER_PREFERENCES_HEADER = "User Preferences"; |
| 36 | + private const string PROJECT_REFERENCES_HEADER = "Project Preferences"; |
| 37 | + |
| 38 | + private const string CODE_GEN_DIRECTORY_LABEL = "Code Generation Output Directory"; |
| 39 | + private const string CODE_GEN_DIRECTORY_DESCRIPTION |
| 40 | + = "The directory where the output of code generation will write to."; |
| 41 | + |
| 42 | + private const string ALLOW_OVERWRITE_LABEL = "Allow Code Generation to Overwrite"; |
| 43 | + private const string ALLOW_OVERWRITE_DESCRIPTION = |
| 44 | + "Allow newly generated code files to overwrite existing ones."; |
| 45 | + |
| 46 | + private const string ASSET_MENU_ORDER_LABEL = "Create Asset Menu Order"; |
| 47 | + private const string ASSET_MENU_ORDER_DESCRIPTION = |
| 48 | + "This determines the order in which the CreateAsset Context Menu will be placed into."; |
| 49 | + |
| 50 | + private static readonly GUILayoutOption MAX_WIDTH; |
| 51 | + |
| 52 | +#if UNITY_2018_3_OR_NEWER |
| 53 | + // Searchable Fields |
| 54 | + private static readonly string[] KEYWORDS = |
| 55 | + { |
| 56 | + "Scriptable", |
| 57 | + "Architecture" |
58 | 58 | };
|
59 |
| -#endif |
60 |
| - |
61 |
| - // User Editor Preferences |
62 |
| - private const string DRAW_EVENT_GIZMOS_PREF = "SOArchitecture.DrawEventGizmoos"; |
63 |
| - private const string ENABLE_DEBUG_PREF = "SOArchitecture.EnableDebug"; |
64 |
| - |
65 |
| - private const bool DRAW_EVENT_GIZMOS_DEFAULT = true; |
66 |
| - private const bool ENABLE_DEBUG_DEFAULT = true; |
67 |
| - |
68 |
| - static SOArchitecturePreferences() |
69 |
| - { |
70 |
| - MAX_WIDTH = GUILayout.MaxWidth(200f); |
| 59 | +#endif |
| 60 | + |
| 61 | + // User Editor Preferences |
| 62 | + private const string DRAW_EVENT_GIZMOS_PREF = "SOArchitecture.DrawEventGizmoos"; |
| 63 | + private const string ENABLE_DEBUG_PREF = "SOArchitecture.EnableDebug"; |
| 64 | + |
| 65 | + private const bool DRAW_EVENT_GIZMOS_DEFAULT = true; |
| 66 | + private const bool ENABLE_DEBUG_DEFAULT = true; |
| 67 | + |
| 68 | + static SOArchitecturePreferences() |
| 69 | + { |
| 70 | + MAX_WIDTH = GUILayout.MaxWidth(200f); |
| 71 | + } |
| 72 | + |
| 73 | +#if UNITY_2018_3_OR_NEWER |
| 74 | + [SettingsProvider] |
| 75 | + private static SettingsProvider CreateProjectPreferenceSettingsProvider() |
| 76 | + { |
| 77 | + return new SettingsProvider(PROJECT_TITLE_PATH, SettingsScope.Project) |
| 78 | + { |
| 79 | + guiHandler = DrawProjectGUI, |
| 80 | + keywords = KEYWORDS |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + [SettingsProvider] |
| 85 | + private static SettingsProvider CreatePersonalPreferenceSettingsProvider() |
| 86 | + { |
| 87 | + return new SettingsProvider(PREFERENCES_TITLE_PATH, SettingsScope.User) |
| 88 | + { |
| 89 | + guiHandler = DrawPersonalPrefsGUI, |
| 90 | + keywords = KEYWORDS |
| 91 | + }; |
| 92 | + } |
| 93 | +#endif |
| 94 | + private static void DrawAllGUI() |
| 95 | + { |
| 96 | + DrawProjectGUI(); |
| 97 | + DrawPersonalPrefsGUI(); |
| 98 | + } |
| 99 | + |
| 100 | + private static void DrawProjectGUI(string value = "") |
| 101 | + { |
| 102 | + EditorGUILayout.LabelField(PROJECT_REFERENCES_HEADER, EditorStyles.boldLabel); |
| 103 | + |
| 104 | + var settings = SOArchitecture_Settings.Instance; |
| 105 | + |
| 106 | + GUI.changed = false; |
| 107 | + |
| 108 | + // Code Generation Target Directory |
| 109 | + EditorGUILayout.HelpBox(CODE_GEN_DIRECTORY_DESCRIPTION, MessageType.Info); |
| 110 | + using (new EditorGUILayout.HorizontalScope()) |
| 111 | + { |
| 112 | + EditorGUILayout.LabelField(new GUIContent(CODE_GEN_DIRECTORY_LABEL), MAX_WIDTH); |
| 113 | + var directory = EditorGUILayout.TextField(settings.CodeGenerationTargetDirectory); |
| 114 | + settings.CodeGenerationTargetDirectory = directory; |
| 115 | + } |
| 116 | + |
| 117 | + // Code Generation Allow Overwrite |
| 118 | + EditorGUILayout.HelpBox(ALLOW_OVERWRITE_DESCRIPTION, MessageType.Info); |
| 119 | + using (new EditorGUILayout.HorizontalScope()) |
| 120 | + { |
| 121 | + EditorGUILayout.LabelField(new GUIContent(ALLOW_OVERWRITE_LABEL), MAX_WIDTH); |
| 122 | + var newOverwrite = EditorGUILayout.Toggle(settings.CodeGenerationAllowOverwrite); |
| 123 | + settings.CodeGenerationAllowOverwrite = newOverwrite; |
| 124 | + } |
| 125 | + |
| 126 | + // Default Create Asset Menu Order |
| 127 | + EditorGUILayout.HelpBox(ASSET_MENU_ORDER_DESCRIPTION, MessageType.Info); |
| 128 | + using (new EditorGUILayout.HorizontalScope()) |
| 129 | + { |
| 130 | + EditorGUILayout.LabelField(ASSET_MENU_ORDER_LABEL, MAX_WIDTH); |
| 131 | + var newMenuOrder = EditorGUILayout.IntField(settings.DefaultCreateAssetMenuOrder); |
| 132 | + settings.DefaultCreateAssetMenuOrder = newMenuOrder; |
| 133 | + } |
| 134 | + |
| 135 | + if (GUI.changed) |
| 136 | + { |
| 137 | + EditorUtility.SetDirty(settings); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static void DrawPersonalPrefsGUI(string value = "") |
| 142 | + { |
| 143 | + EditorGUILayout.LabelField(USER_PREFERENCES_HEADER, EditorStyles.boldLabel); |
| 144 | + |
| 145 | + // Draw Event Gizmo |
| 146 | + var drawEventPref = GetBoolPref(DRAW_EVENT_GIZMOS_PREF, DRAW_EVENT_GIZMOS_DEFAULT); |
| 147 | + |
| 148 | + GUI.changed = false; |
| 149 | + drawEventPref = EditorGUILayout.Toggle("Draw Event Gizmo", drawEventPref); |
| 150 | + if (GUI.changed) |
| 151 | + { |
| 152 | + EditorPrefs.SetBool(DRAW_EVENT_GIZMOS_PREF, drawEventPref); |
| 153 | + } |
| 154 | + |
| 155 | + // Enable Debug |
| 156 | + EditorGUILayout.HelpBox("This will enable debug features for troubleshooting purposes such as " + |
| 157 | + "game events collecting stack traces. This will decrease performance " + |
| 158 | + "in-editor.", MessageType.Info); |
| 159 | + var enableDebugPref = GetBoolPref(ENABLE_DEBUG_PREF, ENABLE_DEBUG_DEFAULT); |
| 160 | + |
| 161 | + GUI.changed = false; |
| 162 | + enableDebugPref = EditorGUILayout.Toggle("Enable Debug", enableDebugPref); |
| 163 | + if (GUI.changed) |
| 164 | + { |
| 165 | + EditorPrefs.SetBool(ENABLE_DEBUG_PREF, enableDebugPref); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /// <summary> |
| 170 | + /// Returns the current bool preference; if none exists, the default is set and returned. |
| 171 | + /// </summary> |
| 172 | + /// <param name="key"></param> |
| 173 | + /// <param name="defaultValue"></param> |
| 174 | + /// <returns></returns> |
| 175 | + private static bool GetBoolPref(string key, bool defaultValue) |
| 176 | + { |
| 177 | + if (!EditorPrefs.HasKey(key)) |
| 178 | + { |
| 179 | + EditorPrefs.SetBool(key, defaultValue); |
| 180 | + } |
| 181 | + |
| 182 | + return EditorPrefs.GetBool(key); |
71 | 183 | }
|
| 184 | + } |
| 185 | +} |
72 | 186 |
|
73 |
| -#if UNITY_2018_3_OR_NEWER |
74 |
| - [SettingsProvider] |
75 |
| - private static SettingsProvider CreateProjectPreferenceSettingsProvider() |
76 |
| - { |
77 |
| - return new SettingsProvider(PROJECT_TITLE_PATH, SettingsScope.Project) |
78 |
| - { |
79 |
| - guiHandler = DrawProjectGUI, |
80 |
| - keywords = KEYWORDS |
81 |
| - }; |
82 |
| - } |
83 |
| - |
84 |
| - [SettingsProvider] |
85 |
| - private static SettingsProvider CreatePersonalPreferenceSettingsProvider() |
86 |
| - { |
87 |
| - return new SettingsProvider(PREFERENCES_TITLE_PATH, SettingsScope.User) |
88 |
| - { |
89 |
| - guiHandler = DrawPersonalPrefsGUI, |
90 |
| - keywords = KEYWORDS |
91 |
| - }; |
92 |
| - } |
93 |
| -#endif |
94 |
| - private static void DrawAllGUI() |
95 |
| - { |
96 |
| - DrawProjectGUI(); |
97 |
| - DrawPersonalPrefsGUI(); |
98 |
| - } |
99 |
| - |
100 |
| - private static void DrawProjectGUI(string value = "") |
101 |
| - { |
102 |
| - EditorGUILayout.LabelField(PROJECT_REFERENCES_HEADER, EditorStyles.boldLabel); |
103 |
| - |
104 |
| - var settings = SOArchitecture_Settings.Instance; |
105 |
| - |
106 |
| - GUI.changed = false; |
107 |
| - |
108 |
| - // Code Generation Target Directory |
109 |
| - EditorGUILayout.HelpBox(CODE_GEN_DIRECTORY_DESCRIPTION, MessageType.Info); |
110 |
| - using (new EditorGUILayout.HorizontalScope()) |
111 |
| - { |
112 |
| - EditorGUILayout.LabelField(new GUIContent(CODE_GEN_DIRECTORY_LABEL), MAX_WIDTH); |
113 |
| - var directory = EditorGUILayout.TextField(settings.CodeGenerationTargetDirectory); |
114 |
| - settings.CodeGenerationTargetDirectory = directory; |
115 |
| - } |
116 |
| - |
117 |
| - // Code Generation Allow Overwrite |
118 |
| - EditorGUILayout.HelpBox(ALLOW_OVERWRITE_DESCRIPTION, MessageType.Info); |
119 |
| - using (new EditorGUILayout.HorizontalScope()) |
120 |
| - { |
121 |
| - EditorGUILayout.LabelField(new GUIContent(ALLOW_OVERWRITE_LABEL), MAX_WIDTH); |
122 |
| - var newOverwrite = EditorGUILayout.Toggle(settings.CodeGenerationAllowOverwrite); |
123 |
| - settings.CodeGenerationAllowOverwrite = newOverwrite; |
124 |
| - } |
125 |
| - |
126 |
| - // Default Create Asset Menu Order |
127 |
| - EditorGUILayout.HelpBox(ASSET_MENU_ORDER_DESCRIPTION, MessageType.Info); |
128 |
| - using (new EditorGUILayout.HorizontalScope()) |
129 |
| - { |
130 |
| - EditorGUILayout.LabelField(ASSET_MENU_ORDER_LABEL, MAX_WIDTH); |
131 |
| - var newMenuOrder = EditorGUILayout.IntField(settings.DefaultCreateAssetMenuOrder); |
132 |
| - settings.DefaultCreateAssetMenuOrder = newMenuOrder; |
133 |
| - } |
134 |
| - |
135 |
| - if (GUI.changed) |
136 |
| - { |
137 |
| - EditorUtility.SetDirty(settings); |
138 |
| - } |
139 |
| - } |
140 |
| - |
141 |
| - private static void DrawPersonalPrefsGUI(string value = "") |
142 |
| - { |
143 |
| - EditorGUILayout.LabelField(USER_PREFERENCES_HEADER, EditorStyles.boldLabel); |
144 |
| - |
145 |
| - // Draw Event Gizmo |
146 |
| - var drawEventPref = GetBoolPref(DRAW_EVENT_GIZMOS_PREF, DRAW_EVENT_GIZMOS_DEFAULT); |
147 |
| - |
148 |
| - GUI.changed = false; |
149 |
| - drawEventPref = EditorGUILayout.Toggle("Draw Event Gizmo", drawEventPref); |
150 |
| - if (GUI.changed) |
151 |
| - { |
152 |
| - EditorPrefs.SetBool(DRAW_EVENT_GIZMOS_PREF, drawEventPref); |
153 |
| - } |
154 |
| - |
155 |
| - // Enable Debug |
156 |
| - EditorGUILayout.HelpBox("This will enable debug features for troubleshooting purposes such as " + |
157 |
| - "game events collecting stack traces. This will decrease performance " + |
158 |
| - "in-editor.", MessageType.Info); |
159 |
| - var enableDebugPref = GetBoolPref(ENABLE_DEBUG_PREF, ENABLE_DEBUG_DEFAULT); |
160 |
| - |
161 |
| - GUI.changed = false; |
162 |
| - enableDebugPref = EditorGUILayout.Toggle("Enable Debug", enableDebugPref); |
163 |
| - if (GUI.changed) |
164 |
| - { |
165 |
| - EditorPrefs.SetBool(ENABLE_DEBUG_PREF, enableDebugPref); |
166 |
| - } |
167 |
| - } |
168 |
| - |
169 |
| - /// <summary> |
170 |
| - /// Returns the current bool preference; if none exists, the default is set and returned. |
171 |
| - /// </summary> |
172 |
| - /// <param name="key"></param> |
173 |
| - /// <param name="defaultValue"></param> |
174 |
| - /// <returns></returns> |
175 |
| - private static bool GetBoolPref(string key, bool defaultValue) |
176 |
| - { |
177 |
| - if (!EditorPrefs.HasKey(key)) |
178 |
| - { |
179 |
| - EditorPrefs.SetBool(key, defaultValue); |
180 |
| - } |
181 |
| - |
182 |
| - return EditorPrefs.GetBool(key); |
183 |
| - } |
184 |
| - } |
185 |
| -} |
186 |
| - |
187 |
| -#endif |
| 187 | +#endif |
0 commit comments