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();
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Assets/_PROJECT/Managers/ConfigManager.cs.meta
Normal file
11
Assets/_PROJECT/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:
|
||||||
46
Assets/_PROJECT/Managers/ConfigManager.prefab
Normal file
46
Assets/_PROJECT/Managers/ConfigManager.prefab
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
%YAML 1.1
|
||||||
|
%TAG !u! tag:unity3d.com,2011:
|
||||||
|
--- !u!1 &8320216767994874586
|
||||||
|
GameObject:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
serializedVersion: 6
|
||||||
|
m_Component:
|
||||||
|
- component: {fileID: 7021351325020055800}
|
||||||
|
- component: {fileID: 4493448285314379156}
|
||||||
|
m_Layer: 0
|
||||||
|
m_Name: ConfigManager
|
||||||
|
m_TagString: Untagged
|
||||||
|
m_Icon: {fileID: 0}
|
||||||
|
m_NavMeshLayer: 0
|
||||||
|
m_StaticEditorFlags: 0
|
||||||
|
m_IsActive: 1
|
||||||
|
--- !u!4 &7021351325020055800
|
||||||
|
Transform:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 8320216767994874586}
|
||||||
|
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||||
|
m_LocalPosition: {x: -74.23521, y: 4.9800367, z: -13.716739}
|
||||||
|
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||||
|
m_ConstrainProportionsScale: 0
|
||||||
|
m_Children: []
|
||||||
|
m_Father: {fileID: 0}
|
||||||
|
m_RootOrder: 38
|
||||||
|
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||||
|
--- !u!114 &4493448285314379156
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 8320216767994874586}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: c7622588c230b4b448152a6dfd6c0588, type: 3}
|
||||||
|
m_Name:
|
||||||
|
m_EditorClassIdentifier:
|
||||||
7
Assets/_PROJECT/Managers/ConfigManager.prefab.meta
Normal file
7
Assets/_PROJECT/Managers/ConfigManager.prefab.meta
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 5d19b02089b931a469209b09142786a0
|
||||||
|
PrefabImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Binary file not shown.
Reference in New Issue
Block a user