SettingsManager.cs
2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using UnityEngine;
using System.Collections;
using MiniJSON;
using System.IO;
/// <summary>
/// Simple script to retrieve settings for app. Stor settings JSON file in StreamingAssets, named settings.json
/// </summary>
public class SettingsManager
{
private static IDictionary _cmsDict = null;
public static IDictionary cmsDict
{
get
{
if (_cmsDict == null)
{
string path = Application.streamingAssetsPath + "/settings.json";
if (!File.Exists(path))
{
Debug.LogError("SettingsManager: No settings file found :(");
return null;
}
_cmsDict = (IDictionary) Json.Deserialize(File.ReadAllText(path));
}
return _cmsDict;
}
}
public static string GetString(string key)
{
string val = string.Empty;
if (cmsDict != null && cmsDict.Contains(key))
val = cmsDict[key].ToString();
else
Debug.LogError(string.Format("SettingsManager: \"{0}\" does not exist in settings file", key));
return val;
}
public static int GetInt(string key)
{
int val = 0;
if (cmsDict != null && cmsDict.Contains(key))
System.Int32.TryParse(cmsDict[key].ToString(), out val);
else
Debug.LogError(string.Format("SettingsManager: \"{0}\" does not exist in settings file", key));
return val;
}
public static float GetFloat(string key)
{
float val = 0;
if (cmsDict != null && cmsDict.Contains(key))
float.TryParse(cmsDict[key].ToString(), out val);
else
Debug.LogError(string.Format("SettingsManager: \"{0}\" does not exist in settings file", key));
return val;
}
public static bool GetBool(string key)
{
bool val = false;
if (cmsDict != null && cmsDict.Contains(key))
val = cmsDict[key].ToString().ToLower() == "true" ? true : false;
else
Debug.LogError(string.Format("SettingsManager: \"{0}\" does not exist in settings file", key));
return val;
}
}