_xiaofang/xiaofang/Assets/Script/JSONReader/JSONReader.cs
2024-11-30 18:15:51 +08:00

136 lines
4.0 KiB
C#

using UnityEngine;
using System.Collections.Generic;
public class JSONReader : MonoBehaviour
{
// 让 Unity 编辑器能够分别拖入两个不同的 JSON 文件
public TextAsset npcJsonFile; // 用来加载 NPC 数据
public TextAsset locationJsonFile; // 用来加载 Location 数据
void Start()
{
// 解析 NPC 和 Location 数据
Dictionary<int, NPCData> npcDictionary = ParseJSON(npcJsonFile.text);
Dictionary<int, LocationData> locationDictionary = LocationParseJSON(locationJsonFile.text);
// 打印 NPC 数据
PrintNPCData(npcDictionary);
// 打印 Location 数据
PrintLocationData(locationDictionary);
}
// 解析 JSON 字符串为 NPC 数据
Dictionary<int, NPCData> ParseJSON(string json)
{
NPCData[] npcArray = JsonHelper.FromJson<NPCData>(json);
Dictionary<int, NPCData> npcDictionary = new Dictionary<int, NPCData>();
foreach (var npc in npcArray)
{
npcDictionary[npc.ID] = npc;
}
return npcDictionary;
}
// 解析 JSON 字符串为 Location 数据
Dictionary<int, LocationData> LocationParseJSON(string json)
{
LocationData[] locationArray = JsonHelper.FromJson<LocationData>(json);
Dictionary<int, LocationData> locationDictionary = new Dictionary<int, LocationData>();
foreach (var location in locationArray)
{
locationDictionary[location.ID] = location;
}
return locationDictionary;
}
// 打印 NPC 数据
void PrintNPCData(Dictionary<int, NPCData> npcDictionary)
{
foreach (var npc in npcDictionary)
{
Debug.Log($"NPC ID: {npc.Value.ID}");
Debug.Log($"Note: {npc.Value.Note}");
Debug.Log($"Name: {npc.Value.Name}");
Debug.Log($"ActionMode: {npc.Value.ActionMode}");
Debug.Log($"Group: {npc.Value.Group}");
Debug.Log($"GroupLeader: {npc.Value.GroupLeader}");
Debug.Log($"IsLeadingNPC: {npc.Value.IsLeadingNPC}");
Debug.Log($"ICON: {npc.Value.ICON}");
Debug.Log($"WeightLimit: {npc.Value.WeightLimit}");
Debug.Log($"Stats: {npc.Value.Stats}");
Debug.Log($"Skills: {npc.Value.Skills}");
Debug.Log($"ResPath: {npc.Value.ResPath}");
Debug.Log("------------------------------------");
}
}
// 打印 Location 数据
void PrintLocationData(Dictionary<int, LocationData> locationDictionary)
{
foreach (var location in locationDictionary)
{
Debug.Log($"Location ID: {location.Value.ID}");
Debug.Log($"Note: {location.Value.Note}");
Debug.Log($"Name: {location.Value.Name}");
Debug.Log($"Scene: {location.Value.Scene}");
Debug.Log($"NpcRatio: {location.Value.NpcRatio}");
Debug.Log($"Oversee: {location.Value.Oversee}");
Debug.Log($"RoleLimit: {location.Value.RoleLimit}");
Debug.Log($"Level: {location.Value.Level}");
Debug.Log("===========================");
}
}
}
// 帮助类,用于解析 JSON 数组
public static class JsonHelper
{
public static T[] FromJson<T>(string json)
{
json = "{\"items\":" + json + "}";
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.items;
}
[System.Serializable]
private class Wrapper<T>
{
public T[] items;
}
}
[System.Serializable]
public class NPCData
{
public int ID;
public string Note;
public string Name;
public int ActionMode;
public int Group;
public int GroupLeader;
public int IsLeadingNPC;
public string ICON;
public int WeightLimit;
public string Stats;
public string Skills;
public string ResPath;
}
[System.Serializable]
public class LocationData
{
public int ID;
public string Note;
public int Name;
public int Scene;
public string NpcRatio;
public string Oversee;
public string RoleLimit;
public int Level;
}