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
0
Question by MattSawrey · Jul 02, 2015 at 08:13 PM · serializationxmlsavingmonobehaviour

Questions about XML serialization of Class Properties

I am currently teaching myself how to save data through C# in Unity using methods other than PlayerPrefs. I decided to start with XML. I first tried to serialiaze a number of properties within my TimeController class, which inherited from Monobehaviour. I then learned that you cannot serialize classes that inherit from monobehaviour. I then created a separate class to read the properties of the original class (TimeSaveData) that doesn't inherit from monobehaviour. I then learned that you cannot serialize a class that has a public constructor, so I removed the TimeSaveData class' public constructor and assigned the values of the properties manually

I now have the following approach:

 //original class
 public class TimeController : MonoBehaviour 
 {    
     //the properties I want to save
     public int igCurrentTime{get; set;}    
     public int igTotalNumDaysPassed {get; set;}
     public int rwTimeOfSave{get; set;}
     
     public void SaveTimes()
     {
         //read real world time in minutes
         rwTimeOfSave = DateTime.Now.Minute;
         
         //Instance the TimeSaveData class and transfer the values of all properties
         TimeSaveData saveData = new TimeSaveData();
         saveData.igCurrentTime = igCurrentTime;
         saveData.igTotalNumDaysPassed = igTotalNumDaysPassed;
         saveData.rwTimeOfSave = rwTimeOfSave;
         
         //I have a seperate class with a generic method for creating XML save files
         SaveLoad.SaveFile(Path.DirectorySeparatorChar + this.ToString(), saveData);
     }
 }
 
 //Class created as a holder for all the properties I want to serialize
 [XmlRoot]
 public class TimeSaveData
 {
     [XmlElement]
     public int igCurrentTime{get; set;}
     [XmlElement]
     public int igTotalNumDaysPassed {get; set;}
     [XmlElement]
     public int rwTimeOfSave{get; set;}
 }

This seems like a cumbersome way of saving data. Am I approaching XML serialization the wrong way, or is the inability to serialize properties from classes that inherit from Monobehaviour just something that is accepted? If so, are there any better/more efficient methods for me to do what I'm trying to do?

Thanks for any responses in advance.

Matt

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
0

Answer by vuwij · Oct 28, 2016 at 07:51 AM

Use the DataContractSerializer instead https://msdn.microsoft.com/en-us/library/6exf3h2k(v=vs.110).aspx According to this page, DataContractSerializer is better than XmlSerializer as it doesn't run into as many problems as XML serialization

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

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

22 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 avatar image

Related Questions

Storing list aka "save function" 3 Answers

Trying to XML serialize objects, getting error that UnityEngine.Transform doesn't implement Add() 1 Answer

Need experienced opinion: Saving a players inventory: XML or PlayerPrefs? 1 Answer

Saving serializable class list at runtime. 1 Answer

Serializing GameObject Data with XML, Adaptation 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