当前位置:网站首页>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"]
]
}
}
边栏推荐
- SATA protocol OOB essay
- Eternal Blue ms17-010exp reappears
- JVM方法调用
- Attack and defense world novice zone PWN
- Those gods on Zhihu reply
- OpenGL learning (III) glut two-dimensional image rendering
- 8. = = and = = =?
- Understand dynamic calculation diagram, requires_ grad、zero_ grad
- ETL开发工具Kettle下载安装环境搭建及使用教程
- Serial vector format (SVF) file format
猜你喜欢
随机推荐
Generate publickey with der format public key and report an error
Nftscan and port3 have reached strategic cooperation in the field of NFT data
使用 tftp 无法向服务器上传文件问题解决
理解corners_align,两种看待像素的视角
Ionic4 learning notes 7 -- UI component 1 (no practice, direct excerpt)
2022暑期杭电多校第一场1012Alice and Bob(博弈论)
Ionic4 learning notes 9 -- an east project 01
Nacos introduction and console service installation
Ionic4 learning notes 5-- custom public module
Go Xiaobai implements a simple go mock server
网络安全80端口—-PHP CGI参数注入执行漏洞
National vocational college skills competition network security competition -- detailed explanation of Apache security configuration
2020年中职组“网络空间安全”赛项浙江省竞赛任务书及答案(Flag)
matplotlib
FPGA 20 routines: 9. DDR3 memory particle initialization write and read through RS232 (Part 2)
Sqoop
C Programming classic tutorial
Cadence OrCAD capture tcl/tk script example
The difference between static method and instance method
【Tkinter】常用组件(二)









