抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

简介

自动监听 Json 配置文件夹,目前实现一件生成配置类,有空的话再增加配置查看和修改的功能。

使用说明

工具说明

按照图片红框所示的步骤即可一件生成配置类文件。
本工具监听配置文件夹,只要有变化自动就会改变面板显示的内容。

工具代码放在 Unity 项目的 Editor 文件夹下。

配置类说明

配置类会保存到配置类输出文件夹的 Bean 目录下。

在输出文件夹根目录中存在一个 ConfigsPathConfig 文件,该文件包含所有配置的名字和配置文件夹的名字。

例:

配置文件说明

配置表属性名后面添加 | 可以分割属性名和类型。

  • 如果我们需要一个本配置类的类型,则我们在对应的配置项就置空。

    例:

  • 如果我们需要一个特定的枚举类型或者类的类型,则可以用 | 来表示我们要生成的类型,即 PropName|EnumType,这样就能生成 EnumType prop

    例:

  • 如果该类型是 int 类型的列表,但是第一项中没有内容,我们只要把属性名写成 PropName|int 即可生成 List<int>

    例:

  • 如果该类型是本数据类,则我们不用写类型,但是要在配置项的位置写一个空列表 [] ,这样生成的时候就会生成 List<ClassName>

    例:

  • 如果要生成一个字典,则我们可以再加一个 | 来表示键值对类型,即写成 PropName|type|type,并且在配置项写一个空对象 {},这样就能生成 Dictionary<type,type>。目前支持的 key 类型为 string 和 int。

    例:

  • 如果要生成一个对象数组,只需要在数组中写入对象键值对,即可生成对象辅助类。本类型不支持自定义数组和字典类型,只能按照输入的内容创建基本数据类型的数组和字典。

    例:

配置管理器说明

使用 ConfigManager.Ins().GetConfig<T>(string folder, string name) 来获取对应的配置。

使用 ConfigManager.Ins().Clear() 来清除缓存。

配置路径修改

修改文件 ConfigEditor 中的 _jsonUrl 属性即可修改读取 json 文件的文件夹路径。

修改文件 ConfigEditor 中的 _outputUrl 属性即可修改写入配置类的路径。

注意

  • 配置文件夹下不能创建和配置文件夹同名的文件夹。
  • 配置文件夹只能存在最多二级文件夹,即最大路径为 /Configs/Folder/Config.json

代码

ConfigEditor
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using LitJson;

