Hi, i want to put this into json file for saving.
public SerializableDictionary<NFT, List<NFTData>> _collectionsPageNFT;
But when i tried to save getting a error;
FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
System.Text.StringBuilder.AppendFormatHelper (System.IFormatProvider provider, System.String format, System.ParamsArray args) (at <6073cf49ed704e958b8a66d540dea948>:0)
System.String.FormatHelper (System.IFormatProvider provider, System.String format, System.ParamsArray args) (at <6073cf49ed704e958b8a66d540dea948>:0)
System.String.Format (System.String format, System.Object[] args) (at <6073cf49ed704e958b8a66d540dea948>:0)
SerializableDictionary`2[TKey,TValue].OnAfterDeserialize () (at Assets/Scripts/SaveLoad/SerializableDictionary.cs:36)
UnityEngine.JsonUtility:FromJson(String)
SaveLoad:Load() (at Assets/Scripts/SaveLoad/SaveLoad.cs:61)
SaveGameManager:TryLoadData() (at Assets/Scripts/SaveLoad/SaveGameManager.cs:41)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)
I think we can’t put list to serializable dictionary. Just want to ask any way to do this.
Also here’s my script to serialize;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[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>();
// save the dictionary to lists
public void OnBeforeSerialize()
{
keys.Clear();
values.Clear();
foreach (KeyValuePair<TKey, TValue> pair in this)
{
keys.Add(pair.Key);
values.Add(pair.Value);
}
}
// load dictionary from lists
public void OnAfterDeserialize()
{
this.Clear();
if (keys.Count != values.Count)
throw new System.Exception(string.Format("there are {0} keys and {1} values after deserialization. Make sure that both key and value types are serializable."));
for (int i = 0; i < keys.Count; i++)
this.Add(keys[i], values[i]);
}
}