当前位置:网站首页>Unity框架之ConfigManager【Json配置文件读写】
Unity框架之ConfigManager【Json配置文件读写】
2022-07-24 18:53:00 【零一与黑白】
Unity框架之ConfigManager【Json配置文件读写】
读取配置信息
int id = 0;
ConfigManager.cheatConfig.GetCheats("TriggerJackPot" , id);
配置文件管理类:ConfigManager.cs
public static class ConfigManager
{
public static CheatConfig cheatConfig{
get; pricate set;}
public static void Initialize(){
UpdateConfig();
LoadAllConfig();
}
/// <summary>
/// 更新配置文件
/// <summary>
private static void UpdateConfig(){
JsonHelper.SaveJson(new CheatConfig());
}
/// <summary>
/// 加载所有的配置文件
/// <summary>
private static void UpdateConfig(){
cheatConfig = JsonHelper.ReadJson<CheatConfig>();
}
}
读写配置文件:JsonHelper.cs
public class CheatConfig
{
private static string PATH = Application.streamingAssetsPath + "/Config/";
/// <summary>
/// 保存Json
/// <summary>
public static void SaveJson<T>(T value) where T : class
{
string filename = string.Format{
"{0}{1}.json", PATH, typeof(T).Name};
if(!File.Exists(filename))
Directory.CreateDirectory(PATH);
else
File.Delete(filename);
using(StreamWriter sw = File.CreateText(filename))
{
sw.Write(JsonConvert.SerializeObject(value));
}
}
/// <summary>
/// 读取Json
/// <summary>
public static T ReadJson<T>(T value) where T : class
{
string filename = string.Format{
"{0}{1}.json", PATH, typeof(T).Name};
if(!File.Exists(filename))
{
Debug.logError("Json文件丢失");
return null;
}
string text = File.ReadAllText(filename);
T obj = JsonConvert.DeserializeObject<T>(text);
return obj;
}
}
配置文件对应的类:CheatConfig.cs
public class CheatConfig
{
public Dictionary<string, string[][]> Cheat = new Dictionary<string, string[][]>{
{
"TiggerFreeGame", new String[][]{
new String[]{
"N", "Scatter", "T"},
new String[]{
"A", "PIC02", "PIC03"},
new String[]{
"PIC05", "Scatter", "A"},
new String[]{
"Q", "J", "PIC04"},
new String[]{
"K", "Scatter", "N"},
},
},
{
"BonusCoinFullGame", new String[][]{
new String[]{
"Coin"},
new String[]{
"Coin"},
new String[]{
"Coin"},
new String[]{
"Coin"},
new String[]{
"Coin"},
new String[]{
"Coin"},
new String[]{
"Coin"},
new String[]{
"Coin"},
new String[]{
"Coin"},
new String[]{
"Coin"},
new String[]{
"Coin"},
new String[]{
"Coin"},
new String[]{
"Coin"},
new String[]{
"Coin"},
new String[]{
"Coin"}
}
},
};
/// <summary>
/// 读取配置信息
/// <summary>
/// <param name="name">名字</param>
/// <param name="id">第几行</param>
/// <returns>返回一行中的所有数据</returns>
public string[] GetCheats(string name, int id){
if(Cheats.ContainKey(name)){
return Cheats[name][id];
}
else{
Debug.logErrorFormat("不存在{0}作弊表");
return default;
}
}
}
JSON配置文件:CheatConfig.json
{
"Cheats":{
"TriggerJackPot":[
["N", "Scatter", "T"],
["A", "Wild", "PIC03"],
["PIC05", "Scatter", "A"],
["Q", "Wild", "PIC04"],
["K", "Scatter", "N"]
],
"BonusCoinFullGame":[
["Coin"],
["Coin"],
["Coin"],
["Coin"],
["Coin"],
["Coin"],
["Coin"],
["Coin"],
["Coin"],
["Coin"],
["Coin"],
["Coin"],
["Coin"],
["Coin"],
["Coin"]
]
}
}
边栏推荐
- matplotlib
- matplotlib
- Analysis of dropout principle in deep learning
- epoch,batch_ size
- Analysis of several possible causes of 0xc0000005 memory violation
- Vsftpd2.3.4 port penetration 6200 IRC_ 3281_ backdoor
- Ionic4 learning notes 7 -- UI component 1 (no practice, direct excerpt)
- [wechat applet development] custom tabbar case (custom message 99 + little hearts)
- Leetcode memory deep search / dynamic planning V2
- Mysql database, subquery, union, limit
猜你喜欢

Web

FPGA 20 routines: 9. DDR3 memory particle initialization write and read through RS232 (Part 1)

Type-C PD protocol chip while charging and listening

C Programming classic tutorial
![[today in history] July 24: caldera v. Microsoft; Amd announced its acquisition of ATI; Google launches chromecast](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 24: caldera v. Microsoft; Amd announced its acquisition of ATI; Google launches chromecast

Ionic4 learning notes 9 -- an east project 01

Attack and defense world novice zone PWN

Analysis of dropout principle in deep learning

【微信小程序开发】自定义tabBar案例(定制消息99+小红心)

National vocational college skills competition network security competition -- detailed explanation of Apache security configuration
随机推荐
EasyUI adds row level buttons to the DataGrid
Tcl/tk file operation
【微信小程序开发】自定义tabBar案例(定制消息99+小红心)
Excel practice notes 1
OPENGL学习(四)GLUT三维图像绘制
Nacos introduction and console service installation
Mysqlworkbench performance analysis tool -- Performance dashboard
2022杭电多校第二场1009 ShuanQ(数学)
Type-C边充边听PD协议芯片
Redis data type
Vsftpd2.3.4-端口渗透 6200 irc_3281_backdoor
Mysql数据库,去重,连接篇
Understand dynamic calculation diagram, requires_ grad、zero_ grad
[Tkinter] common components (II)
MySQL - bufferpool related information
2022 Hangdian multi school second session 1009 shuangq (Mathematics)
matplotlib
Understand corners_ Align, two perspectives for viewing pixels
8. = = and = = =?
32-bit stack overflow advanced