Serialize a List containing another List (List<List<Object>>)

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`), and it works perfectly, thus it's not an issue.

Thanks & a nice day :)

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_, values *);*_
_*```*_

Well, looks like the answer is simple: nope, can't be done :P

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.

If you use VFW, you could serialize (and expose) infinitely-nested lists :stuck_out_tongue:

public List< List< List< List< string>>>> craizeh = new List< List< List< List< string>>>>();

38141-wa.png

Hi Vexe, could you please show a quick example script how a List<List<int>> can be serialized ?