#if !COMBAT_SERVER using System; using System.Collections.Generic; using UnityEngine; namespace Fort23.GameData { public class JsonHelper { public static string ToJson(object obj) { #if !COMBAT_SERVER return JsonUtility.ToJson(obj); #endif return null; } public static T FromJson(string json) { #if !COMBAT_SERVER return JsonUtility.FromJson(json); #endif return default; } public static object FromJson(string json, Type type) { #if !COMBAT_SERVER return JsonUtility.FromJson(json, type); #endif return null; } public static T Clone(T t) { return FromJson(ToJson(t)); } } /// /// 扩展JsonUtility的List能力, T必须加上[Serializable]标签 /// https://blog.csdn.net/Truck_Truck/article/details/78292390 /// /// [Serializable] public class Serialization { [SerializeField] List target; public List ToList() { return target; } public Serialization(List target) { this.target = target; } } /// /// 扩展JsonUtility的Dictionary能力,TValue必须加上[Serializable]标签 /// Dictionary /// /// /// [Serializable] public class Serialization : ISerializationCallbackReceiver { [SerializeField] List keys; [SerializeField] List values; Dictionary target; public Dictionary ToDictionary() { return target; } public Serialization(Dictionary target) { this.target = target; } public void OnBeforeSerialize() { keys = new List(target.Keys); values = new List(target.Values); } public void OnAfterDeserialize() { var count = Math.Min(keys.Count, values.Count); target = new Dictionary(count); for (var i = 0; i < count; ++i) { target.Add(keys[i], values[i]); } } } } #endif