Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Demigiant · Feb 24, 2011 at 05:43 PM · listserialization

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 :)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sradforth · Mar 27, 2012 at 03:18 PM 0
Share

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

4 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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] );
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Demigiant · Feb 24, 2011 at 07:48 PM 0
Share

Thanks $$anonymous$$olix :) though, actually, I'm already using parallel Lists (the sample List I wrote about is part of the pair :P)

avatar image Molix · Feb 24, 2011 at 10:27 PM 0
Share

Oh I see, so you have a dictionary of lists, e.g. Dictionary

avatar image Molix · Feb 24, 2011 at 10:31 PM 0
Share

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.

avatar image Demigiant · Feb 25, 2011 at 09:47 AM 0
Share

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

avatar image Demigiant · Feb 25, 2011 at 09:48 AM 0
Share

P.S. about array of arrays, I read somewhere else that it's not possible :/

Show more comments
avatar image
4
Best Answer

Answer by Demigiant · Feb 25, 2011 at 09:48 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bovine · Apr 29, 2012 at 07:19 PM 0
Share

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.

avatar image nuverian · Oct 05, 2012 at 08:29 AM 0
Share

Is that a bug or, is this the way it works (regarding the 8th depth)

avatar image Bovine · Oct 05, 2012 at 08:35 AM 0
Share

Unsure - it could be that some arbitrary depth was chosen so that serialization of an object doesn't dig too deeply, but who knows!

avatar image SinisterRainbow · Oct 21, 2013 at 09:45 PM 0
Share

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.

avatar image Bovine · Oct 21, 2013 at 11:03 PM 0
Share

I'd presumed this was editor serialization. Does your solution work for that?

avatar image
1

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>>>>();

alt text


wa.png (7.5 kB)
wa.png (23.8 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kpetkov · Nov 13, 2018 at 10:15 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges