• 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
3
Question by Asse · Jun 08, 2010 at 02:20 PM · serializefield

Serialize class with generic Dictionary that holds custom class objects

I just can't get this to work. I want to serialize a class that holds a generic Dictionary which holds custom class objects. Here's a piece of code to show what I mean.

public class EditorData : ScriptableObject {

 [System.Serializable]

 public class ObjectData
 {
     public SubObjectData[]      listSubObjectData           = null;
 }


 [System.Serializable]

 public class SubObjectData
 {
     public System.Reflection.FieldInfo[]    listFieldInfos  = null;
     public System.Object[]                  listValues      = null;
 }


 public System.Collections.Generic.Dictionary<System.String, ObjectData> m_listObjectData    = new System.Collections.Generic.Dictionary<System.String, ObjectData>( );

}

I tried serializing the dictionary of the EditorData object by myself using a BinaryFormatter but always got the error that the class System.Reflection.MonoField isn't marked as Serializable.

Then I tried saving the EditorData object as asset with AssetDatabase.CreateAsset, but when I restart Unity and load the asset, the dictionary is empty.

Any suggestions, hints, cake?

Comment
Add comment
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

1 Reply

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

Answer by Lucas Meijer 1 · Jun 08, 2010 at 09:43 PM

Unfortunattely Unity's built in serialization system doesn't support serializing System.Collections.Generic.Dictionary right now. As a workaround, you can store your data in two List<>'s one with keys and one with values, and "stich them together" in an Awake() function.

Check this post for more details: http://answers.unity3d.com/questions/1559/edit-and-continue-scripting-destroys-current-state/1583#1583

Comment
Add comment · Show 3 · 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 Asse · Jun 09, 2010 at 08:32 AM 0
Share

Thanks for the answer. I thought that Generic.Dictionary isn't serializable after some tests, at least not with own class types.

I've already restructured the design so I completely use Generic.List, I think that's more elegant :-)

avatar image SoundGuy32 · Jun 17, 2010 at 05:51 AM 0
Share

apparantly lists aren't serializable in unity iphone either:

any ideas?

error: (Filename: /Users/build/builds/unity-iphone/iphone/Projects/../Runtime/Export/Generated/BaseClass.cpp Line: 1719) ExecutionEngineException: Attempting to JIT compile method 'System.Reflection.$$anonymous$$onoProperty:GetterAdapterFrame (System.Reflection.$$anonymous$$onoProperty/Getter`2 ,object)' while running with --aot-only.

System.Xml.Serialization.XmlSerializer.Serialize (System.Xml.XmlWriter writer, System

avatar image Lucas Meijer 1 ♦♦ · Jun 17, 2010 at 07:44 AM 1
Share

I'm sorry my answer indeed is not correct for Unity iPhone 1.7. In iPhone1.7, you can only use built in arrays. This will be fixed in Unity3

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to expose a field of type Interface in the inspector? 10 Answers

Insert enum breaking existing inspector values 2 Answers

How can I define two-dimensional curves? (like: circle) 1 Answer

Is there any difference between [SerializeField] and a public variable? 1 Answer

How can i save objects to a file 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