在游戏开发过程中,有一些简单的配置,不需要走表,使用一个文本文件去配置,更方便。

1. 配置文件格式如下,[Config.txt],文件名自己定义

1
2
3
4
5
6
7
8
9
10
11
12
13
GameFrame = 60

[Login]
LoginChannel = 1
TestUserName = JackLong
TestPassword = HahaFuckyou

[Net]
Timeout = 3.5

[Debug]
DebugMode = true
DebugMode2 = false

“[ ]” 中的部分,为模块名称,像 GameFrame 这样没有在某个模块下的配置,为全局配置

2. 如何使用

注意:在游戏开始时,首先要读取配置文件,路径自己定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string path = @"D:/Config.txt";
SimpleConfig.Load(path);

int gameFrame = SimpleConfig.GetIntValue("GameFrame");
int loginChannel = SimpleConfig.GetIntValue("Login", "LoginChannel");
string testUserName = SimpleConfig.GetStringValue("Login", "TestUserName");
string testPassword = SimpleConfig.GetStringValue("Login", "TestPassword");
float timeout = SimpleConfig.GetFloatValue("Net", "Timeout");
bool dm = SimpleConfig.GetBoolValue("Debug", "DebugMode");
bool dm2 = SimpleConfig.GetBoolValue("Debug", "DebugMode2");

Debug.LogFormat("GameFrame: {0}", gameFrame);
Debug.LogFormat("LoginChannel: {0}", loginChannel);
Debug.LogFormat("testUserName: {0}", testUserName);
Debug.LogFormat("testPassword: {0}", testPassword);
Debug.LogFormat("timeout: {0}", timeout);
Debug.LogFormat("dm: {0}, dm2: {1}", dm, dm2);

3. 源代码

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public static class SimpleConfig
{
private static Dictionary<string, Dictionary<string, string>> dataDict = new Dictionary<string, Dictionary<string, string>>();

public static void Load(string configFilePath)
{
var enumer = dataDict.GetEnumerator();
while (enumer.MoveNext())
{
enumer.Current.Value.Clear();
}
dataDict.Clear();

System.IO.StreamReader sr = new System.IO.StreamReader(configFilePath);
string line;
string currSection = null;
char[] splitor = new char[] { '=' };
while((line = sr.ReadLine()) != null)
{
line = line.Trim();
if (string.IsNullOrEmpty(line))
{
continue;
}

if(line.StartsWith("[") && line.EndsWith("]"))
{
currSection = line.Substring(1, line.Length - 2);
if (!dataDict.ContainsKey(currSection))
{
dataDict.Add(currSection, new Dictionary<string, string>());
}
}
else
{
string[] keyAndValue = line.Split(splitor, 2);
if(keyAndValue == null || keyAndValue.Length < 2)
{
continue;
}

if(currSection == null)
{
currSection = "Global";
if (!dataDict.ContainsKey(currSection))
{
dataDict.Add(currSection, new Dictionary<string, string>());
}
}

string key = keyAndValue[0].Trim();
string value = keyAndValue[1].Trim();

if (dataDict[currSection].ContainsKey(key))
{
Debug.LogErrorFormat("在 {0} 部分已经包含名为 {1} 的变量!", currSection, key);
continue;
}

dataDict[currSection].Add(key, value);
}
}
}

public static int GetIntValue(string variable)
{
return GetIntValue("Global", variable);
}

public static int GetIntValue(string section, string variable)
{
if (dataDict.ContainsKey(section) && dataDict[section].ContainsKey(variable))
{
string rawValue = dataDict[section][variable];
int value = 0;
if (int.TryParse(rawValue, out value))
{
return value;
}
}

Debug.LogErrorFormat("解析 {0} 部分的 {1} int 值出错", section, variable);
return -1;
}


public static float GetFloatValue(string variable)
{
return GetFloatValue("Global", variable);
}

public static float GetFloatValue(string section, string variable)
{
if (dataDict.ContainsKey(section) && dataDict[section].ContainsKey(variable))
{
string rawValue = dataDict[section][variable];
float value = 0;
if (float.TryParse(rawValue, out value))
{
return value;
}
}

Debug.LogErrorFormat("解析 {0} 部分的 {1} float 值出错", section, variable);
return -1;
}


public static string GetStringValue(string variable)
{
return GetStringValue("Global", variable);
}

public static string GetStringValue(string section, string variable)
{
if (dataDict.ContainsKey(section) && dataDict[section].ContainsKey(variable))
{
string rawValue = dataDict[section][variable];
return rawValue;
}

Debug.LogErrorFormat("解析 {0} 部分的 {1} string 值出错", section, variable);
return string.Empty;
}

public static bool GetBoolValue(string variable)
{
return GetBoolValue("Global", variable);
}

public static bool GetBoolValue(string section, string variable)
{
if (dataDict.ContainsKey(section) && dataDict[section].ContainsKey(variable))
{
string rawValue = dataDict[section][variable].ToLower();
bool value = false;
if (bool.TryParse(rawValue, out value))
{
return value;
}
}

Debug.LogErrorFormat("解析 {0} 部分的 {1} bool 值出错", section, variable);
return false;
}
}

https://github.com/Solleter/Unity3DSimpleConfig