using System.Collections; using System; using UnityEngine; using WindowsInput.Native; using TMPro; using UnityEngine.UI; using System.IO; using Newtonsoft.Json; using System.Collections.Generic; public class ConfigManager : MonoBehaviour { public static Config config; private static bool hasInitialized = false; public static event Action onConfigChanged; private static float saverTimer = 0; private static bool isSavingConfig = false; private float saverDelay = 1.5f; public List Tabs; [SerializeField] public static List ConfigPanelComponents; void Awake() { onConfigChanged += EnsureInitialization; onConfigChanged += SaveFile; ConfigPanelComponents = GetConfigPanelComponents(); EnsureInitialization(); } void Start() { AddListenerToWidget(ConfigPanelComponents); onConfigChanged?.Invoke(); } public static void EnsureInitialization() { if (hasInitialized) return; LoadFile(); UpdateConfigPanelFromConfig(ref ConfigPanelComponents); hasInitialized = true; } private static void LoadFile() { Debug.Log("Loading config file"); if (File.Exists(GetFileName())) { Debug.Log("Config file exists"); config = JsonConvert.DeserializeObject(File.ReadAllText(GetFileName())); } else { Debug.Log("Config file does not exist"); config = new Config(); SaveFile(); Debug.Log("Config file created"); } } public static string GetFileName() { return Application.dataPath + "/../config.json"; } public static void SaveFile() { isSavingConfig = true; saverTimer = 0; //Debug.Log("Saving config file"); } public void saveFileWait() { File.WriteAllText(GetFileName(), JsonConvert.SerializeObject(config, Formatting.Indented)); Debug.Log("Config file saved"); } void Update() { if (isSavingConfig) { saverTimer += Time.deltaTime; if (saverTimer >= saverDelay) { isSavingConfig = false; saverTimer = 0; saveFileWait(); } } } public static GameObject GetConfigPanelWidget(string configKeyName) { foreach (var item in ConfigPanelComponents) { if (item.ConfigKeyName == configKeyName) { return item.Widget; } } return null; } private List GetConfigPanelComponents() { var _configPanelComponents = new List(); foreach (var tab in Tabs) { var _configPanelComponentsInTab = tab.GetComponentsInChildren(); _configPanelComponents.AddRange(_configPanelComponentsInTab); } return _configPanelComponents; } private void AddListenerToWidget(List _configPanelComponents) { foreach (var configPanelComponent in _configPanelComponents) { var widget = configPanelComponent.Widget; if (widget.GetComponent() != null) { var dropdown = widget.GetComponent(); // add listener to dropdown to update config by key name dropdown.onValueChanged.AddListener(delegate { var field = config.GetType().GetField(configPanelComponent.ConfigKeyName); field.SetValue(config, dropdown.value); onConfigChanged?.Invoke(); }); } else if (widget.GetComponent() != null) { var toggle = widget.GetComponent(); toggle.onValueChanged.AddListener(delegate { var field = config.GetType().GetField(configPanelComponent.ConfigKeyName); field.SetValue(config, toggle.isOn); onConfigChanged?.Invoke(); }); } else if (widget.GetComponent() != null) { var slider = widget.GetComponent(); slider.onValueChanged.AddListener(delegate { var field = config.GetType().GetField(configPanelComponent.ConfigKeyName); field.SetValue(config, slider.value); onConfigChanged?.Invoke(); }); } else if (widget.GetComponent() != null) { var valueManager = widget.GetComponent(); valueManager.onValueChanged.AddListener(delegate { var field = config.GetType().GetField(configPanelComponent.ConfigKeyName); field.SetValue(config, valueManager.Value); onConfigChanged?.Invoke(); }); } } } private static void UpdateConfigPanelFromConfig(ref List _configPanelComponents) { foreach (var configPanelComponent in _configPanelComponents) { Debug.Log(configPanelComponent.ConfigKeyName); var componentObject = configPanelComponent.Widget; if (componentObject.GetComponent() != null) { var dropdown = componentObject.GetComponent(); var field = config.GetType().GetField(configPanelComponent.ConfigKeyName); dropdown.value = (int)field.GetValue(config); dropdown.onValueChanged?.Invoke(dropdown.value); } else if (componentObject.GetComponent() != null) { var toggle = componentObject.GetComponent(); var field = config.GetType().GetField(configPanelComponent.ConfigKeyName); toggle.isOn = (bool)field.GetValue(config); toggle.onValueChanged?.Invoke(toggle.isOn); } else if (componentObject.GetComponent() != null) { var slider = componentObject.GetComponent(); var field = config.GetType().GetField(configPanelComponent.ConfigKeyName); slider.value = (float)field.GetValue(config); slider.onValueChanged?.Invoke(slider.value); } else if (componentObject.GetComponent() != null) { var valueManager = componentObject.GetComponent(); var field = config.GetType().GetField(configPanelComponent.ConfigKeyName); valueManager.Value = (float)field.GetValue(config); valueManager.onValueChanged?.Invoke(); } } } }