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

136 lines
4.0 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>
void Start()
{
// <20><><EFBFBD><EFBFBD> NPC <20><> Location <20><><EFBFBD><EFBFBD>
Dictionary<int, NPCData> npcDictionary = ParseJSON(npcJsonFile.text);
Dictionary<int, LocationData> locationDictionary = LocationParseJSON(locationJsonFile.text);
// <20><>ӡ NPC <20><><EFBFBD><EFBFBD>
PrintNPCData(npcDictionary);
// <20><>ӡ Location <20><><EFBFBD><EFBFBD>
PrintLocationData(locationDictionary);
}
// <20><><EFBFBD><EFBFBD> JSON <20>ַ<EFBFBD><D6B7><EFBFBD>Ϊ NPC <20><><EFBFBD><EFBFBD>
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;
}
// <20><><EFBFBD><EFBFBD> JSON <20>ַ<EFBFBD><D6B7><EFBFBD>Ϊ Location <20><><EFBFBD><EFBFBD>
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;
}
// <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("===========================");
}
}
}
// <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;
}