- Home /
Editor Serialization of Collections/Collection Interfaces
According to this page in the documentation, only arrays and Lists of serializable classes can be serialized for persistence in the editor (for example, between assembly reloads).
Is it possible to somehow serialize collections that implement IList<T>, or other strongly typed generic collection interfaces (whose instances would all be typed to a serializable class)?
I imagine that under-the-hood, Unity is relying on IList<T> or ICollection<T> member methods to reconstruct a serialized List<T> in memory, but since it needs to know the final type of the collection it just only allows for serialization of List<T>; (no need to figure out the final derived type if there's only one possibility).
Answer by vexe · Jan 04, 2015 at 12:40 PM
I know this is an old question, but better have an answer than no answer.
Unfortunately, even today with Unity 5 on the horizons, with Unity's serialization there's not much one could do. Don't go too far with IList
, try to even inherit List
and you will fail too. Simply because List handling is hardcoded into Unity's serialization logic (inspect the code in Data/Managed/SerializationLogic.dll and you will see it in plain text what unity can/can't serialize)
Solution? well, you need to implement your own serialization system if you want to cover those edge-cases. Fortunately, this is now easier since the introduction of ISerializationCallbackReceiver in Unity 4.5 or so. That, and a good custom serializer you could pretty much solve most of Unity's serialization problems. This is what I basically did in VFW, a custom serialization system that pretty much serializes anything.
Your answer
Follow this Question
Related Questions
Serialize Texture3D 1 Answer
Detecting serialization reload in editor 1 Answer
Big Problem Serializing 0 Answers