• 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
Question by $$anonymous$$ · Aug 08, 2014 at 06:44 PM · serializationscriptableobjectxmlxmlserializer

ScriptableObject+XmlSerializer=sad

Hello friends, I've been banging my head in the wall for a couple of hours with this:

I am making an editor extension and I have that Node class which has a lot of children classes. I also have a List with all the nodes. At first I had the problem that this list didn't survive through serialization (when I hit play all the children specific data was lost, including their Type). I read a lot of crap about Serialization and I finally managed to fix it by simply making the Node class driven by ScriptableObject. Though that ended up coming at a huge cost, because that means I can't create nodes with new Node(), but with CreateInstance. For me that's acceptable, but for my buddy - the XmlSerializer it isn't, since who am I to tell him how to Deserialize his objects...

Long story short - XmlSerializer uses the Node() constructor and ScriptableObject doesn't like that so I'm looking for a way to make my Nodes list survive 'assembly reload' or whatever happens when I hit play without the making them ScriptableObjects.

Things I can't do:

  • Ain't no way I'm replacing XmlSerializer because I'm inlove with it

  • Also I can't make multiple Lists for each type of nodes because that would fuc...screw up the entire code logic (which is a lot of junk to fix, believe me)

  • I also really don't want to create another Node class that won't be driven by SO and also keep the old one for editor purposes

I'm aware that this question has kind of been asked before about 50 times but I didn't find an acceptable solution. Also it's worth mentioning that everything related to my editor extension is in a custom namespace if that makes any difference.

Please help me if you know a solution, I really am stuck. Also I need to get some sleep, I'll check back here tomorrow.

Thanks in advance guys

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by rutter · Aug 08, 2014 at 06:55 PM

Unity's serialization isn't polymorphic at all. It's usually fine, but once you end up with a fairly experienced programmer who expects to be able to use all of their OOP skills, it can be a harsh gotcha.

If you can serialize to XML, it might be easier to just do that and have Unity keep track of a string. Every time you make a change in the editor? Write out the XML. Every time you play the game? Load in the XML. You can even use lazy instantiation principles to make sure the data is always available when it's needed.

Depending on your specific needs, this string could be kept in a TextAsset, a ScriptableObject, or just a serialized field in some component.

It's fine to use custom serialization, as long as you do it in a way that plays nicely with Unity -- usually that means serializing out primitive types, like strings, byte arrays, and so on. You can also use resources and streaming assets if you're in a pinch.

The only Unity-specific part of this question is getting Unity to handle your data. After that, it's important to remember that you have the full power of the .NET framework at your fingertips -- anything from XML to JSON to SQL is completely accessible, just as long as you can handle the data.

Comment
Bunny83

People who like this

1 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 $$anonymous$$ · Aug 09, 2014 at 07:20 AM 0
Share

Thanks for the reply. I know that serializing and deserializing each time something happens is a possibility, it just felt like there must be a sexier fix. Probably it's because the ScriptableObject thing was so straight forward. The alternatives just don't feel right. Whatever, I'll give it a try and see if it'll make me change my mind

avatar image

Answer by $$anonymous$$ · Aug 10, 2014 at 11:00 AM

I just found that:

ISerializationCallbackReceiver

How have I been so blind not knowing about its existance. It does wonders, allowing to quickly load and save the data and even dodge making my classes serializable while Unity does its stuff.

Comment

People who like this

0 Show 0 · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Unity Serialization with XML Root problem: 'Does Not Denote Valid Type' 0 Answers

DataContract serialization not compatible with Unity? 0 Answers

[SOLVED] XML Deserialization of a single XML file into multiple objects 2 Answers

ArgumentException: Path is empty while saving data in an XML 0 Answers

What are the pros and cons of ScriptableObjects vs. JSON for data files? 2 Answers


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