forked from cgvr/DeltaVR
Moved the ConfigManager script to the Scripts folder and ConfigManager prefab to the Prefabs folder.
This commit is contained in:
197
Assets/_PROJECT/Scripts/Managers/ConfigManager.cs
Normal file
197
Assets/_PROJECT/Scripts/Managers/ConfigManager.cs
Normal file
@@ -0,0 +1,197 @@
|
||||
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();
|
||||
|
||||
} 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();
|
||||
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));
|
||||
|
||||
/*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();*/
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
11
Assets/_PROJECT/Scripts/Managers/ConfigManager.cs.meta
Normal file
11
Assets/_PROJECT/Scripts/Managers/ConfigManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c7622588c230b4b448152a6dfd6c0588
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user