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
3
Question by nikomikulicic · Mar 16, 2015 at 01:38 PM · serializationxmlxmlserializer

DataContract serialization not compatible with Unity?

Hi everyone,

I've ran into couple of problems using DataContractSerializer within Unity. After many hours of debugging I've decided to copy the classes I'm trying to serialize/deserialize to Visual Studio solution and try it without Unity. I used target framework "Unity .Net 3.5 Subset Base Class Libraries".

These are things I found not working well within Unity while Visual Studio solution worked perfectly fine:

  • Visual Studio had no problems deserializing empty array while Unity kept throwing ArgumentException: Object type System.Collections.ArrayList cannot be converted to target type: MyNamespace.MyType[]

  • Circular references where handled nicely within Visual Studio with DataContract(IsReference=true). Unity kept throwing SerializationException: Deserialized object with reference Id 'i1' was not found

  • Methods with attached attributes OnDeserialized, OnSerialized, OnSerializing, OnDeserializing were not called within Unity

I was hoping to convert empty list to null before serialization and set circular references after deserialization in callback functions but it turned out to be a problem itself.

I chose DataContractSerializer instead of XmlSerializer because DataContractSerializer can cope with circular references and dictionaries which XmlSerializer supposedly can't.

So, my question is simply this: Are there known difficulties with using DataContractSerializer within Unity or is this just me not knowing something simple? Which serializer is preferred in Unity to save data to Xml? What's with these callback methods not being called? I'm quite new in serialization so any information would be helpful.

Comment
Add comment · Show 3
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 Polymo · Apr 24, 2015 at 01:18 PM 0
Share

I'm definitely not an expert, but i'm using the DataContractSerializer too (for a dialog system). I also had many problems, but most were either related to deserializing unity types which i had to replace or fixed by using the $$anonymous$$nownTypes option. i only use the generic list, which works for me and had no problems with circular references. Apart from that you could also have a look at unity's ISerializationCallbackReceiver interface, depending on what you want to do.

avatar image nikomikulicic · Apr 24, 2015 at 09:07 PM 0
Share

Thank you for your comment.

I also used $$anonymous$$nownTypes to specify concrete types in my class hierarchy and I was serializing only classes that I wrote. Interesting point is that my code worked fine in Visual Studio so I can't say I was doing something wrong. It just didn't work in Unity and kept throwing errors I wrote above.

What just came to my $$anonymous$$d now is that I tried to serialize a hierarchy that was not derived from $$anonymous$$onoBehaviour nor ScriptableObject. Of course Visual Studio had no problems with that, but maybe Unity had. $$anonymous$$aybe Unity can serialize/deserialize a single class not derived from $$anonymous$$onoBehaviour/ScriptableObject but has a problem with polymorphic serialization of hierarchy that was not derived from $$anonymous$$onoBehaviour or ScriptableObject... Do you have any experience with that kind of serialization in Unity?

ISerializationCallback is called on every attempt of serialization or just serialization with Unitys built-in serializer?

avatar image Geebles nikomikulicic · Sep 26, 2015 at 12:08 PM 0
Share

Have you had any luck with this? I have a similar problem. I wrote a very simple test script for the serialiser:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Serialization;
 
 public class SerializerTest : $$anonymous$$onoBehaviour
 {
     public string added = "";
     [Context$$anonymous$$enu("Save")]
     void Save()
     {
         TestData data = new TestData();
         for (int i = 0; i < 2; i++) { data.test[i] = new TestClass(); }
         data.test[0][0] = data.test[1];
         data.test[1][0] = data.test[0];
         Serializer.Save<TestData>(gameObject.name+added, data);
     }
     [Context$$anonymous$$enu("Load")]
     void Load() { TestData data = Serializer.Load<TestData>(gameObject.name+added); }
 }
 [DataContract(IsReference = true)]
 public class TestClass
 {
     public TestClass this[int i] { get { return test[i]; } set { test[i] = value; } }
     [Data$$anonymous$$ember] public TestClass[] test = new TestClass[2];
 }
 [DataContract]
 public class TestData
 {
     [Data$$anonymous$$ember] public TestClass test = new TestClass();
 }

For some reason it serialises and deserialises perfectly on windows within unity. But when I try exactly the same code on a mac within unity I get an exception on deserialisation:

SerializationException: Deserialized object with reference Id 'i1' was not found

It feels like an issue with unity's $$anonymous$$ono/.NET implementation.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Problems with the XML Serializer 1 Answer

"Root Element Missing: XML Exception" when trying to load data from XML file 2 Answers

Xml Serialization of "sub classes" 0 Answers

URGENT - Cannot be serialized because it does not have a default public constructor.... 1 Answer

Unity Serialization with XML Root problem: 'Does Not Denote Valid Type' 0 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