using UnityEngine; using System.Collections.Generic; using System; // Serialize対象が自作classなら[Serializable]を付ける必要がある // List -> Json文字列 ( 例 : List ) // string str = JsonUtility.ToJson(new Serialization(enemies)); // Json文字列 -> List // List enemies = JsonUtility.FromJson>(str).ToList(); [Serializable] public class Serialization { [SerializeField] private List target; public List ToList(){ return target; } public Serialization(List target){ this.target = target; } } // Dictionary -> Json文字列 ( 例 : Dictionary ) // string str = JsonUtility.ToJson(new Serialization(enemies)); // Json文字列 -> Dictionary // Dictionary enemies = JsonUtility.FromJson>(str).ToDictionary(); [Serializable] public class Serialization : ISerializationCallbackReceiver { [SerializeField] private List keys; [SerializeField] private List values; private 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]); } } }