public class ConfigEditor : EditorWindow
{
/// <summary>
/// 配置文件路径
/// </summary>
private string _jsonUrl;
/// <summary>
/// 配置类输出路径
/// </summary>
private string _outputUrl;

private Dictionary<string, List<FileInfo>> _allConfigs = new Dictionary<string, List<FileInfo>>();

private int _configsNum = 0;

private bool _foldConfigs = true;

private Vector2 _scrollRoot;

ConfigEditor()
{
titleContent = new GUIContent("配置编辑器");
}

[MenuItem("PreUtils/EditorEditor")]
static public void ShowEditor()
{
// Debug.Log("启动配置编辑器");
EditorWindow.GetWindow(typeof(ConfigEditor));
}

private void OnEnable()
{
_jsonUrl = Application.dataPath + "/Configs/";
_outputUrl = Application.dataPath + "/Script/Loader/Config/";
//获取所有配置
GetAllConfigs();
//监听配置文件夹变化
Debug.Log("启动文件夹监听");
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.IncludeSubdirectories = true;
watcher.Path = _jsonUrl;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "*.json";
FileSystemEventHandler changeHandle = new FileSystemEventHandler(OnJsonFileChanged);
watcher.Changed += changeHandle;
//watcher.Deleted += changeHandle;
watcher.Created += changeHandle;
watcher.EnableRaisingEvents = true;
watcher.InternalBufferSize = 10240;
}

public void OnGUI()
{
GUILayout.Space(10);

//获取所有配置
_foldConfigs = EditorGUILayout.BeginFoldoutHeaderGroup(_foldConfigs, "所有配置");

if (_foldConfigs)
{
GUILayout.Label("总配置数量:" + _configsNum);

GUILayout.BeginHorizontal();
GUILayout.Space(10);
_scrollRoot = EditorGUILayout.BeginScrollView(_scrollRoot, GUILayout.Height(400), GUILayout.Width(position.width - 20));

GUIStyle configButtonStyle = new GUIStyle(GUI.skin.button);
configButtonStyle.fixedHeight = 22;
configButtonStyle.fontSize = 16;
configButtonStyle.alignment = TextAnchor.MiddleLeft;

GUIStyle configLabelStyle = new GUIStyle(GUI.skin.label);
configLabelStyle.fontSize = 18;
configLabelStyle.alignment = TextAnchor.MiddleLeft;
configLabelStyle.fixedHeight = 20;

foreach (var fileInfo in _allConfigs)
{
EditorGUILayout.LabelField(fileInfo.Key + "【" + fileInfo.Value.Count + "个配置文件】:", configLabelStyle);
GUILayout.Space(5);
for (int i = 0, len = fileInfo.Value.Count; i < len; i++)
{
FileInfo file = fileInfo.Value[i];
//EditorGUILayout.LabelField("测试");
GUIContent name = new GUIContent((i + 1) + "." + file.Name.Replace(".json", ""));
if (GUILayout.Button(name, configButtonStyle))
{
//EditorWindow.GetWindow(typeof(ConfigDetailEditor));
}
GUILayout.Space(2);
}
GUILayout.Space(10);
}

EditorGUILayout.EndScrollView();
GUILayout.EndHorizontal();
}
EditorGUILayout.EndFoldoutHeaderGroup();

GUILayout.Space(10);

if (GUILayout.Button("一键生成所有配置文件数据类"))
{
GenerateDataClass();
GenerateConfigPath();
}
}

/// <summary>
/// 创建数据类
/// </summary>
private void GenerateDataClass()
{
//生成基类
string ns = "Bean";
string baseClass = "namespace " + ns + "\r\n{\r\n" +
"\tpublic class ConfigBaseData\r\n" +
"\t{\r\n" +
"\t\tpublic int id;\r\n" +
"\t}\r\n}";
string output = _outputUrl + "ConfigBaseData.cs";
GeneratorUtils.WriteFile(output, baseClass);

//创建所有数据类
foreach (var fileInfo in _allConfigs)
{
//生成数据类
for (int i = 0, len = fileInfo.Value.Count; i < len; i++)
{
FileInfo file = fileInfo.Value[i];
string content = GeneratorUtils.ReadFile(file.FullName);
JsonData jsonData = JsonMapper.ToObject(content);
GenerateClass(GeneratorUtils.UpperCaseFirstChar(file.Name.Split('.')[0]) + "Data", jsonData, fileInfo.Key);
}
}
}

/// <summary>
/// 创建类
/// </summary>
/// <param name="clsName"></param>
/// <param name="json"></param>
/// <param name="folder"></param>
private void GenerateClass(string clsName, JsonData json, string folder)
{
List<string> listClass = new List<string>();

List<string> listProps = new List<string>();

string ns = "Bean";
//创建命名空间
listClass.Add("using System;\r\n" +
"using System.Collections.Generic;\r\n" +
"namespace " + ns + "{\r\n");

//创建类头
listClass.Add("\tpublic class " + clsName +
": ConfigBaseData, ICloneable{\r\n");

//创建构造函数
string strConstructor = "\t\tpublic " + clsName + "(){\r\n";

//遍历属性
IDictionary props = json[0];
foreach (var key in props.Keys)
{
Dictionary<string, bool> dicClass = new Dictionary<string, bool>();
string propName = key.ToString();
if (propName == "id") continue;

JsonData child = json[0][propName];
object type = child == null ? JsonType.None : child.GetJsonType();

if (type.Equals(JsonType.None))
{
listClass.Add("\t\tpublic " + clsName + " " + propName + ";\r\n");
}
else if (type.Equals(JsonType.Array))
{
//属性为数组
if (child.Count == 0)
{
//最终类型非对象,该属性为纯数组
string strList = "";
string strListEnd = "";

strList += "List<";
strListEnd += ">";

string[] propSplit = propName.Split('|');
string strType;

if (propSplit.Length > 1)
{
//strList += propSplit[1] + strListEnd;
strType = propSplit[1];
}
else
{
strType = clsName;
}
strList += strType + strListEnd;

Debug.Log("list数组 ==== " + strList);

//cls += "\t\tpublic " + strList + " " + propName + ";\r\n";
listClass.Add("\t\tpublic " + strList + " " + propSplit[0] + ";\r\n");

string strConstructorChild = "\t\t\t" + propSplit[0] + " = new List<" + strType + ">();\r\n";
strConstructor += strConstructorChild;
}
else
{
//获取最终类型
JsonData tempData = child[0];
JsonType tempType = tempData.GetJsonType();
int loop = 1;
while (tempData.GetJsonType().Equals(JsonType.Array))
{
tempData = tempData[0];
tempType = tempData.GetJsonType();
loop++;
}

if (tempType.Equals(JsonType.Object))
{
//最终类型为对象
//创建附属类
string strListClassName = GeneratorUtils.UpperCaseFirstChar(propName) + "Data";
if (!dicClass.ContainsKey(strListClassName))
{
GeneratorUtils.GenerateSubClass(child, strListClassName, listClass, dicClass);
dicClass.Add(strListClassName, true);
}

//创建附属类列表
string strList = "";
string strListEnd = "";
for (int i = 0; i < loop; i++)
{
strList += "List<";
strListEnd += ">";
if (i == loop - 1)
{
strList += strListClassName + strListEnd;
}
}

//strListClass += "\t\tpublic " + strList + " " + propName + ";\r\n";
listClass.Add("\t\tpublic " + strList + " " + propName + ";\r\n");

string strConstructorChild = "\t\t\t" + propName + " = new List<" + strListClassName + ">();\r\n";
strConstructor += strConstructorChild;
}
else
{
//最终类型非对象,该属性为纯数组
string strList = "";
string strListEnd = "";
for (int i = 0; i < loop; i++)
{
strList += "List<";
strListEnd += ">";
if (i == loop - 1)
{
strList += GeneratorUtils.GetType(tempType) + strListEnd;
}
}
Debug.Log("list数组 ==== " + strList);

//cls += "\t\tpublic " + strList + " " + propName + ";\r\n";
listClass.Add("\t\tpublic " + strList + " " + propName + ";\r\n");

string strConstructorChild = "\t\t\t" + propName + " = new List<" + GeneratorUtils.GetType(tempType) + ">();\r\n";
strConstructor += strConstructorChild;
}
}
}
else if (type.Equals(JsonType.Object))
{
//属性为对象
string[] propSplits = propName.Split("|");
if (propSplits.Length > 1)
{
listClass.Add("\t\tpublic Dictionary<" + propSplits[1] + "," + propSplits[2] + "> " + propSplits[0] + ";\r\n");

string strConstructorChild = "\t\t\t" + propSplits[0] + " = new Dictionary<" + propSplits[1] + "," + propSplits[2] + ">();\r\n";
strConstructor += strConstructorChild;
}
else
{
//创建附属类
string strListClassName = GeneratorUtils.UpperCaseFirstChar(propName) + "Data";
if (!dicClass.ContainsKey(strListClassName))
{
GeneratorUtils.GenerateSubClass(child, strListClassName, listClass, dicClass);
dicClass.Add(strListClassName, true);
}
listClass.Add("\t\tpublic " + strListClassName + " " + propName + ";\r\n");
}
}
else
{
//属性为基本类型
//Debug.Log("属性名" + propName + "," + type + "," + json[0][propName]);
//cls += "\t\tpublic " + GetType(type) + " " + propName + ";\r\n";

if (type == null)
{
listClass.Add("\t\tpublic " + clsName + " " + propName + ";\r\n");
}
else
{
string[] propSplit = propName.Split('|');
if (propSplit.Length > 1)
{
listClass.Add("\t\tpublic " + propSplit[1] + " " + propSplit[0] + ";\r\n");
}
else
{
listClass.Add("\t\tpublic " + GeneratorUtils.GetType((JsonType)type) + " " + propName + ";\r\n");
}
}
}

listProps.Add(propName);
}

strConstructor += "\t\t}\r\n";

listClass.Add(strConstructor);

string strGenerator = "";
strGenerator += "\t\tpublic " + clsName + "(" + clsName + " obj){\r\n";
for (int i = 0, len = listProps.Count; i < len; i++)
{
string[] propSplit = listProps[i].Split('|');
if (propSplit.Length > 1)
{
strGenerator += "\t\t\t" + propSplit[0] + " = obj." + propSplit[0] + ";\r\n";
}
else
{
strGenerator += "\t\t\t" + listProps[i] + " = obj." + listProps[i] + ";\r\n";
}
}
strGenerator += "\t\t}\r\n";
listClass.Add(strGenerator);

//创建克隆函数
listClass.Add("\t\tpublic object Clone()\r\n" +
"\t\t{\r\n" +
"\t\t\treturn new " + clsName + "(this);\r\n" +
"\t\t}\r\n");

//补充文件结构
listClass.Add("\t}" +
"\r\n}");

//拼装数据类内容
string cls = "";
for (int i = 0, len = listClass.Count; i < len; i++)
{
cls += listClass[i];
}

//保存数据类
Debug.Log("生成类" + cls);
string strFolder;
if (folder == "Configs")
{
strFolder = "";
}
else
{
strFolder = folder + "/";
}
string output = _outputUrl + "Bean/" + strFolder + clsName + ".cs";
GeneratorUtils.WriteFile(output, cls);
}

private void GenerateConfigPath()
{
//string ns = "Bean";
string cls = "namespace Bean{\r\n" +
"\tpublic class ConfigsFolderConfig\r\n" +
"\t{\r\n";

string folders = "\t\tpublic const string Null = null;\r\n";
string names = "";
//配置名字典,防止重名配置重复添加
Dictionary<string, bool> dicNames = new Dictionary<string, bool>();
//创建所有数据类
foreach (var fileInfo in _allConfigs)
{
if (fileInfo.Key != "Configs")
{
folders += "\t\tpublic const string " + fileInfo.Key + " = \"" + fileInfo.Key + "\";\r\n";
}
//生成数据类
for (int i = 0, len = fileInfo.Value.Count; i < len; i++)
{
FileInfo file = fileInfo.Value[i];
string fileName = file.Name.Replace(".json", "");
if (!dicNames.ContainsKey(fileName))
{
names += "\t\tpublic const string " + GeneratorUtils.UpperCaseFirstChar(fileName) + " = \"" + fileName + "\";\r\n";
dicNames.Add(fileName, true);
}
}
}

cls += folders;

cls += "\t}\r\n\r\n" +
"\tpublic class ConfigsNameConfig\r\n" +
"\t{\r\n";

cls += names;

cls += "\t}\r\n" +
"}";

Debug.Log("生成配置表路径配置\n" + cls);
GeneratorUtils.WriteFile(_outputUrl + "ConfigsPathConfig.cs", cls);
}

//-----自动函数-----

/// <summary>
/// 配置文件发生变化后的响应事件
/// </summary>
/// <param name="obj"></param>
/// <param name="args"></param>
private void OnJsonFileChanged(object obj, FileSystemEventArgs args)
{
Debug.Log("配置文件发生变化,重载配置文件");
_allConfigs.Clear();
GetAllConfigs();
}

/// <summary>
/// 获取所有配置
/// </summary>
private void GetAllConfigs()
{
Debug.Log("获取所有配置1:" + _jsonUrl);
DirectoryInfo directoryInfo = new DirectoryInfo(_jsonUrl);
FileInfo[] files = directoryInfo.GetFiles("*", SearchOption.AllDirectories);
_configsNum = 0;

for (int i = 0, len = files.Length; i < len; i++)
{
FileInfo file = files[i];
if (file.Name.EndsWith(".meta")) continue;

string[] folders = file.DirectoryName.Split('\\');
string parent = folders[folders.Length - 1];
//_allConfigs.Add(file);
if (!_allConfigs.ContainsKey(parent))
{
_allConfigs.Add(parent, new List<FileInfo>());
}
_allConfigs[parent].Add(file);
//ConsoleUtils.Log("添加路径" , (file.DirectoryName + "/"), _jsonUrl);
_configsNum++;
}
}
}

