ShanxiKnowledgeBase/SXElectricityInformationAcq.../Assets/Scripts/DataConfigManager.cs

119 lines
3.3 KiB
C#

using System.Collections.Generic;
using System.IO;
using MotionFramework;
using Newtonsoft.Json.Linq;
using UnityEngine;
namespace DefaultNamespace
{
public class DataConfigManager : ModuleSingleton<DataConfigManager>, IModule
{
private Dictionary<string, List<string>> toolsPackDict;
public void OnCreate(object createParam)
{
LoadConfig();
}
public void OnUpdate()
{
}
public void OnDestroy()
{
}
public void OnGUI()
{
}
public List<string> GetToolsPackData(string toolsName)
{
foreach (var v in toolsPackDict)
{
if (v.Key == toolsName)
{
if (v.Value.Count > 0)
{
return v.Value;
}
else
{
return null;
}
}
}
return null;
}
private void LoadConfig()
{
string path = Application.streamingAssetsPath + "/DataConfig/ToolsPackData.json";
if (File.Exists(path))
{
string jsonContent = File.ReadAllText(path);
JObject jsonObj = JObject.Parse(jsonContent);
toolsPackDict = new Dictionary<string, List<string>>();
var tools = jsonObj["tools"] as JObject;
if (tools != null)
{
ParseTools(tools["models"] as JArray);
}
// Debug.Log("工具和子项:");
// foreach (var item in toolsDict)
// {
// Debug.Log($"{item.Key}:");
// foreach (var subItem in item.Value)
// {
// Debug.Log($" - {subItem}");
// }
// }
}
else
{
}
}
void ParseTools(JArray models)
{
foreach (var model in models)
{
if (model is JValue)
{
string modelName = model.ToString();
if (!toolsPackDict.ContainsKey(modelName))
{
toolsPackDict[modelName] = new List<string>();
}
}
else if (model is JObject subTool)
{
string subToolName = subTool["name"].ToString();
var subItems = subTool["models"] ?? subTool["items"];
List<string> subItemList = new List<string>();
if (subItems is JArray subArray)
{
foreach (var subItem in subArray)
{
subItemList.Add(subItem.ToString());
}
}
toolsPackDict[subToolName] = subItemList;
if (subItems is JArray)
{
continue;
}
ParseTools(subItems as JArray); // 递归解析子对象
}
}
}
}
}