5 using System.Collections.Generic;
10 namespace HKI.Core.Settings
36 bool isEditor =
false;
37 bool changeFunctionCallbacksLinked =
false;
39 string configFilePath =
"";
41 [SerializeField]
string ConfigFilename =
"config.config";
42 [SerializeField]
string EditorPath =
"";
43 string runtimePath =
"";
45 [SerializeField]
bool WarningsEnabled =
true;
47 [SerializeField]
bool UseTargetFrameRate =
true;
50 [SerializeField]
Boolean Fullscreen = null;
51 [SerializeField]
Integer RefreshRate = null;
52 [SerializeField]
String Quality = null;
56 [SerializeField]
Language AudioLanguage = null;
64 [SerializeField] AudioMixer AudioMixer = null;
72 isEditor = Application.isEditor;
74 runtimePath = Application.persistentDataPath +
"/";
77 configFilePath = EditorPath + ConfigFilename;
79 configFilePath = runtimePath + ConfigFilename;
81 if(isEditor && !Application.isPlaying)
84 LinkStandardSettingsToOnChangeFunctions();
87 ActivateAllSettings();
98 if(changeFunctionCallbacksLinked)
99 UnLinkStandardSettingsFromOnChangeFunctions();
108 XmlDocument document =
new XmlDocument();
110 XmlNode rootNode = document.CreateElement(
"Config");
111 document.AppendChild(rootNode);
113 document.Save(configFilePath);
122 Resolution = GetVar<IntegerWidthHeight>(
"Resolution");
123 Fullscreen = GetVar<Boolean>(
"Fullscreen");
124 RefreshRate = GetVar<Integer>(
"Refresh Rate");
125 Quality = GetVar<String>(
"Quality");
126 FRAVSM = GetVar<FrameRateAndVSyncMode>(
"FRAVSM");
127 AAM = GetVar<AntiAliasingMode>(
"AAM");
128 TextLanguage = GetVar<Language>(
"Text Language");
129 AudioLanguage = GetVar<Language>(
"Audio Language");
130 MasterVolume = GetVar<FloatMinMax>(
"Master Volume");
131 MusicVolume = GetVar<FloatMinMax>(
"Music Volume");
132 AmbientVolume = GetVar<FloatMinMax>(
"Ambient Volume");
133 FxVolume = GetVar<FloatMinMax>(
"Fx Volume");
134 UiFxVolume = GetVar<FloatMinMax>(
"Ui Fx Volume");
135 VoicesVolume = GetVar<FloatMinMax>(
"Voices Volume");
144 T GetVar<T>(
string variableName) where T :
HKIVar 146 HKIVar variable = GetHKIVarByName(variableName);
150 Debug.LogError(
"Couldn't find a >" + variableName +
"< of type >" + typeof(T).Name +
"< variable as child of the settings system!");
166 if(!File.Exists(configFilePath))
170 XmlDocument document =
new XmlDocument();
172 document.Load(configFilePath);
173 XmlNode configNode = document.SelectSingleNode(
"//Config/" + name.Replace(
" ",
""));
174 if(configNode == null)
176 Debug.LogError(
"Config file is corupted!");
182 ActivateAllSettings();
193 ActivateAllSettings();
203 ChangeFrameRateAndVSyncMode();
213 if(Resolution != null)
216 Resolution.OnValueChanged += () => { LogInfoForNotSetableInEditor(Resolution); };
218 Resolution.OnValueChanged += ChangeResolution;
220 else if(WarningsEnabled)
221 Debug.LogWarning(
"There is no linked >Resolution< variable of type >IntegerWidthHeight< in this settings system!");
224 if(Fullscreen != null)
227 Fullscreen.OnValueChanged += () => { LogInfoForNotSetableInEditor(Fullscreen); };
229 Fullscreen.OnValueChanged += ChangeResolution;
231 else if(WarningsEnabled)
232 Debug.LogError(
"There is no linked >Fullscreen< variable of type >Boolean< in this settings system!");
235 if(UseTargetFrameRate && RefreshRate != null)
238 RefreshRate.OnValueChanged += () => { LogInfoForNotSetableInEditor(RefreshRate); };
240 RefreshRate.OnValueChanged += ChangeResolution;
242 else if(UseTargetFrameRate && WarningsEnabled)
243 Debug.LogError(
"There is no linked >RefreshRate< variable of type >Integer< in this settings system!");
247 Quality.OnValueChanged += ChangeQuality;
248 else if(WarningsEnabled)
249 Debug.LogError(
"There is no linked >Quality< variable of type >String< in this settings system!");
253 FRAVSM.OnValueChanged += ChangeFrameRateAndVSyncMode;
254 else if(WarningsEnabled)
255 Debug.LogError(
"There is no linked >FRAVSM< variable of type >FrameRateAndVSyncMode< in this settings system!");
261 if (MasterVolume != null)
262 MasterVolume.OnValueChanged += ChangeAudio;
263 else if(WarningsEnabled)
264 Debug.LogError(
"There is no linked >MasterVolume< variable of type >FloatMinMax< in this settings system!");
267 if(MusicVolume != null)
268 MusicVolume.OnValueChanged += ChangeAudio;
269 else if(WarningsEnabled)
270 Debug.LogError(
"There is no linked >MusicVolume< variable of type >FloatMinMax< in this settings system!");
273 if(AmbientVolume != null)
274 AmbientVolume.OnValueChanged += ChangeAudio;
275 else if(WarningsEnabled)
276 Debug.LogError(
"There is no linked >AmbientVolume< variable of type >FloatMinMax< in this settings system!");
280 FxVolume.OnValueChanged += ChangeAudio;
281 else if(WarningsEnabled)
282 Debug.LogError(
"There is no linked >FxVolume< variable of type >FloatMinMax< in this settings system!");
285 if(UiFxVolume != null)
286 UiFxVolume.OnValueChanged += ChangeAudio;
287 else if(WarningsEnabled)
288 Debug.LogError(
"There is no linked >UiFxVolume< variable of type >FloatMinMax< in this settings system!");
291 if(VoicesVolume != null)
292 VoicesVolume.OnValueChanged += ChangeAudio;
293 else if(WarningsEnabled)
294 Debug.LogError(
"There is no linked >VoicesVolume< variable of type >FloatMinMax< in this settings system!");
296 changeFunctionCallbacksLinked =
true;
305 if(Resolution != null)
308 Resolution.OnValueChanged -= () => { LogInfoForNotSetableInEditor(Resolution); };
310 Resolution.OnValueChanged -= ChangeResolution;
314 if(Fullscreen != null)
317 Fullscreen.OnValueChanged -= () => { LogInfoForNotSetableInEditor(Fullscreen); };
319 Fullscreen.OnValueChanged -= ChangeResolution;
323 if(UseTargetFrameRate && RefreshRate != null)
326 RefreshRate.OnValueChanged -= () => { LogInfoForNotSetableInEditor(RefreshRate); };
328 RefreshRate.OnValueChanged -= ChangeResolution;
333 Quality.OnValueChanged -= ChangeQuality;
337 FRAVSM.OnValueChanged -= ChangeFrameRateAndVSyncMode;
343 if (MasterVolume != null)
344 MasterVolume.OnValueChanged -= ChangeAudio;
347 if(MusicVolume != null)
348 MusicVolume.OnValueChanged -= ChangeAudio;
351 if (AmbientVolume != null)
352 AmbientVolume.OnValueChanged -= ChangeAudio;
356 FxVolume.OnValueChanged -= ChangeAudio;
359 if(UiFxVolume != null)
360 UiFxVolume.OnValueChanged -= ChangeAudio;
363 if(VoicesVolume != null)
364 VoicesVolume.OnValueChanged -= ChangeAudio;
366 changeFunctionCallbacksLinked =
false;
371 Debug.Log(
"Can't set this setting (" + variable.name +
") in the editor (only in a runtime build): " + variable.ToString());
380 if(Resolution == null || Fullscreen == null || isEditor)
383 if(UseTargetFrameRate && RefreshRate != null)
384 Screen.SetResolution(Resolution.
Value.Width, Resolution.
Value.Height, Fullscreen.
Value, RefreshRate.
Value);
386 Screen.SetResolution(Resolution.
Value.Width, Resolution.
Value.Height, Fullscreen.
Value);
394 int qualityIndex = 0;
396 string[] qualityNames = QualitySettings.names;
397 for(
int i = 0; i < qualityNames.Length; i++)
399 if(qualityNames[i] == Quality.
Value)
406 QualitySettings.SetQualityLevel(qualityIndex);
439 ChangeFrameRateAndVSyncMode(-1, 1);
449 Application.targetFrameRate = fr;
450 QualitySettings.vSyncCount = vsync;
458 if(AudioMixer == null)
461 if (MasterVolume != null)
462 AudioMixer.SetFloat(
"SetMasterVolume", MasterVolume.Value);
464 if(MusicVolume != null)
465 AudioMixer.SetFloat(
"SetMusicVolume", MusicVolume.Value);
467 if (AmbientVolume != null)
468 AudioMixer.SetFloat(
"SetAmbientVolume", AmbientVolume.Value);
471 AudioMixer.SetFloat(
"SetFxVolume", FxVolume.Value);
473 if(UiFxVolume != null)
474 AudioMixer.SetFloat(
"SetUiFxVolume", UiFxVolume.Value);
476 if(VoicesVolume != null)
477 AudioMixer.SetFloat(
"SetVoicesVolume", VoicesVolume.Value);
This interface allows the initialization of anything by the Initialzer.
void ChangeResolution()
This function will set the resolution.
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...
void CreateConfigFile()
This function will create the config file. This could be the case on the first time the game starts a...
void ChangeFrameRateAndVSyncMode()
This function will analyse the FrameRateAndVSyncMode value and set the frame rate and vsync according...
bool Load()
This function loads the saved settings from a XML file and tries to activate them.
void ActivateAllSettings()
This function activates all settings.
void Save()
Saves all settings (custom settings that are given to this system too) into a XML file...
This abstract class of a ScriptableObject is provides child classes with on value changed callback ca...
Special Variable enables the use of the FrameRateAndVSyncModes data type as a variable of the setting...
void GetReferences()
This helper function gets all references to Unity standard settings. For a list look into the class d...
This container is itself a HKIVar but it purpose is to hold other HKIVars together at one place...
void ChangeFrameRateAndVSyncMode(int fr, int vsync)
This function will set the frame rate and vsync.
Special Variable enables the use of the SystemLanguage data type as a variable of the settings system...
Implementation of a boolean value as a HKIVar via HKIVarGeneric.
void ChangeAudio()
This function will set all audio volume settings.
void LogInfoForNotSetableInEditor(HKIVar variable)
void LinkStandardSettingsToOnChangeFunctions()
This function links the change functions to the standard settings variables.
Implementation of a integer value as a HKIVar via HKIVarGeneric.
void UnLinkStandardSettingsFromOnChangeFunctions()
This will unlink the change functions from standard settings variables.
void Init()
The init function will load and tries to activate all settings. Some settings can't be set in the edi...
Implementation of a string value as a HKIVar via HKIVarGeneric.
void ChangeQuality()
This function will set the quality.
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.