GameConfig.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using Sirenix.OdinInspector;
  7. using KairoEngine.Core;
  8. using KairoEngine.Core.ConfigOptions;
  9. using UniRx;
  10. namespace KairoEngine.Core.ModuleSystem
  11. {
  12. [CreateAssetMenu(fileName = "GameConfig", menuName = "KairoEngine/GameConfig"), HideMonoScript]
  13. public class GameConfig : ScriptableObject, ISerializationCallbackReceiver
  14. {
  15. [ListDrawerSettings(DraggableItems = false, HideRemoveButton = true, ShowPaging = false)]
  16. [ShowInInspector, NonSerialized, OnValueChanged("StartModule"), OnInspectorInit("StartModule")]
  17. public List<GameModule> modules = new List<GameModule>();
  18. [InlineProperty, HideLabel] public ConfigOptionsManager configOptions = new ConfigOptionsManager();
  19. private void StartModule()
  20. {
  21. foreach (var module in modules)
  22. {
  23. if(module.isInitialized == false)
  24. {
  25. module.isInitialized = true;
  26. module.gameConfig = this;
  27. }
  28. }
  29. }
  30. [OnInspectorInit("GetModuleNames"), OnValueChanged("AddNewModule")]
  31. [ValueDropdown("possibleModules", IsUniqueList = false)]
  32. [LabelText("Add New Module")]
  33. [NonSerialized]
  34. public GameModule newModule;
  35. [NonSerialized] private IEnumerable possibleModules = new ValueDropdownList<GameModule>();
  36. private void GetModuleNames()
  37. {
  38. possibleModules = ReflectiveEnumerator.GetEnumerableOfType<GameModule>(this)
  39. .Where(x => x.typeName != "GameModule")
  40. .Where(x => x.typeName != "GameModuleBase")
  41. .Select(x => new ValueDropdownItem(x.name, x));
  42. }
  43. private void AddNewModule()
  44. {
  45. for (int i = 0; i < modules.Count; i++)
  46. {
  47. if(modules[i].name == newModule.name)
  48. {
  49. newModule = null;
  50. return;
  51. }
  52. }
  53. if(newModule != null)
  54. {
  55. modules.Add(newModule);
  56. newModule = null;
  57. }
  58. }
  59. [SerializeField, HideInInspector] private List<string> serializedModules = new List<string>();
  60. [SerializeField, HideInInspector] private ObjectSerializer serializer = new ObjectSerializer();
  61. public void OnBeforeSerialize()
  62. {
  63. if(modules == null) return;
  64. serializedModules = new List<string>();
  65. serializer.Clear();
  66. for (int i = 0; i < modules.Count; i++)
  67. {
  68. modules[i].OnBeforeSerialize(serializer);
  69. string data = JsonUtility.ToJson(modules[i]);
  70. serializedModules.Add(data);
  71. }
  72. }
  73. public void OnAfterDeserialize()
  74. {
  75. if(serializedModules == null) return;
  76. if(modules == null) modules = new List<GameModule>();
  77. if(modules.Count > 0) modules.Clear();
  78. for (int i = 0; i < serializedModules.Count; i++)
  79. {
  80. GameModule module = JsonUtility.FromJson<GameModuleBase>(serializedModules[i]);
  81. module = GameModule.InvokeStringMethod(module.className, module.typeName, serializedModules[i]);
  82. module.gameConfig = this;
  83. module.OnBeforeDeserialize(serializer);
  84. modules.Add(module);
  85. }
  86. serializer.Clear();
  87. }
  88. }
  89. }