ISerializationCallbackReceiver interface and serialization in Unity

My problem started when I started using ISerializationCallbackReciever

to create Serializable Dictionary

in Unity. It worked great for most of my classes, but when I nested 2 Serializable Dictionaries

I started getting problems with Enumeration.

I suppose that at a time OnBeforeSerialize

when the dictionary was having problems adding KeyValue Pairs

after a certain amount, I solved it by specifying a property of the dictionary and using it to access the data in my keys and lists, to be a bit more clear I was working on Custom Inspector

. which had options for keys, so I thought using Serializable Dictionary

would work best since I have over a hundred options, what my object does is collect data from my assets and divide that data depending on the key I gave it.

code for my Scriptable object:

[System.Serializable]
public class LetterDataObject : ScriptableObject
{
    [SerializeField]
    public DictionaryOfLettersData lettersData;

    [SerializeField]
    public bool isText = false;

    [SerializeField]
    public bool [] folds;
}

[System.Serializable]
public class Letter_Data
{
    [SerializeField]
    public string letterText;
    [SerializeField]
    public Sprite letterSprite;
    [SerializeField]
    public AudioClip letterAudio;
}

[System.Serializable]
public class LetterDataDictionary : SerializableDict<int, Letter_Data>
{

}

[System.Serializable]
public class DictionaryOfLettersData : SerializableDict<int, LetterDataDictionary>
{

}

      

As you can see, each datatype Serializable

, I believe my problem is that the dictionary changes over time OnBeforeSerialize()

, probably as I am accessing Serializable Dictionary

another object, so I used the dictionary property to solve it.

[Serializable]
public class SerializableDict<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
{



  [SerializeField, HideInInspector]
    private List<TKey> keys = new List<TKey>();
    [SerializeField, HideInInspector]
    private List<TValue> values = new List<TValue>();


    public Dictionary<TKey, TValue> serializedDictionary
    {
         get;
        private set;
    }

    public void OnBeforeSerialize()
    {

        foreach (var item in serializedDictionary)
            {
                if (!keys.Contains(item.Key))
                    keys.Add(item.Key);

                if (!values.Contains(item.Value))
                    values.Add(item.Value);
            }
    }

    public void OnAfterDeserialize()
    {
        serializedDictionary = new Dictionary<TKey, TValue>();
            for (var i = 0; i < keys.Count; i++)
            {
                serializedDictionary[keys[i]] = values[i];
            }
    }
}

      

This may not be a problem as it has been resolved now, but I am curious if anyone can give a hint as to what is going on? Maybe there is something I didn't know about with unity serialization, so I would love it, if someone ran into the same problem and helped me figure it out further, I can't move on without understanding my little hack.

+3


source to share





All Articles