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

248 lines
7.4 KiB
C#
Raw Normal View History

2024-11-30 18:15:51 +08:00
using UnityEngine;
using System.Collections.Generic;
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-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-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);
foreach (var npc in locationDictionary)
2024-11-30 23:32:17 +08:00
{
Debug.Log("111111111111"+npc.Value.RoleLimit);
// ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD>ŷָ<C5B7> RoleLimit <20>ֶ<EFBFBD>
string roleLimit = npc.Value.RoleLimit;
// <20><><EFBFBD><EFBFBD> RoleLimit <20><><EFBFBD>ǿ<EFBFBD><C7BF>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ն<EFBFBD><D5B6>ŷָ<C5B7>
if (!string.IsNullOrEmpty(roleLimit))
{
string[] roleLimits = roleLimit.Split(',');
// <20><>ӡ<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>Ľ<EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>
foreach (string role in roleLimits)
{
Debug.Log("RoleLimit Item: " + role);
}
}
else
{
Debug.Log("RoleLimit is empty for NPC ID: " + npc.Key);
}
2024-11-30 23:32:17 +08:00
}
2024-11-30 18:15:51 +08:00
2024-11-30 23:32:17 +08:00
//<2F><>ӡ NPC <20><><EFBFBD><EFBFBD>
//PrintNPCData(npcDictionary);
2024-11-30 18:15:51 +08:00
// <20><>ӡ Location <20><><EFBFBD><EFBFBD>
2024-11-30 23:32:17 +08:00
////PrintLocationData(locationDictionary);
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-11-30 23:32:17 +08:00
// <20><><EFBFBD><EFBFBD> JSON <20>ַ<EFBFBD><D6B7><EFBFBD>Ϊ Event <20><><EFBFBD><EFBFBD>
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-11-30 23:32:17 +08:00
// <20><>ӡ NPC <20><><EFBFBD><EFBFBD>
//void PrintNPCData(Dictionary<int, NPCData> 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("------------------------------------");
// }
//}
// // <20><>ӡ Location <20><><EFBFBD><EFBFBD>
//void PrintLocationData(Dictionary<int, LocationData> 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("===========================");
// }
//}
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-11-30 23:32:17 +08:00