HKI Core
SettingsSystemEditor.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEngine.Audio;
3 using UnityEditor;
4 using System.Reflection;
5 using System.Collections.Generic;
6 using HKI.Core.Loc;
7 using HKI.Core.Variables;
9 using HKI.Core.Editor.Misc;
10 
11 namespace HKI.Core.Settings.Editor
12 {
16  [CustomEditor(typeof(SettingsSystem))]
17  public class SettingsSystemEditor : UnityEditor.Editor
18  {
19  // Private variables
20  const string warningsEnabledSPName = "WarningsEnabled";
21  const string useTargetFrameRateSPName = "UseTargetFrameRate";
22  static readonly string[] serializedPropertyNames = { "Resolution", "Fullscreen", "RefreshRate", "Quality", "FRAVSM", "AAM", "TextLanguage", "AudioLanguage", "MasterVolume", "AmbientVolume", "FxVolume", "UiFxVolume", "VoicesVolume" };
23  const string audioMixerSPName = "AudioMixer";
24  const string filenameSPName = "ConfigFilename";
25  const string editorPathSPName = "EditorPath";
26 
27  bool showReferences = false;
28 
29  SerializedProperty warningsEnabledSP = null;
30  SerializedProperty useTargetFrameRateSP = null;
31  List<SerializedProperty> serializedProperties = new List<SerializedProperty>();
32  SerializedProperty audioMixerSP = null;
33 
34  SerializedProperty filenameSP = null;
35  SerializedProperty editorPathSP = null;
36 
37  HKIVar_ContainerHeader header = null;
38  HKIVar_ContainerReorderableList containerRL = null;
39 
40  // Create function
41  [MenuItem("HKI/Core/Subsystems/Settings System")]
43  {
44  CreateSettingsSystem();
45  }
46 
47  public static void CreateSettingsSystem(string rootFolder = null)
48  {
49  // Find and select the settings system if it exists
50  if(HKI.Core.Editor.Misc.EditorHelper.FindAndSelectIfExists("Settings System"))
51  return;
52 
53  // Create a new setting system
54  string folder = null;
55 
56  if (!string.IsNullOrEmpty(rootFolder))
57  folder = rootFolder;
58 
59  if(string.IsNullOrEmpty(folder))
60  {
61  folder = EditorHelper.GetFolderFromUser("Select folder for settings system to be saved in", "The given path for the settings system is null or empty");
62  if (string.IsNullOrEmpty(folder))
63  return;
64  }
65 
66  EditorUtility.DisplayProgressBar("Create settings system", "Create settings system", 0.0f);
67 
68  SettingsSystem settingsSystem = EditorHelper.CreateAsset<SettingsSystem>("Settings System", folder);
69 
70  SerializedObject settingsSystemSO = new SerializedObject(settingsSystem);
71 
72  settingsSystemSO.Update();
73 
74  // Create all default settings
75  EditorUtility.DisplayProgressBar("Create settings system", "Create default settings for screen", 0.2f);
76 
77  IntWidthHeight resolution = new IntWidthHeight();
78  resolution.SetWidthAndHeight(1920, 1080);
79  ContainerEditor.AddHKIVarToContainer<IntegerWidthHeight>(settingsSystem, "Resolution", resolution);
80  ContainerEditor.AddHKIVarToContainer<Boolean>(settingsSystem, "Fullscreen", true);
81  ContainerEditor.AddHKIVarToContainer<Integer>(settingsSystem, "Refresh Rate", 60);
82  ContainerEditor.AddHKIVarToContainer<String>(settingsSystem, "Quality", "Normal");
83 
84  ContainerEditor.AddHKIVarToContainer<FrameRateAndVSyncMode>(settingsSystem, "FRAVSM", FrameRateAndVSyncModes.VsyncOnDefault);
85  ContainerEditor.AddHKIVarToContainer<AntiAliasingMode>(settingsSystem, "AAM", AntiAliasingModes.TAA);
86 
87  EditorUtility.DisplayProgressBar("Create settings system", "Create default settings for localization", 0.4f);
88 
89  ContainerEditor.AddHKIVarToContainer<Language>(settingsSystem, "Text Language", SystemLanguage.English);
90  ContainerEditor.AddHKIVarToContainer<Language>(settingsSystem, "Audio Language", SystemLanguage.English);
91 
92  EditorUtility.DisplayProgressBar("Create settings system", "Create default settings for volume", 0.6f);
93 
94  ContainerEditor.AddHKIVarToContainer<FloatMinMax>(settingsSystem, "Master Volume", 0.0f, -80.0f, 20.0f);
95  ContainerEditor.AddHKIVarToContainer<FloatMinMax>(settingsSystem, "Music Volume", 0.0f, -80.0f, 20.0f);
96  ContainerEditor.AddHKIVarToContainer<FloatMinMax>(settingsSystem, "Ambient Volume", 0.0f, -80.0f, 20.0f);
97  ContainerEditor.AddHKIVarToContainer<FloatMinMax>(settingsSystem, "Fx Volume", 0.0f, -80.0f, 20.0f);
98  ContainerEditor.AddHKIVarToContainer<FloatMinMax>(settingsSystem, "Ui Fx Volume", 0.0f, -80.0f, 20.0f);
99  ContainerEditor.AddHKIVarToContainer<FloatMinMax>(settingsSystem, "Voices Volume", 0.0f, -80.0f, 20.0f);
100 
101  settingsSystemSO.ApplyModifiedProperties();
102 
103  settingsSystemSO.Update();
104 
105  // Find and set audio mixer if it exists
106  EditorUtility.DisplayProgressBar("Create settings system", "Find audio mixer", 0.8f);
107 
108  SerializedProperty audioMixerSP = settingsSystemSO.FindProperty(audioMixerSPName);
109 
110  AudioMixer audioMixer = EditorHelper.FindAndLoad<AudioMixer>(".mixer");
111 
112  if (audioMixer != null)
113  audioMixerSP.objectReferenceValue = audioMixer;
114  else
115  Debug.LogError("Couldn't find a audio mixer! Please add it manually.");
116 
117  settingsSystemSO.FindProperty(editorPathSPName).stringValue = Application.dataPath + "/";
118 
119  settingsSystemSO.ApplyModifiedProperties();
120 
121  EditorUtility.DisplayProgressBar("Create settings system", "Get references to settings variables", 0.8f);
122 
123  MethodInfo getReferencesMethod = typeof(SettingsSystem).GetMethod("GetReferences", BindingFlags.NonPublic | BindingFlags.Instance);
124  getReferencesMethod.Invoke(settingsSystem, null);
125 
127 
128  settingsSystemSO.ApplyModifiedProperties();
129 
130  EditorUtility.DisplayProgressBar("Create settings system", "Done", 1.0f);
131 
132  Selection.activeObject = settingsSystem;
133 
134  EditorUtility.ClearProgressBar();
135  }
136 
137  // OnEnable function
138  void OnEnable()
139  {
140  for (int i = 0; i < serializedPropertyNames.Length; i++)
141  serializedProperties.Add(serializedObject.FindProperty(serializedPropertyNames[i]));
142 
143  warningsEnabledSP = serializedObject.FindProperty(warningsEnabledSPName);
144  useTargetFrameRateSP = serializedObject.FindProperty(useTargetFrameRateSPName);
145 
146  filenameSP = serializedObject.FindProperty(filenameSPName);
147  editorPathSP = serializedObject.FindProperty(editorPathSPName);
148 
149  audioMixerSP = serializedObject.FindProperty(audioMixerSPName);
150 
151  header = new HKIVar_ContainerHeader(serializedObject, target, true, 2);
152  containerRL = new HKIVar_ContainerReorderableList(serializedObject, Repaint, target);
153  }
154 
155  // OnInspectorGUI function
156  public override void OnInspectorGUI()
157  {
158  serializedObject.Update();
159 
160  EditorGUILayout.LabelField("References", EditorStyles.boldLabel);
161 
162  showReferences = EditorGUILayout.Foldout(showReferences, "References");
163  if(showReferences)
164  {
165  if(GUILayout.Button("Get References to standard settings."))
166  {
167  MethodInfo getReferencesMethod = typeof(SettingsSystem).GetMethod("GetReferences", BindingFlags.NonPublic | BindingFlags.Instance);
168  getReferencesMethod.Invoke(target, null);
169  }
170 
171  for(int i = 0; i < serializedProperties.Count; i++)
172  EditorGUILayout.PropertyField(serializedProperties[i]);
173 
174  EditorGUILayout.Space();
175 
176  EditorGUILayout.PropertyField(audioMixerSP);
177  }
178 
179  EditorGUILayout.Space();
180 
181  EditorGUILayout.LabelField("Settings system settings", EditorStyles.boldLabel);
182 
183  EditorGUILayout.PropertyField(warningsEnabledSP);
184  EditorGUILayout.PropertyField(useTargetFrameRateSP);
185 
186  EditorGUILayout.Space();
187 
188  EditorGUILayout.LabelField("Paths and filename", EditorStyles.boldLabel);
189 
190  EditorGUILayout.PropertyField(filenameSP);
191 
192  EditorGUILayout.BeginHorizontal();
193  EditorGUILayout.PropertyField(editorPathSP);
194  if(GUILayout.Button("Change", GUILayout.Width(60.0f)))
195  {
196  string newEditorPath = "";
197 
198  if(!newEditorPath.EndsWith("/") && !newEditorPath.EndsWith("\\"))
199  newEditorPath += "/";
200 
201  editorPathSP.stringValue = newEditorPath;
202  }
203  EditorGUILayout.EndHorizontal();
204 
205  GUI.enabled = false;
206  EditorGUILayout.TextField("Runtime Path", Application.persistentDataPath + "/");
207  GUI.enabled = true;
208 
209  EditorGUILayout.Space();
210 
211  header.DrawHeader();
212 
213  EditorGUILayout.Space();
214 
215  containerRL.DoLayoutList();
216 
217  serializedObject.ApplyModifiedProperties();
218  }
219  }
220 }
static void AddScriptableObjectToInitializer(ScriptableObject obj)
This function adds a scriptable object as a subasset to the initializer asset.
static bool FindAndSelectIfExists(string filter, string fileEnding=".asset")
Definition: EditorHelper.cs:13
Special Variable enables the use of the AntiAliasingModes data type as a variable of the settings sys...
This system loades, saves and stores settings. In addition to that it sets settings in the engine for...
This class containes the header code for the custom inspector of the Container.
This is a data type that combines a int width and a int height value into one value. This can be used for storing resolution information.
static void CreateSettingsSystem(string rootFolder=null)
This class containes helper functions for editor scripts.
Definition: EditorHelper.cs:10
This class extends the Unity ReorderableList with features so it can be used by HKIVar. HKIVars are longer than one line so the height of each ReorderableList element must be adjusted accordingly.
static string GetFolderFromUser(string panelText, string errorMsg)
Definition: EditorHelper.cs:32
Custom inspector for the Container class. The code is split into two subclasses for better readabilit...
AntiAliasingModes
All possible settings for anti aliasing.
Special Variable enables the use of the FrameRateAndVSyncModes data type as a variable of the setting...
Special Variable enables the use of the SystemLanguage data type as a variable of the settings system...
Definition: Language.cs:13
Implementation of a boolean value as a HKIVar via HKIVarGeneric.
Definition: Boolean.cs:16
This class contains the creation function of the menu for all HKI Core systems.
Custom inspector for the SettingsSystem class.
Implementation of a integer value as a HKIVar via HKIVarGeneric.
Definition: Integer.cs:16
static Object FindAndLoad(string objectType, string fileEnding=".asset")
Definition: EditorHelper.cs:91
Implementation of a string value as a HKIVar via HKIVarGeneric.
Definition: String.cs:16
FrameRateAndVSyncModes
All possible settings for frame rate and VSync.
Implementation of a IntegerWidthHeight value as a HKIVar via HKIVarGeneric.
Implementation of a min max float value as a HKIVar via HKIVarGenericMinMax.
Definition: FloatMinMax.cs:16
void SetWidthAndHeight(int width, int height)