직렬화된 딕셔너리 만든다음에 인스펙터 창에서 리스트추가하려고 하면 ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 에러가 뜬다...
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 (var kvp in this)
{
keys.Add(kvp.Key);
values.Add(kvp.Value);
}
}
// load dictionary from lists
public void OnAfterDeserialize()
{
this.Clear();
for( int i = 0; i < keys.Count(); i++ )
{
Debug.Log(i);
Debug.Log(this.Count());
this.Add( keys[i], values[i] );
}
}
}
(작성한 코드)
문제가 되는 부분은 OnAfterDeserialize()
https://drehzr.tistory.com/931
여기보면 키와 벨류의 리스트 크기가 다르면 예외를 발생시키고 있음.
인스펙터에서 리스트 추가하려고 할때 순간적으로 키와 벨류 리스트의 크기가 매치되지 않는 순간이 발생해서 그런거 아닐까 싶은데.. 참고해봐
확실히 그쪽문제라고 생각해봐서 if문으로 검토 해봤는데 FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
에러가 뜨네...
확실한건 키와 벨류 리스틍릐 크기가 달라서 생기는 문제가 맞는것같음