GlobalConfigGO.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. [ExecuteInEditMode]
  4. public class GlobalConfigGO:MonoBehaviour {
  5. // Serializable properties
  6. [SerializeField]
  7. private string[] entryNames;
  8. [SerializeField]
  9. private int[] entryTypes;
  10. [SerializeField]
  11. private string[] entryValuesString;
  12. [SerializeField]
  13. private bool[] entryValuesBool;
  14. // Properties
  15. private List<ConfigEntry> entries = new List<ConfigEntry>();
  16. private bool hasStarted;
  17. private static GlobalConfigGO instance;
  18. // ================================================================================================================
  19. // MAIN EVENT INTERFACE -------------------------------------------------------------------------------------------
  20. void Awake() {
  21. instance = this;
  22. entries = new List<ConfigEntry>();
  23. hasStarted = true;
  24. load();
  25. }
  26. void Update() {
  27. if (Application.isEditor && !Application.isPlaying) {
  28. load();
  29. }
  30. }
  31. // ================================================================================================================
  32. // PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
  33. public static GlobalConfigGO getInstance() {
  34. return instance;
  35. }
  36. public void addEntry(ConfigEntry entry) {
  37. entries.Add(entry);
  38. }
  39. public ConfigEntry getEntryAt(int index) {
  40. return entries[index];
  41. }
  42. public int getNumEntries() {
  43. return entries.Count;
  44. }
  45. public void load() {
  46. //Debug.Log("loading serialized data, it has " + entryNames.Length + " entries");
  47. // Load from serialized properties
  48. entries.Clear();
  49. ConfigEntry entry;
  50. for (int i = 0; i < entryNames.Length; i++) {
  51. entry = new ConfigEntry();
  52. entry.name = entryNames[i];
  53. entry.type = (ConfigEntry.Types)entryTypes[i];
  54. entry.valueString = entryValuesString[i];
  55. entry.valueBool = entryValuesBool[i];
  56. entries.Add(entry);
  57. }
  58. }
  59. public void save() {
  60. // Save to serialized properties
  61. if (hasStarted) {
  62. entryNames = new string[entries.Count];
  63. entryTypes = new int[entries.Count];
  64. entryValuesString = new string[entries.Count];
  65. entryValuesBool = new bool[entries.Count];
  66. for (int i = 0; i < entries.Count; i++) {
  67. entryNames[i] = entries[i].name;
  68. entryTypes[i] = (int)entries[i].type;
  69. entryValuesString[i] = entries[i].valueString;
  70. entryValuesBool[i] = entries[i].valueBool;
  71. }
  72. //Debug.Log("saving all serialized data, it has " + entries.Count + " entries; " + entryNames.Length + " recorded");
  73. }
  74. }
  75. public string getString(string key, string defaultValue = "") {
  76. foreach (var entry in entries) {
  77. if (entry.name == key) return entry.valueString;
  78. }
  79. return defaultValue;
  80. }
  81. public bool getBool(string key, bool defaultValue = false) {
  82. foreach (var entry in entries) {
  83. if (entry.name == key) return entry.valueBool;
  84. }
  85. return defaultValue;
  86. }
  87. public float getFloat(string key, float defaultValue = 0.0f) {
  88. foreach (var entry in entries) {
  89. if (entry.name == key) return float.Parse(entry.valueString);
  90. }
  91. return defaultValue;
  92. }
  93. public int getInteger(string key, int defaultValue = 0) {
  94. foreach (var entry in entries) {
  95. if (entry.name == key) return int.Parse(entry.valueString);
  96. }
  97. return defaultValue;
  98. }
  99. // ================================================================================================================
  100. // INTERNAL INTERFACE ---------------------------------------------------------------------------------------------
  101. // ================================================================================================================
  102. // AUXILIARY CLASSES
  103. public class ConfigEntry {
  104. public enum Types : int {
  105. String = 0,
  106. Int = 1,
  107. Float = 2,
  108. Boolean = 3
  109. }
  110. public string name = "";
  111. public Types type = Types.String;
  112. public string valueString = "";
  113. public bool valueBool = false;
  114. public ConfigEntry() {
  115. }
  116. }
  117. }