azixMcAze/Unity-SerializableDictionary

Using SerializableDictionary in ScriptableObjects: data loss

Bogenbai opened this issue · 3 comments

Maybe that's me dummy-dumb, but I faced next issue:

  1. I create scriptableobject that has SerializableDictionary as field
  2. I fill it with data via inspector (no problems here)
  3. I reload project and all data of ScriptableObject are lost
[Serializable]
public class StringEventDictionary : SerializableDictionary<string, Event> {}

[CreateAssetMenu(fileName = "New GameEvents Data", menuName = "GameEvents Data", order = 54)]
public class GameEventsData : ScriptableObject
{
        [SerializeField]
        private StringEventDictionary events = null;

        public IDictionary<string, Event> Events
        {
            get { return events; }
            set { events.CopyFrom(value); }
        }
}

[Serializable]
public class Event
{
        public event Action<int> action;
        public void EventRunner(int id)
        {
            action?.Invoke(id);
        }
}

I'm running into that too.

Hi,

Are you using System.Action in the class Event? System.Action cannot be serialized by Unity. I think this is the cause of the data loss.
If I use a type serializable by unity, I don't observe a data loss when used in a ScriptableObject.

Facing the same issue still in 2024. Funnily, only one of two SOs of same type loses data. the other is fine. They are literally of instances of same type.