当前位置:网站首页>ConfigManager JsonToObject
ConfigManager JsonToObject
2022-06-22 07:36:00 【Liam666】
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class ConfigManager : ManagerBase<ConfigManager>
{
private Dictionary<string, object> _cache = new Dictionary<string, object>();
private string _rootPath;
protected override void init()
{
base.init();
_rootPath = Path.Combine(ClientInfo.DataPath, "ConfigDatas");
}
public void readConfigs()
{
totalCount = 0;
this.scan(_rootPath);
}
public T load<T>() where T : class
{
string type = typeof(T).ToString();
if(_cache.ContainsKey(type))
{
T obj = _cache[type] as T;
return obj;
}
return null;
}
private int totalCount;
private void scan(string directory)
{
try
{
if (!Directory.Exists(directory))
{
Debug.LogError(" path does not exist :" + directory);
return;
}
string[] dircs = Directory.GetDirectories(directory);
for (int i = 0; i < dircs.Length; i++)
{
scan(dircs[i]);
}
string[] filePaths = Directory.GetFiles(directory);
for (int i = 0; i < filePaths.Length; i++)
{
string filePath = filePaths[i];
string extend = Path.GetExtension(filePath);
string name = Path.GetFileNameWithoutExtension(filePath);
if (!string.Equals(extend , ".json")) continue;
string structName = string.Format("{0}Data", name);
Type type = Type.GetType(structName);
if (type == null)
{
Debug.LogWarning(" No definition " + structName);
continue;
}
totalCount++;
#if UNITY_EDITOR
filePath = filePath.Replace("/", "\\");
#endif
filePath = string.Format("file://{0}", filePath);
FileTool.Instance.LoadFile(filePath, (string info) =>
{
//if (string.IsNullOrEmpty(info)) return;
try
{
object obj = JsonUtility.FromJson(info, type);
_cache[structName] = obj;
if (obj is IBaseData)
{
IBaseData bd = obj as IBaseData;
bd.makeDict();
}
totalCount--;
if (totalCount == 0)
readEnd();
}
catch (Exception e)
{
Console.WriteLine(e);
Debug.LogError(filePath);
throw;
}
});
}
}
catch (Exception ex)
{
Debug.LogError("fail,error:" + ex.Message);
}
}
public void add<T>(string json) where T : class
{
Type type = typeof(T);
string structName = type.ToString();
object obj = JsonUtility.FromJson(json, type);
_cache[structName] = obj;
if (obj is IBaseData)
{
IBaseData bd = obj as IBaseData;
bd.makeDict();
}
}
public void add<T>(T data) where T : class
{
Type type = typeof(T);
string structName = type.ToString();
_cache[structName] = data;
if (data is IBaseData)
{
IBaseData bd = data as IBaseData;
bd.makeDict();
}
}
private void readEnd()
{
//GameManager.Instance.EventManager.DispatchEvent(new GameEventArgs(WorldEventType.ReadConfigEnd));
//AssetBundleTool.instance.ReadyLoad();
}
}
边栏推荐
- Canoe learning notes (1) illustration of new project and channel configuration steps
- Idea cannot connect to sqlsms
- Canoe uses tricks to export data from logging file/trace to Matlab for analysis
- Open version - account information synchronization and unification
- Summary of methods for calculating the number of solutions of indefinite equations
- 报错 E: Unable to locate package sudo
- selenium防爬和模拟手机浏览器
- Get through - coupons
- 架构图颜色搭配
- Does it matter if you delete the pictures in the picture space after uploading to the store
猜你喜欢

目标检测系列——开山之作RCNN原理详解

Qualcomm platform LCM summary

Find and replace the spaces, Nan and special symbols contained in a column of data in the dataframe

Canoe uses tricks to export data from logging file/trace to Matlab for analysis

js实现随机生成16位的密钥——基础积累

由浅入深拿下OpenFeign

How to import and upload a CSV generated by a third-party platform to a Taobao store

What are the ways for Taobao merchants to put their babies on the shelves in batches

Multimedia architecture -- Introduction to display

How to cancel crmeb customer service link
随机推荐
Authority management of okcc call center
Summary of methods for calculating the number of solutions of indefinite equations
New year's Day mall material Icon
Qualcomm platform LCM summary
Multimedia architecture -- Introduction to display
JS array flattening (recursive writing)
Toyota bz4x canceled the launch conference. Even if the low-temperature charging problem does not exist, how about the product strength?
Crmeb mall order shipping function
微信小游戏(五)
Asyncstorage quick start
Canoe learning notes (9) sending module can Ig diagram
Coursera self driving car Part4 motion planning finalproject principle and key code analysis
Taobao assistant can not be used. How to export the baby in the warehouse to backup the data package
Flutter black triangle
antd——a-upload-dragger拖拽上传组件——基础积累
How to authorize a picture in a store to another store? What are the methods of unauthorized replication
ASP. Net core development experience
What if the finder fails to respond continuously? Finder unresponsive solution tutorial
Major enterprises are losing money one after another. Where will the environmental protection industry go?
A simple examination system based on C language