GeneratorUtils
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
using System.Text;

public class GeneratorUtils
{
/// <summary>
/// 生成辅助类
/// </summary>
/// <param name="json"></param>
/// <param name="className"></param>
/// <param name="list"></param>
/// <param name="dic"></param>
/// <returns></returns>
public static string GenerateSubClass(JsonData json, string className, List<string> list, Dictionary<string, bool> dic)
{
string strListClass = "\t\tpublic class " + className + " {\r\n";
Debug.Log("json数据 ===== " + json.ToJson());
IDictionary classProps = json.GetJsonType().Equals(JsonType.Array) ? json[0] : json;
foreach (var classKey in classProps.Keys)
{
string propName = classKey.ToString();
//JsonData child = json[0][propName];
//strListClass += "\t\tpublic " + GetType(type) + " " + propName + ";\r\n";
JsonData child = (JsonData)classProps[classKey];
JsonType type = child.GetJsonType();
if (type.Equals(JsonType.Array))
{
string[] propSplit = propName.Split('|');
//获取最终类型
JsonData tempData = child[0];
JsonType tempType = tempData.GetJsonType();
int loop = 1;
while (tempData.GetJsonType().Equals(JsonType.Array))
{
tempData = tempData[0];
tempType = tempData.GetJsonType();
loop++;
}

if (tempType.Equals(JsonType.Object))
{
string strListClassName = GeneratorUtils.UpperCaseFirstChar(propName) + "Data";
if (!dic.ContainsKey(strListClassName))
{
GenerateSubClass(child, strListClassName, list, dic);
dic.Add(strListClassName, true);
}


//创建附属类列表
string strList = "";
string strListEnd = "";
for (int i = 0; i < loop; i++)
{
strList += "List<";
strListEnd += ">";
if (i == loop - 1)
{
strList += strListClassName + strListEnd;
}
}

strListClass += "\t\t\tpublic " + strList + " " + propName + ";\r\n";
}
else
{
//非对象,该属性为纯数组
string strList = "";
string strListEnd = "";
for (int i = 0; i < loop; i++)
{
strList += "List<";
strListEnd += ">";
if (i == loop - 1)
{
strList += GetType(tempType) + strListEnd;
}
}

strListClass += "\t\t\tpublic " + strList + " " + propName + ";\r\n";
}

}
else if (type.Equals(JsonType.Object))
{
string strListClassName = GeneratorUtils.UpperCaseFirstChar(propName) + "Data";
if (!dic.ContainsKey(strListClassName))
{
GenerateSubClass(child, strListClassName, list, dic);
}
strListClass += "\t\t\tpublic " + strListClassName + " " + propName + ";\r\n";
}
else
{
string[] propSplit = propName.Split('|');
Debug.Log("分离:" + propSplit.Length);
if (propSplit.Length > 1)
{
strListClass += "\t\t\tpublic " + propSplit[1] + " " + propSplit[0] + ";\r\n";
}
else
{
strListClass += "\t\t\tpublic " + GetType(type) + " " + propName + ";\r\n";
}
}
}

strListClass += "\t\t}\r\n";
list.Insert(2, strListClass);

return strListClass;
}

public static string GetType(JsonType name)
{
switch (name)
{
case JsonType.Int:
return "int";
case JsonType.Boolean:
return "bool";
case JsonType.String:
return "string";
case JsonType.Double:
return "double";
case JsonType.Long:
return "long";
}
return "";
}

/// <summary>
/// 首字母大写
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string UpperCaseFirstChar(string s)
{
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
char[] a = s.ToCharArray();
a[0] = char.ToUpper(a[0]);
return new string(a);
}

/// <summary>
/// 创建文件
/// </summary>
/// <param name="path"></param>
/// <param name="name"></param>
/// <param name="info"></param>
public static void WriteFile(string path, string content)
{
//if (File.Exists(path)) {
//}
string folderPath = Path.GetDirectoryName(path);
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
File.WriteAllText(path, content, Encoding.Default);
}

/// <summary>
/// 读取文件
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string ReadFile(string path)
{
path = path.Replace("/", "\\");
return File.ReadAllText(path);
}
}

