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
4
Question by absameen · Jul 24, 2012 at 05:29 PM · listserializenested

Serialize Nested Lists

Will Unity not serialize nested lists such as:

 List<List<int>> nList = new List<List<int>>();

How can we serialize this list?

Comment
Add comment · Show 8
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 whydoidoit · Jul 24, 2012 at 05:50 PM 0
Share

In what way do you want to serialize it? Internally as an asset? Externally as a file or a string?

avatar image absameen · Jul 24, 2012 at 06:13 PM 0
Share

I need to serialize it as an asset (ScriptableObject) saved in the project directory.

avatar image whydoidoit · Jul 24, 2012 at 06:15 PM 0
Share

It's not just working like that? I serialize some complicated stuff and it always seems to work on ScriptableObject - what is happening?

avatar image absameen · Jul 24, 2012 at 06:23 PM 0
Share
 List<int> serializes fine. But with List<List<int>>, all data is lost when unity is restarted.
avatar image whydoidoit · Jul 24, 2012 at 06:27 PM 0
Share

Hmmm - I guess you might have to implement ISerializable then and store the data yourself - perhaps serializing that list of lists into a string or byte array using BinaryFormatter.

Show more comments

3 Replies

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

Answer by CapnCromulent · Sep 14, 2012 at 04:16 AM

Unfortunately, you'll have to write a wrapper class, like:

 [System.Serializable]
 public class ListWrapper
 {
      public List<int> myList;
 }

then declare within your ScriptableObject

 List<ListWrapper> nList =new List<ListWrapper>();
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 whydoidoit · Sep 14, 2012 at 08:48 AM 2
Share

Nice one. I do wish they'd fix that stuff.

avatar image anacierdem · Jan 21, 2014 at 09:42 PM 0
Share

Works like a charm. Except all the hassle :)

avatar image svizcay · Oct 09, 2017 at 11:45 AM 0
Share

It works fine but doesn't work if I make the wrapper class a template. Any suggestions for that?

avatar image brunocoimbra svizcay · Oct 09, 2017 at 12:23 PM 1
Share

@svizcay you mean something like this?

 [System.Serializable]
 public class ListWrapper<T>
 {
     public List<T> myList;
 }

You should do that:

 [System.Serializable]
 public class IntListWrapper : ListWrapper<int> { }
 
 [System.Serializable]
 public class FloatListWrapper : ListWrapper<float> { }

And so on for each type.

avatar image tuyh7845 · Oct 20, 2018 at 02:08 PM 0
Share

It works for me. $$anonymous$$y version is 2017.3 ... Thank you so much!

avatar image Knipscher · Feb 04, 2020 at 12:44 AM 0
Share

A note in case this helps anyone else: your wrapper class can't inherit from ScriptableObject or $$anonymous$$onoBehaviour, as Unity serializes those differently.

avatar image
3

Answer by dzliergaard · Feb 26, 2018 at 09:44 PM

For anyone still coming upon this answer, you could simplify your wrapper class by implementing the bracket accessor, so you can access the elements in the wrapped list the same as you would an actual List.

 public int this[int key]
 {
     get
     {
         return InnerList[key];
     }
     set
     {
         InnerList[key] = value;
     }
 }

Link

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
-1

Answer by rajeshranjan531 · Dec 11, 2018 at 05:55 AM

https://github.com/garettbass/UnityExtensions.ArrayDrawer

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 Bunny83 · Dec 11, 2018 at 11:13 AM 0
Share

This does not answer the question. Property drawers can't change what data is actually serialized. Nested lists / array simply can't be serialized by Unity and you need to use a wrapper. You just throw a link at us which seems to just provide a property drawer for reorderable lists. This is off topic.

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

21 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

Nested Lists "Add" method problem 1 Answer

Add elements to array/list in inspector? 1 Answer

Save Data from Editor Script, Serialize Nested Class List 1 Answer

List of ScriptableObjects lost on project reload 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