_xiaofang/xiaofang/Assets/Script/JSONReader/JSONReader.cs

345 lines
9.5 KiB
C#
Raw Normal View History

2024-11-30 18:15:51 +08:00
using UnityEngine;
using System.Collections.Generic;
2024-12-05 15:39:10 +08:00
using UnityEngine.UI;
2024-11-30 18:15:51 +08:00
public class JSONReader : MonoBehaviour
{
// <20><> Unity <20><EFBFBD><E0BCAD><EFBFBD>ܹ<EFBFBD><DCB9>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC> JSON <20>ļ<EFBFBD>
public TextAsset npcJsonFile; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> NPC <20><><EFBFBD><EFBFBD>
public TextAsset locationJsonFile; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Location <20><><EFBFBD><EFBFBD>
2024-11-30 23:32:17 +08:00
public TextAsset eventJsonFile;
public TextAsset matialJsonFile;
public TextAsset sceneJsonFile;
2024-12-01 22:36:41 +08:00
public TextAsset incidentSiteJosnFile;
2024-12-01 23:00:40 +08:00
public TextAsset NPCJosnFile;
2024-12-04 22:58:31 +08:00
public TextAsset LanguageJsonFile;
2024-11-30 23:32:17 +08:00
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>();
2024-12-01 22:36:41 +08:00
public Dictionary<int, IncidentSite> incidentSiteDictionary = new Dictionary<int, IncidentSite>();
2024-12-01 23:00:40 +08:00
public Dictionary<int, NPC> NPCDictionary = new Dictionary<int, NPC>();
2024-12-04 22:58:31 +08:00
public Dictionary<int, Language> LanguageDictionary = new Dictionary<int, Language>();
2024-11-30 18:15:51 +08:00
2024-11-30 23:32:17 +08:00
void Awake()
2024-11-30 18:15:51 +08:00
{
// <20><><EFBFBD><EFBFBD> NPC <20><> Location <20><><EFBFBD><EFBFBD>
2024-11-30 23:32:17 +08:00
npcDictionary = ParseJSON(npcJsonFile.text);
locationDictionary = LocationParseJSON(locationJsonFile.text);
eventDictionary = EventParseJSON(eventJsonFile.text);
matialDictionary = MatialParseJSON(matialJsonFile.text);
sceneDictionary = SceneParseJSON(sceneJsonFile.text);
2024-12-01 22:36:41 +08:00
incidentSiteDictionary = IncidentSiteParseJSON(incidentSiteJosnFile.text);
2024-12-01 23:00:40 +08:00
NPCDictionary = NPCParseJSON(incidentSiteJosnFile.text);
2024-12-05 15:39:10 +08:00
LanguageDictionary = LanguageParseJSON(LanguageJsonFile.text);
2024-12-04 22:58:31 +08:00
GetLanguageByID(20008);
2024-12-05 15:39:10 +08:00
foreach (var npc in sceneDictionary)
2024-11-30 23:32:17 +08:00
{
2024-12-05 15:39:10 +08:00
// Debug.Log(npc.Value.ID);
//Debug.Log(npc.Value.Name);
2024-11-30 23:32:17 +08:00
}
2024-12-04 16:37:51 +08:00
GetNpcDataByID(8001);
2024-11-30 18:15:51 +08:00
}
// <20><><EFBFBD><EFBFBD> JSON <20>ַ<EFBFBD><D6B7><EFBFBD>Ϊ NPC <20><><EFBFBD><EFBFBD>
2024-11-30 23:32:17 +08:00
public Dictionary<int, NPCData> ParseJSON(string json)
2024-11-30 18:15:51 +08:00
{
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;
}
// <20><><EFBFBD><EFBFBD> JSON <20>ַ<EFBFBD><D6B7><EFBFBD>Ϊ Location <20><><EFBFBD><EFBFBD>
2024-11-30 23:32:17 +08:00
public Dictionary<int, LocationData> LocationParseJSON(string json)
2024-11-30 18:15:51 +08:00
{
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;
}
2024-12-04 16:37:51 +08:00
// <20><><EFBFBD><EFBFBD> JSON <20>ַ<EFBFBD><D6B7><EFBFBD>Ϊ Event <20><><EFBFBD><EFBFBD>
2024-11-30 23:32:17 +08:00
public Dictionary<int, EventData> EventParseJSON(string json)
2024-11-30 18:15:51 +08:00
{
2024-11-30 23:32:17 +08:00
EventData[] locationArray = JsonHelper.FromJson<EventData>(json);
Dictionary<int, EventData> locationDictionary = new Dictionary<int, EventData>();
2024-11-30 18:15:51 +08:00
2024-11-30 23:32:17 +08:00
foreach (var location in locationArray)
2024-11-30 18:15:51 +08:00
{
2024-11-30 23:32:17 +08:00
locationDictionary[location.ID] = location;
2024-11-30 18:15:51 +08:00
}
2024-11-30 23:32:17 +08:00
return locationDictionary;
2024-11-30 18:15:51 +08:00
}
2024-11-30 23:32:17 +08:00
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;
}
2024-12-01 22:36:41 +08:00
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;
}
2024-12-01 23:00:40 +08:00
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;
}
2024-12-01 22:36:41 +08:00
2024-12-04 22:58:31 +08:00
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;
}
2024-12-04 16:37:51 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD>ݶ<EFBFBD><DDB6>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD>ݸ<EFBFBD><DDB8><EFBFBD><EFBFBD><EFBFBD>ID<49><44>ȡ<EFBFBD><C8A1>Ӧ<EFBFBD><D3A6>NPC<50><43><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public void GetNpcDataByID(int id)
{
// <20><><EFBFBD><EFBFBD><E7A3AC>ȡ NPC <20>ֵ<EFBFBD><D6B5>ж<EFBFBD>Ӧ ID <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (npcDictionary.ContainsKey(id))
{
NPCData npcData = npcDictionary[id];
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
foreach (var npc in npcDictionary)
{
}
}
else
{
Debug.Log($"No NPC found with ID: {id}");
}
}
2024-12-04 22:58:31 +08:00
2024-12-05 15:39:10 +08:00
//<2F><><EFBFBD><EFBFBD>id<69><64>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
2024-12-04 22:58:31 +08:00
public Language GetLanguageByID(int id)
{
Language info = null;
if (LanguageDictionary.TryGetValue(id, out info))
{
// <20>ж<EFBFBD> Note <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"
if (info.Note == <><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")
{
Debug.Log($"Found Language: ID = {info.ID}, Text = {info.Text}, Note = {info.Note}");
return info; // <20><><EFBFBD>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Language <20><><EFBFBD><EFBFBD>
}
else
{
Debug.Log($"Language with ID {id} found, but Note is not 'Ԥ<><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>'.");
}
}
else
{
Debug.Log($"No Language found with ID: {id}");
}
return null; // <20><><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD> null
}
2024-12-05 15:39:10 +08:00
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;
}
//<2F><><EFBFBD><EFBFBD>UI<55>ı<EFBFBD><C4B1>ķ<EFBFBD><C4B7><EFBFBD>
public void SetUIText(Text text, int id)
{
Language languageinfo = GetLanguageByID(id);
text.text = languageinfo.Text;
}
2024-11-30 18:15:51 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0A3AC><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD> JSON <20><><EFBFBD><EFBFBD>
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;
}
2024-11-30 23:32:17 +08:00
[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;
}
2024-12-01 22:36:41 +08:00
[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;
}
2024-12-01 23:00:40 +08:00
[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;
}
2024-12-04 22:58:31 +08:00
[System.Serializable]
public class Language
{
public int ID;
public string Text;
public string Note;
}