Search Unity

No serialization?

Discussion in 'Windows' started by CazicThule, Aug 16, 2013.

  1. CazicThule

    CazicThule

    Joined:
    Nov 29, 2012
    Posts:
    95
    I'm getting errors when trying to build for Windows 8 WP8 regarding serialization

    Code (csharp):
    1. "The type or namespace name 'ISerializable" could not be found..."
    2.  
    3. "The type or namespace name 'Formatters" could not be found.."
    4.  
    5. "The type or namespace name 'SerializationInfo" could not be found..."
    Is this not supported in Unity Windows/Phone 8 or is something wrong?
     
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,905
    Those classes are indeed not available on Windows Store Apps/WP8.
     
  3. Deleted User

    Deleted User

    Guest

    Could you tell me how to solve it?If my project has a lot of "ISerializable" and "Serializable",may I must to change my Code?
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    It depends whether you use it for serialization or just have such attributes on classes. In the later case you can wrap those attributes with #if UNITY_METRO.
     
  5. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Aurimas is right, you'll need to wrap those to hide them from the build. It's done in my JSON .NET asset for serialization, but you can also look at the original JSON .NET source code as a reference:

    http://json.codeplex.com/

    Download the source package and take a look at what JNK did for Windows Phone and WinRT.
     
  6. Deleted User

    Deleted User

    Guest

    It also report "Error: type `System.Runtime.Serialization.SerializationInfo` doesn't exist in target framework. ",
     
  7. Deleted User

    Deleted User

    Guest

    Hello,because i use xml to save my scene,many problem show while build.The JSON.NET can simplily change it to json.If can ,how to do it?Thankes a lot!
     
  8. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    John -

    It won't automatically convert your XML to JSON if that's what you're asking but if you did want to use my asset you won't need the Serialization attributes. You can use them, but not necessary. For instance, if you have a class called "Person" and an instance of it in the variable "mainPerson" you would serialize and deserialize as follows:

    Code (csharp):
    1.  
    2.     //create new person instance
    3.     Person mainPerson = new Person();
    4.  
    5.     //serialize it.
    6.     string serialized = JsonConvert.SerializeObject(mainPerson);
    7.  
    8.  
    9.     //deserialize an already serialized person
    10.     Person newPerson = JsonConvert.DeserializeObject<Person>(serialized);
    11.  
    Bear in mind though that some things (like MonoBehaviors) can't be automatically serialized because you'll get issues for properties like rigidbody if one isn't assigned. Texture2D is another one that can cause issues because of the lack of a public parameterless constructor that will work with JSON .NET so you'll have to create custom serialization binders or proxy types.