248 lines
7.4 KiB
C#
248 lines
7.4 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 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>();
|
|
|
|
void Awake()
|
|
{
|
|
// 解析 NPC 和 Location 数据
|
|
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)
|
|
{
|
|
Debug.Log("111111111111"+npc.Value.RoleLimit);
|
|
// 通过逗号分隔 RoleLimit 字段
|
|
string roleLimit = npc.Value.RoleLimit;
|
|
|
|
// 如果 RoleLimit 不是空字符串,按照逗号分隔
|
|
if (!string.IsNullOrEmpty(roleLimit))
|
|
{
|
|
string[] roleLimits = roleLimit.Split(',');
|
|
|
|
// 打印分隔后的角色限制
|
|
foreach (string role in roleLimits)
|
|
{
|
|
Debug.Log("RoleLimit Item: " + role);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("RoleLimit is empty for NPC ID: " + npc.Key);
|
|
}
|
|
}
|
|
|
|
//打印 NPC 数据
|
|
//PrintNPCData(npcDictionary);
|
|
|
|
// 打印 Location 数据
|
|
////PrintLocationData(locationDictionary);
|
|
}
|
|
|
|
// 解析 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;
|
|
}
|
|
|
|
// 打印 NPC 数据
|
|
//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("------------------------------------");
|
|
// }
|
|
//}
|
|
|
|
// // 打印 Location 数据
|
|
//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("===========================");
|
|
// }
|
|
//}
|
|
}
|
|
|
|
// 帮助类,用于解析 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;
|
|
}
|
|
|
|
|