using UnityEngine; using System.Collections.Generic; public class JSONReader : MonoBehaviour { // 让 Unity 编辑器能够分别拖入两个不同的 JSON 文件 public TextAsset npcJsonFile; // 用来加载 NPC 数据 public TextAsset locationJsonFile; // 用来加载 Location 数据 public TextAsset eventJsonFile; public TextAsset matialJsonFile; public TextAsset sceneJsonFile; public Dictionary npcDictionary = new Dictionary(); public Dictionary locationDictionary = new Dictionary(); public Dictionary eventDictionary = new Dictionary(); public Dictionary matialDictionary = new Dictionary(); public Dictionary sceneDictionary = new Dictionary(); void Awake() { // 解析 NPC 和 Location 数据 npcDictionary = ParseJSON(npcJsonFile.text); locationDictionary = LocationParseJSON(locationJsonFile.text); eventDictionary = EventParseJSON(eventJsonFile.text); matialDictionary = MatialParseJSON(matialJsonFile.text); sceneDictionary = SceneParseJSON(sceneJsonFile.text); foreach (var npc in sceneDictionary) { Debug.Log($"Scene ID: {npc.Value.ID}"); Debug.Log($"Scene Name: {npc.Value.Name}"); Debug.Log($"Scene Type: {npc.Value.Type}"); Debug.Log($"Incident Type: {npc.Value.IncidentType}"); Debug.Log($"Object List: {npc.Value.ObjList}"); Debug.Log($"Area List: {npc.Value.AreaList}"); Debug.Log($"Storeroom: {npc.Value.Storeroom}"); Debug.Log("==========================="); } //打印 NPC 数据 //PrintNPCData(npcDictionary); // 打印 Location 数据 ////PrintLocationData(locationDictionary); } // 解析 JSON 字符串为 NPC 数据 public Dictionary ParseJSON(string json) { NPCData[] npcArray = JsonHelper.FromJson(json); Dictionary npcDictionary = new Dictionary(); foreach (var npc in npcArray) { npcDictionary[npc.ID] = npc; } return npcDictionary; } // 解析 JSON 字符串为 Location 数据 public Dictionary LocationParseJSON(string json) { LocationData[] locationArray = JsonHelper.FromJson(json); Dictionary locationDictionary = new Dictionary(); foreach (var location in locationArray) { locationDictionary[location.ID] = location; } return locationDictionary; } // 解析 JSON 字符串为 Event 数据 public Dictionary EventParseJSON(string json) { EventData[] locationArray = JsonHelper.FromJson(json); Dictionary locationDictionary = new Dictionary(); foreach (var location in locationArray) { locationDictionary[location.ID] = location; } return locationDictionary; } public Dictionary MatialParseJSON(string json) { MatialData[] locationArray = JsonHelper.FromJson(json); Dictionary locationDictionary = new Dictionary(); foreach (var location in locationArray) { locationDictionary[location.ID] = location; } return locationDictionary; } public Dictionary SceneParseJSON(string json) { SceneData[] locationArray = JsonHelper.FromJson(json); Dictionary locationDictionary = new Dictionary(); foreach (var location in locationArray) { locationDictionary[location.ID] = location; } return locationDictionary; } // 打印 NPC 数据 //void PrintNPCData(Dictionary 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 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(string json) { json = "{\"items\":" + json + "}"; Wrapper wrapper = JsonUtility.FromJson>(json); return wrapper.items; } [System.Serializable] private class Wrapper { 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; } [System.Serializable] public class EventData { public int ID; public string Note; public int Name; public string Role; public string DisasterLocation; } [System.Serializable] public class MatialData { public int ID; public string Note; public int Type; public int Name; public int Weight; public int Scene; public string RoleLimit; public string Icon; public string ResPath; public int ConsumableType; public int Durations; public string Attribute; public string IsPickup; public int PutInStore; } [System.Serializable] public class SceneData { public int ID; public int Name; public int Type; public string IncidentType; public string ObjList; public string AreaList; public int Storeroom; }