_xiaofang/xiaofang/Assets/Script/JSONReader/JSONReader.cs
2024-12-28 23:53:11 +08:00

688 lines
19 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
using Google.Protobuf.WellKnownTypes;
using System;
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 TextAsset GlobalJsonFile;
public TextAsset SelectsJsonFile;
public Dictionary<int, NPCData> npcDictionary = new Dictionary<int, NPCData>();
public Dictionary<int, LocationData> locationDictionary = new Dictionary<int, LocationData>();
public Dictionary<int, EventData> eventDictionary = new Dictionary<int, EventData>();
public Dictionary<int, MatialData> matialDictionary = new Dictionary<int, MatialData>();
public Dictionary<int, SceneData> sceneDictionary = new Dictionary<int, SceneData>();
public Dictionary<int, IncidentSite> incidentSiteDictionary = new Dictionary<int, IncidentSite>();
public Dictionary<int, NPC> NPCDictionary = new Dictionary<int, NPC>();
public Dictionary<int, Language> LanguageDictionary = new Dictionary<int, Language>();
public Dictionary<string, Select> SelectDictionary = new Dictionary<string, Select>();
public Dictionary<string, Task_> TaskDictionary = new Dictionary<string, Task_>();
//小智专用
//读取职业名字
public Dictionary<int, GlobalJson> GlobalDictionary = new Dictionary<int, GlobalJson>();
public Dictionary<string, Select> ZZSelectsDictionary = new Dictionary<string, Select>();
public Dictionary<int, NPCData> OpcNameDictionary=new Dictionary<int, NPCData>();
public Dictionary<string, List<Select>> ZZFindTaskID = new Dictionary<string, List<Select>>();
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);
//小智专用
OpcNameDictionary = SelectOpcName(npcJsonFile.text);
GlobalDictionary = GlobelJSON(GlobalJsonFile.text);
ZZSelectsDictionary = SelectsJSON(SelectsJsonFile.text);
ZZFindTaskID = BoolTask(SelectJsonFile.text);
GetLanguageByID(20008);
foreach (var npc in sceneDictionary)
{
Debug.Log(npc.Value.ID);
//Debug.Log(npc.Value.Name);
}
GetNpcDataByID(8001);
}
// 解析 JSON 字符串为 Task 数据
public Dictionary<string, Task_> TaskJSON(string json)
{
Task_[] npcArray = JsonHelper.FromJson<Task_>(json);
Dictionary<string, Task_> selectDictionary = new Dictionary<string, Task_>();
foreach (var npc in npcArray)
{
TaskDictionary[npc.ID] = npc;
}
return TaskDictionary;
}
// 解析 JSON 字符串为 Select 数据
public Dictionary<string, Select> SelectJSON(string json)
{
Select[] npcArray = JsonHelper.FromJson<Select>(json);
Dictionary<string, Select> selectDictionary = new Dictionary<string, Select>();
foreach (var npc in npcArray)
{
SelectDictionary[npc.ID] = npc;
}
return selectDictionary;
}
// 解析 JSON 字符串为 NPC 数据
public 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 数据
public 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;
}
// 解析 JSON 字符串为 Event 数据
public Dictionary<int, EventData> EventParseJSON(string json)
{
EventData[] locationArray = JsonHelper.FromJson<EventData>(json);
Dictionary<int, EventData> locationDictionary = new Dictionary<int, EventData>();
foreach (var location in locationArray)
{
locationDictionary[location.ID] = location;
}
return locationDictionary;
}
public Dictionary<int, MatialData> MatialParseJSON(string json)
{
MatialData[] locationArray = JsonHelper.FromJson<MatialData>(json);
Dictionary<int, MatialData> locationDictionary = new Dictionary<int, MatialData>();
foreach (var location in locationArray)
{
locationDictionary[location.ID] = location;
}
return locationDictionary;
}
public Dictionary<int, SceneData> SceneParseJSON(string json)
{
SceneData[] locationArray = JsonHelper.FromJson<SceneData>(json);
Dictionary<int, SceneData> locationDictionary = new Dictionary<int, SceneData>();
foreach (var location in locationArray)
{
locationDictionary[location.ID] = location;
}
return locationDictionary;
}
public Dictionary<int, IncidentSite> IncidentSiteParseJSON(string json)
{
IncidentSite[] locationArray = JsonHelper.FromJson<IncidentSite>(json);
Dictionary<int, IncidentSite> locationDictionary = new Dictionary<int, IncidentSite>();
foreach (var location in locationArray)
{
locationDictionary[location.ID] = location;
}
return locationDictionary;
}
public Dictionary<int, NPC> NPCParseJSON(string json)
{
NPC[] locationArray = JsonHelper.FromJson<NPC>(json);
Dictionary<int, NPC> locationDictionary = new Dictionary<int, NPC>();
foreach (var location in locationArray)
{
locationDictionary[location.ID] = location;
}
return locationDictionary;
}
public Dictionary<int, Language> LanguageParseJSON(string json)
{
Language[] locationArray = JsonHelper.FromJson<Language>(json);
Dictionary<int, Language> locationDictionary = new Dictionary<int, Language>();
foreach (var location in locationArray)
{
locationDictionary[location.ID] = location;
}
return locationDictionary;
}
public Dictionary<int, GlobalJson> GlobelJSON(string json)
{
GlobalJson[] locationArray = JsonHelper.FromJson<GlobalJson>(json);
Dictionary<int, GlobalJson> locationDictionary = new Dictionary<int, GlobalJson>();
foreach (var location in locationArray)
{
locationDictionary[location.ID] = location;
}
return locationDictionary;
}
public Dictionary<string, Select> SelectsJSON(string json)
{
Select[] locationArray = JsonHelper.FromJson<Select>(json);
Dictionary<string, Select> locationDictionary = new Dictionary<string,Select>();
foreach (var location in locationArray)
{
locationDictionary[location.ID] = location;
}
return locationDictionary;
}
//小智专用
//读取职业名字
public Dictionary<int, NPCData> SelectOpcName(string json)
{
NPCData[] npcArray = JsonHelper.FromJson<NPCData>(json);
Dictionary<int, NPCData> OpcNameDictionary = new Dictionary<int, NPCData>();
foreach (var npc in npcArray)
{
OpcNameDictionary[npc.ID]= npc;
}
return OpcNameDictionary;
}
//找到玩家接取的任务ID对应的
public Dictionary<string, List<Select>> BoolTask(string json)
{
Select[] locationArray = JsonHelper.FromJson<Select>(json);
Dictionary<string, List<Select>> locationDictionary = new Dictionary<string, List<Select>>();
foreach (var location in locationArray)
{
if (!locationDictionary.ContainsKey(location.Precondition))
{
// 如果键不存在,初始化一个新的列表
locationDictionary[location.Precondition] = new List<Select>();
}
// 添加到列表中
locationDictionary[location.Precondition].Add(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;
}
else return 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 string SetUIText(string id)
{
Language info = null;
if (LanguageDictionary.TryGetValue(int.Parse(id), out info))
{
return info.Text;
}
else
{
Debug.Log($"No Language found with ID: {id}");
return null;
}
}
/// <summary>
/// 判断指挥官选择了哪一个选项(展示废除)
/// </summary>
/// <param name="a">a=1选择了第一个a=2,a=3</param>
/// <param name="id">ID不出情况一般固定90016</param>
/// <returns></returns>
public string GetGlobal(int a,int id)
{
GlobalJson temp=null ;
switch(a)
{
case 1:
if (GlobalDictionary.TryGetValue(id, out temp))
return temp.VectorInt1;
else
return null;
case 2:
if (GlobalDictionary.TryGetValue(id, out temp))
return temp.VectorInt2;
else
return null;
case 3:
if (GlobalDictionary.TryGetValue(id, out temp))
return temp.VectorInt3;
else
return null;
default:
return "颠仔没有这种东西";
}
//GlobalJson info = null;
//if (GlobalDictionary.TryGetValue(id,out info))
//{
// return info.Int1.ToString();
//}
//else
//{
// Debug.Log($"No Language found with ID: {id}");
// return null;
//}
}
public string GetEvacuate(string id)
{
Select info = null;
if (ZZSelectsDictionary.TryGetValue(id.ToString(), out info))
{
return info.Note;
}
else
{
Debug.Log($"No Select found with ID: {id}");
return null;
}
}
/// <summary>
/// 读取职业名字
/// </summary>
/// <param name="id">职业ID去Role表获取</param>
/// <returns></returns>
public string GetOcpName(int id)
{
NPCData info = null;
if (OpcNameDictionary.TryGetValue(id, out info))
{
return info.Note;
}
else
{
Debug.Log($"No Select found with ID: {id}");
return null;
}
}
public string GetOcpID(int id)
{
Task_ info = null;
if (TaskDictionary.TryGetValue(id.ToString(), out info))
{
return info.Role;
}
else
{
Debug.Log($"No Select found with ID: {id}");
return null;
}
}
public string GetNPCdian(string id)
{
Select info = null;
if (ZZSelectsDictionary.TryGetValue(id.ToString(), out info))
{
return info.Route;
}
else
{
Debug.Log($"No Select found with ID: {id}");
return null;
}
}
}
// 帮助类,用于解析 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 Range;
public string NpcRatio;
public string PlayerSpawnPoint;
public string LabelPos;
public string ShowRadius;
public string Oversee;
public string EndPoint;
public string RoleLimit;
public string 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;
}
[Serializable]
public class Select
{
public string ID;
public string Note;
public string PlayScript;
public string Group;
public string UnDisplay;
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;
}
[System.Serializable]
public class GlobalJson
{
public int ID;
public string Note;
public int Scene;
public int PlayScript;
public int Int1;
public int Int2;
public int Int3;
public float Float1;
public float Float2;
public float Float3;
public string VectorInt1;
public string VectorInt2;
public string VectorInt3;
public string VectorInt4;
public string VectorInt5;
public string String1;
public string String2;
public string String3;
}