Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- public static class JsonUtilityList
- {
- public static List<T> FromJson<T>(string json)
- {
- Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
- return wrapper.Items;
- }
- public static string ToJson<T>(List<T> list, bool prettyPrint = false)
- {
- Wrapper<T> wrapper = new Wrapper<T>(list);
- return JsonUtility.ToJson(wrapper, prettyPrint);
- }
- [Serializable]
- private class Wrapper<T>
- {
- public List<T> Items;
- public Wrapper(List<T> items)
- {
- Items = items;
- }
- }
- }
- using UnityEngine;
- using System.Collections.Generic;
- [System.Serializable]
- public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
- {
- [SerializeField] private List<TKey> keys = new List<TKey>();
- [SerializeField] private List<TValue> values = new List<TValue>();
- public void OnBeforeSerialize()
- {
- keys.Clear();
- values.Clear();
- foreach (KeyValuePair<TKey, TValue> pair in this)
- {
- keys.Add(pair.Key);
- values.Add(pair.Value);
- }
- }
- public void OnAfterDeserialize()
- {
- this.Clear();
- if (keys.Count != values.Count)
- Debug.LogError(" the amount of keys ("+ keys.Count + ") does not match the number of values (" + values.Count + ") so something went wrong");
- for (int i = 0; i < keys.Count; i++)
- this.Add(keys[i], values[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment