forked from cgvr/DeltaVR
186 lines
4.4 KiB
C#
186 lines
4.4 KiB
C#
using FishNet.Component.Spawning;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using UnityEngine;
|
|
using Valve.Newtonsoft.Json;
|
|
using static ConfigManager;
|
|
|
|
public class ConfigManager : MonoBehaviour
|
|
{
|
|
public static ConfigManager instance = null;
|
|
|
|
[System.Serializable]
|
|
public class Config
|
|
{
|
|
public bool isContinuousLocomotion = false;
|
|
public float continuousLocomotionSpeed = 3.0f;
|
|
public float volumeMaster = 0.5f;
|
|
public float volumeAmbient = 0.5f;
|
|
public float volumeMusic = 0.5f;
|
|
public float volumeSFX = 0.5f;
|
|
public float volumeUI = 0.5f;
|
|
public float volumeVO = 0.5f;
|
|
|
|
}
|
|
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);
|
|
fileStream.Close();
|
|
|
|
}
|
|
|
|
string json = JsonUtility.ToJson(currentConfig, true);
|
|
File.WriteAllText(configFilePath, json);
|
|
}
|
|
|
|
|
|
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 currentConfig;
|
|
}
|
|
}
|
|
Config config = JsonUtility.FromJson<Config>(File.ReadAllText(configFilePath));
|
|
|
|
return config;
|
|
}
|
|
|
|
protected void CreateDefaultConfigFile()
|
|
{
|
|
currentConfig = new Config();
|
|
SaveConfigToFile();
|
|
}
|
|
|
|
public void RestoreDefaultConfig()
|
|
{
|
|
currentConfig = new Config();
|
|
SaveConfigToFile();
|
|
}
|
|
|
|
|
|
// Getters and Setters
|
|
|
|
public bool GetIsContinuousLocomotion()
|
|
{
|
|
|
|
return currentConfig.isContinuousLocomotion;
|
|
}
|
|
public void SetIsContinuousLocomotion(bool isContinuousLocomotion)
|
|
{
|
|
currentConfig.isContinuousLocomotion = isContinuousLocomotion;
|
|
SaveConfigToFile();
|
|
}
|
|
|
|
public float GetContinuousLocomotionSpeed()
|
|
{
|
|
|
|
return currentConfig.continuousLocomotionSpeed;
|
|
}
|
|
public void SetContinuousLocomotionSpeed(float continuousLocomotionSpeed)
|
|
{
|
|
currentConfig.continuousLocomotionSpeed = continuousLocomotionSpeed;
|
|
SaveConfigToFile();
|
|
}
|
|
|
|
public float GetVolumeMaster()
|
|
{
|
|
|
|
return currentConfig.volumeMaster;
|
|
}
|
|
public void SetVolumeMaster(float masterVolume)
|
|
{
|
|
currentConfig.volumeMaster = masterVolume;
|
|
SaveConfigToFile();
|
|
}
|
|
|
|
public float GetVolumeAmbient()
|
|
{
|
|
|
|
return currentConfig.volumeAmbient;
|
|
}
|
|
public void SetVolumeAmbient(float ambientVolume)
|
|
{
|
|
currentConfig.volumeAmbient = ambientVolume;
|
|
SaveConfigToFile();
|
|
}
|
|
|
|
public float GetVolumeMusic()
|
|
{
|
|
|
|
return currentConfig.volumeMusic;
|
|
}
|
|
public void SetVolumeMusic(float musicVolume)
|
|
{
|
|
currentConfig.volumeMusic = musicVolume;
|
|
SaveConfigToFile();
|
|
}
|
|
|
|
public float GetVolumeSFX()
|
|
{
|
|
|
|
return currentConfig.volumeSFX;
|
|
}
|
|
public void SetVolumeSFX(float sfxVolume)
|
|
{
|
|
currentConfig.volumeSFX = sfxVolume;
|
|
SaveConfigToFile();
|
|
}
|
|
|
|
public float GetVolumeUI()
|
|
{
|
|
|
|
return currentConfig.volumeUI;
|
|
}
|
|
public void SetVolumeUI(float uiVolume)
|
|
{
|
|
currentConfig.volumeUI = uiVolume;
|
|
SaveConfigToFile();
|
|
}
|
|
|
|
public float GetVolumeVO()
|
|
{
|
|
|
|
return currentConfig.volumeVO;
|
|
}
|
|
public void SetVolumeVO(float voVolume)
|
|
{
|
|
currentConfig.volumeVO = voVolume;
|
|
SaveConfigToFile();
|
|
}
|
|
}
|