FileUtils
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
using System.IO;
using System.Text;

public class FileUtils
{
// Start is called before the first frame update
/// <summary>
/// 创建文件
/// </summary>
/// <param name="path"></param>
/// <param name="name"></param>
/// <param name="info"></param>
public static void WriteFile(string path, string content)
{
//if (File.Exists(path)) {
//}
string folderPath = Path.GetDirectoryName(path);
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
File.WriteAllText(path, content, Encoding.Default);
}

/// <summary>
/// 读取文件
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string ReadFile(string path)
{
path = path.Replace("/", "\\");
return File.ReadAllText(path);
}
}

ConfigManager
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
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System.Linq;
using LitJson;
using Bean;
using System.Text.RegularExpressions;

public class ConfigManager:Singleton<ConfigManager>
{
private Dictionary<string, object> _configs = new Dictionary<string, object>();
private Dictionary<string, Dictionary<string, FileInfo>> _fileInfo;

/// <summary>
/// 读取配置
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="folder">配置分类</param>
/// <param name="name">配置名</param>
/// <returns>配置字典</returns>
public Dictionary<int, T> GetConfig<T>(string folder, string name) where T : ConfigBaseData
{
object obj = null;
string path = folder + "_" + name;
_configs.TryGetValue(path, out obj);
if (obj != null)
{
Debug.Log("读取配置缓存【" + name + "】");
return (Dictionary<int, T>)obj;
}
else
{
string config = FileUtils.ReadFile(Application.dataPath + "/Configs/" + (folder != null ? folder + "/" : "") + name + ".json");

config = Regex.Replace(config, "(\\|).*(?=\")", "");

Debug.Log("配置读取路径 - " + Application.dataPath + "/Configs/" + (folder != null ? folder + "/" : "") + name + ".json");
Debug.Log("读取内容 = " + config);

if (!string.IsNullOrEmpty(config))
{
T[] json = JsonMapper.ToObject<T[]>(config);
obj = json.ToDictionary(key => key.id, value => value);
_configs.Add(path, obj);
return (Dictionary<int, T>)obj;
}
else
{
Debug.LogWarning("不存在配置【" + name + "】");
return null;
}
}
}

/// <summary>
/// 清除目标缓存
/// </summary>
/// <param name="folder"></param>
/// <param name="name"></param>
public void Clear(string folder, string name)
{
string path = folder + "_" + name;
if (_configs.ContainsKey(path))
{
_configs.Remove(path);
}
}

/// <summary>
/// 清除所有缓存
/// </summary>
public void ClearAll()
{
_configs.Clear();
}
}

更新日志

2023-12-13

  1. 更新配置读取管理器。
  2. 修改 LitJson 序列化使其支持 int 类型作为字典的 key。

2023-12-09

  1. 更新基础版本。

评论