Config manager. Just creating, reading and writing the config file for now.
This commit is contained in:
120
Assets/_PROJECT/Managers/ConfigManager.cs
Normal file
120
Assets/_PROJECT/Managers/ConfigManager.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
using FishNet.Component.Spawning;
|
||||
using SimpleJSON;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using UnityEngine;
|
||||
using Valve.Newtonsoft.Json;
|
||||
|
||||
public class ConfigManager : MonoBehaviour
|
||||
{
|
||||
public static ConfigManager instance = null;
|
||||
|
||||
[System.Serializable]
|
||||
public class Config
|
||||
{
|
||||
public bool isContinuousLocomotion;
|
||||
public float masterVolume;
|
||||
}
|
||||
protected Config currentConfig = new Config();
|
||||
protected string configFileName = "config.json";
|
||||
protected string configFilePath;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (!instance) { instance = this; }
|
||||
else if (instance != this) { Destroy(gameObject); }
|
||||
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
configFilePath = Directory.GetCurrentDirectory() + "\\" + configFileName;
|
||||
Debug.Log(configFilePath);
|
||||
currentConfig = LoadConfigFromFile();
|
||||
}
|
||||
|
||||
public Config GetConfig()
|
||||
{
|
||||
return currentConfig;
|
||||
}
|
||||
|
||||
|
||||
protected void SaveConfigToFile()
|
||||
{
|
||||
FileStream fileStream;
|
||||
if (!File.Exists(configFilePath))
|
||||
{
|
||||
fileStream = File.Create(configFilePath);
|
||||
|
||||
} else
|
||||
{
|
||||
fileStream = new FileStream(configFilePath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
|
||||
}
|
||||
StreamWriter writer = new StreamWriter(fileStream);
|
||||
writer.Write(JsonUtility.ToJson(currentConfig));
|
||||
writer.Close();
|
||||
fileStream.Close();
|
||||
}
|
||||
|
||||
|
||||
protected Config LoadConfigFromFile()
|
||||
{
|
||||
if (!File.Exists(configFilePath))
|
||||
{
|
||||
CreateDefaultConfigFile(); // File did not exist, try to create an empty file
|
||||
if (!File.Exists(configFilePath))
|
||||
{
|
||||
Debug.LogError("Config file was not found and could not be created: " + configFilePath);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
FileStream fileStream = new FileStream(configFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
StreamReader reader = new StreamReader(fileStream);
|
||||
Config config = JsonUtility.FromJson<Config>(reader.ReadToEnd());
|
||||
reader.Close();
|
||||
fileStream.Close();
|
||||
|
||||
//Config config = JsonUtility.FromJson<Config>(File.ReadAllText(configFilePath));
|
||||
|
||||
Debug.Log(config.isContinuousLocomotion);
|
||||
Debug.Log(config.masterVolume);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
protected void CreateDefaultConfigFile()
|
||||
{
|
||||
currentConfig = new Config();
|
||||
SaveConfigToFile();
|
||||
}
|
||||
|
||||
// Getters and Setters
|
||||
|
||||
public bool GetIsContinuousLocomotion()
|
||||
{
|
||||
|
||||
return currentConfig.isContinuousLocomotion;
|
||||
}
|
||||
public void SetIsContinuousLocomotion(bool isContinuousLocomotion)
|
||||
{
|
||||
currentConfig.isContinuousLocomotion = isContinuousLocomotion;
|
||||
SaveConfigToFile();
|
||||
}
|
||||
|
||||
|
||||
public float getMasterVolume()
|
||||
{
|
||||
|
||||
return currentConfig.masterVolume;
|
||||
}
|
||||
public void SetMasterVolume(float masterVolume)
|
||||
{
|
||||
currentConfig.masterVolume = masterVolume;
|
||||
SaveConfigToFile();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user