- Home /
Serialize a List containing another List (List>)
Hello,
I read lots of posts about serialization, but I can't find an answer to this one.
With C#, I'm trying to serialize a List containing another List (to avoid Unity's serialization limits with Dictionaries etc). Unity supports serialization of Lists of known objects without issues, but in this case it doesn't work. Is there a way to do it?
Sample list:
public List<List<MyClass>> targets;
Please note that I tried to serialize a simple List of "MyClass" (List<MyClass>
), and it works perfectly, thus it's not an issue.
Thanks & a nice day :)
If it helps anyone else, I found your question was similar to what I was trying to do in that I had a list of serializable objects with a list of scriptable objects inside each list element.
This wouldn't serialize out correctly however having a making just the root data item a scriptable object and the rest no inherit from scriptable object it did work... i.e. you can serialize list of lists
Answer by Molix · Feb 24, 2011 at 06:12 PM
Although this does not answer your question directly, it may help with what you indicated was the underlying problem. If the reason you're doing it is to get around serializing a dictionary, you could just have two 'parallel' Lists, e.g.
public List<string> keys;
public List<MyClass> values;
private Dictionary<string,MyClass> dict;
And then in Awake():
dict = new Dictionary<string,MyClass>();
for( int i=0; i<keys.Length; ++i )
dict.Add( keys[i], values[i] );
Thanks $$anonymous$$olix :) though, actually, I'm already using parallel Lists (the sample List I wrote about is part of the pair :P)
Oh I see, so you have a dictionary of lists, e.g. Dictionary
If that's the case, could you wrap the key and List<$$anonymous$$yClass> in another Serializable class, and then just List<$$anonymous$$yClassWrapper>? Also I wonder if the behaviour is different if you use normal arrays vs List.
Yup, I finally ended serializing a List<$$anonymous$$yClassA>, which contained a List<$$anonymous$$yClassB>, and this way it works :) I suppose that the answer to my question is: nope, can't be done :P
P.S. about array of arrays, I read somewhere else that it's not possible :/
Answer by Demigiant · Feb 25, 2011 at 09:48 AM
Well, looks like the answer is simple: nope, can't be done :P
Answer by Bovine · Oct 26, 2011 at 10:39 AM
However I believe you can do:
[System.Serializable]
public class ListContainer
{
public List<string> ContainedList = new List<string>();
}
[System.Serializable]
public class MasterList
{
public List<ListContainer> = new List<ListContainer>();
}
It's not fool proof, but it should work.
Incidentally this does work but irritatingly (at least in 3.5.1) at around the 8th depth the List ceases to serialise - I have submitted a bug.
Is that a bug or, is this the way it works (regarding the 8th depth)
Unsure - it could be that some arbitrary depth was chosen so that serialization of an object doesn't dig too deeply, but who knows!
I've seen it listed at a depth of 7 elsewhere (7-8 i guess depends if you are counting 0 i would guess)... I have taken a new approach to serialization and have made a master serialize class, and using delegates, use OnDataLoad() and OnDataSave() routines in other classes where I want data saved.. In these methods in each class is where list of array and store the individual arrays through the OnDataSave() when it's requested and similarly through load. This also makes it easy to encrypt the one file while it's in memory and before it's written to the disk, as well as generate hashcheck file and what have you. I don't think I ever fully got through the serialization docs before I decided to do this so maybe there's a better way, but i guess I just saw the problems right away and thought this was better. I can perhaps bundle it and sell it in the asset store for cheap if anyone thinks this will be useful. I haven't checked to see if there's anything similar there yet.
I'd presumed this was editor serialization. Does your solution work for that?
Answer by vexe · Jan 04, 2015 at 11:28 AM
If you use VFW, you could serialize (and expose) infinitely-nested lists :P
public List< List< List< List< string>>>> craizeh = new List< List< List< List< string>>>>();
Hi Vexe, could you please show a quick example script how a List<List<int>>
can be serialized ?
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Private list is serialized inside a prefab 0 Answers
How to add strings in a list AND be able to save it and load it? Using playerpref OR serialization 2 Answers
Custom Inspector; adding to list not getting saved 2 Answers
Scriptable Object's Data Gets Lost After Re-opening Unity!!! 1 Answer