using UnityEngine; using System.Collections.Generic; using UnityEngine.UI; using Google.Protobuf.WellKnownTypes; public class JSONReader : MonoBehaviour { // 让 Unity 编辑器能够分别拖入两个不同的 JSON 文件 public TextAsset npcJsonFile; // 用来加载 NPC 数据 public TextAsset locationJsonFile; // 用来加载 Location 数据 public TextAsset eventJsonFile; public TextAsset matialJsonFile; public TextAsset sceneJsonFile; public TextAsset incidentSiteJosnFile; public TextAsset NPCJosnFile; public TextAsset LanguageJsonFile; public TextAsset SelectJsonFile; public TextAsset TaskJsonFile; 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(); public Dictionary incidentSiteDictionary = new Dictionary(); public Dictionary NPCDictionary = new Dictionary(); public Dictionary LanguageDictionary = new Dictionary(); public Dictionary SelectDictionary = new Dictionary(); public Dictionary TaskDictionary = 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); incidentSiteDictionary = IncidentSiteParseJSON(incidentSiteJosnFile.text); NPCDictionary = NPCParseJSON(incidentSiteJosnFile.text); LanguageDictionary = LanguageParseJSON(LanguageJsonFile.text); SelectDictionary = SelectJSON(SelectJsonFile.text); TaskDictionary = TaskJSON(TaskJsonFile.text); GetLanguageByID(20008); foreach (var npc in npcDictionary) { //Debug.Log(npc.Value.ID); //Debug.Log(npc.Value.Name); } GetNpcDataByID(8001); } // 解析 JSON 字符串为 Task 数据 public Dictionary TaskJSON(string json) { NPCData[] npcArray = JsonHelper.FromJson(json); Dictionary selectDictionary = new Dictionary(); foreach (var npc in npcArray) { npcDictionary[npc.ID] = npc; } return selectDictionary; } // 解析 JSON 字符串为 Select 数据 public Dictionary SelectJSON(string json) { NPCData[] npcArray = JsonHelper.FromJson(json); Dictionary selectDictionary = new Dictionary(); foreach (var npc in npcArray) { npcDictionary[npc.ID] = npc; } return selectDictionary; } // 解析 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; } public Dictionary IncidentSiteParseJSON(string json) { IncidentSite[] locationArray = JsonHelper.FromJson(json); Dictionary locationDictionary = new Dictionary(); foreach (var location in locationArray) { locationDictionary[location.ID] = location; } return locationDictionary; } public Dictionary NPCParseJSON(string json) { NPC[] locationArray = JsonHelper.FromJson(json); Dictionary locationDictionary = new Dictionary(); foreach (var location in locationArray) { locationDictionary[location.ID] = location; } return locationDictionary; } public Dictionary LanguageParseJSON(string json) { Language[] locationArray = JsonHelper.FromJson(json); Dictionary locationDictionary = new Dictionary(); foreach (var location in locationArray) { locationDictionary[location.ID] = location; } return locationDictionary; } //将所有的数据都拿出来方便调用 // 根据给定的ID获取对应的NPC数据,并返回字典中所有数据 public void GetNpcDataByID(int id) { // 例如,获取 NPC 字典中对应 ID 的数据 if (npcDictionary.ContainsKey(id)) { NPCData npcData = npcDictionary[id]; // 遍历并输出字典中的所有数据 foreach (var npc in npcDictionary) { } } else { Debug.Log($"No NPC found with ID: {id}"); } } //根据id获取数据 public Language GetLanguageByID(int id) { Language info = null; if (LanguageDictionary.TryGetValue(id, out info)) { // 判断 Note 是否等于 "预定演练" if (info.Note == "预订演练") { Debug.Log($"Found Language: ID = {info.ID}, Text = {info.Text}, Note = {info.Note}"); return info; // 返回符合条件的 Language 数据 } else { Debug.Log($"Language with ID {id} found, but Note is not '预定演练'."); } } else { Debug.Log($"No Language found with ID: {id}"); } return null; // 如果没有找到符合条件的语言数据,返回 null } //获取任务 public Task_ GetTaskByID(int id) { Task_ info = null; if (TaskDictionary.TryGetValue(id.ToString(), out info)) { return info; // 返回符合条件的 Task 数据 } else { Debug.Log($"No Language found with ID: {id}"); } return null; // 如果没有找到符合条件的语言数据,返回 null } public LocationData GetAreaDateById(int id) { LocationData info = null; if (locationDictionary.TryGetValue(id, out info)) { return info; } else return null; } public SceneData GetSceneById(int id) { SceneData info = null; if (sceneDictionary.TryGetValue(id, out info)) { return info; } else return null; } public EventData GetEvenById(int id) { EventData info = null; if (eventDictionary.TryGetValue(id, out info)) { return info; } else return null; } public IncidentSite GetIncidentSiteById(int id) { IncidentSite info = null; if (incidentSiteDictionary.TryGetValue(id, out info)) { return info; } else return null; } public NPCData GetRoleDateById(int id) { NPCData info = null; if (npcDictionary.TryGetValue(id, out info)) { return info; } else return null; } //设置UI文本的方法 public void SetUIText(Text text, int id) { Language languageinfo = GetLanguageByID(id); text.text = languageinfo.Text; } } // 帮助类,用于解析 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 string Name; public int Type; public string IncidentType; public string ObjList; public string AreaList; public int Storeroom; } [System.Serializable] public class IncidentSite { public int ID; public string Note; public int Name; public int Scene; public string Position; public int Volume; public string Difficulty; public string SpecialEvent; public string Role; public string GeneralRole; } [System.Serializable] public class NPC { public int ID; public string Note; public string Name; public string PlayScript; public string ResPath; public string State1; public string StateRes1; public string State2; public string StateRes2; public string Stats1; public string Stats2; } [System.Serializable] public class Language { public int ID; public string Text; public string Note; } [System.Serializable] public class Select { public string ID; public string Note; public string PlayScript; public string Group; public string ShowText; public string Icon; public string Precondition; public string Exclusive; public string TakeTime; public string TimeLimit; public string NextSelect; public string TaskLink; public string TaskLinkDetails; public string CallMode; public string CallRecipient; public string CorrectOption; public string Reward; public string Result; public string Route; public string TimingChange; public string AppliedUI; public string UIDetails; public string Seq; // 其他字段可根据需要添加 } [System.Serializable] public class Task_ { public string ID; public string Note; public string Name; public string Type; public string PlayScript; public string Trigger; public string TriggerLogic; public string OverseeCond; public string TimeLimit; public string Role; public string Selects; public string TargetType; public string Targets1; public string Targets2; public string Targets2Logic; public string ExtraValue; public string Sort; public string IsShow; public string Exclusive; public string Reward; }