265 lines
7.3 KiB
C#
265 lines
7.3 KiB
C#
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 TextAsset incidentSiteJosnFile;
|
||
public TextAsset NPCJosnFile;
|
||
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>();
|
||
|
||
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);
|
||
foreach (var npc in locationDictionary)
|
||
{
|
||
|
||
}
|
||
GetNpcDataByID(8001);
|
||
}
|
||
|
||
// 解析 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;
|
||
}
|
||
|
||
//将所有的数据都拿出来方便调用
|
||
// 根据给定的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}");
|
||
}
|
||
}
|
||
}
|
||
|
||
// 帮助类,用于解析 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;
|
||
}
|
||
[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;
|
||
}
|
||
[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;
|
||
}
|
||
|
||
[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;
|
||
}
|