• 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 NeatWolf · Feb 27, 2015 at 11:03 PM · serializationxmlresources

Serializing and Deserializing from/to XML inside Resources folder

Hi everyone,

I'm using a XML file to save all data about my levels in the game, and I'd like to include it automatically on build. So I was thinking to store the XML inside the Resources folder.

Fact is, the sample version of the XML serialize and deserialize functions use a filepath and a FileStream to Serialize/Deserialize the XML, while I need something like Resources.Load("leveldata.xml") and... something else to save the data in the same position.

Here's part of the code I'm using to serialize the content I need

             string filepath = Application.dataPath + @"/leveldata.xml";
 
             XmlSerializer serializer = new XmlSerializer(typeof(Levels));
             FileStream stream = new FileStream(filepath, FileMode.Create);
             serializer.Serialize(stream, _ls);
             stream.Close();

And here's the code I'm using to deserialize it:

             string filepath = Application.dataPath + @"/leveldata.xml";
 
             XmlSerializer serializer = new XmlSerializer(typeof(Levels));
             FileStream stream = new FileStream(filepath, FileMode.Open);
             levels = serializer.Deserialize(stream) as Levels;
             stream.Close();

What should I change to obtain what I'd like? Do you think there would be a better solution to save/load a bunch of levels to a XML file at runtime?

I'd like a cross/platform solution, that's why I'm thinking about the Resources folder.

Thank you for your time! ;)

EDIT: I forgot to mention that I'll be doing the editing from within the editor while using Play, but of course the final multi-platform build will have to access the Resources version of the file in another way, could you please point me to the right direction? Thank you!

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

1 Reply

  • Sort: 
avatar image

Answer by NeatWolf · Feb 27, 2015 at 11:38 PM

I'm currently using:

             TextAsset _xml = Resources.Load<TextAsset>("Match3/leveldata");
 
             XmlSerializer serializer = new XmlSerializer(typeof(Levels));
             StringReader reader = new StringReader(_xml.ToString());
             levels = serializer.Deserialize(reader) as Levels;
             reader.Close();

Is there a better way to do it?

I'm also forcing the refresh of the assets on save by using UnityEditor.AssetDatabase.Refresh();

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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Saving and Loading XML in Standalone 1 Answer

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

[SCREENSHOTS]Save scenes using XML/json descriptor 0 Answers

Can gameobject be exported as unity3d form? 0 Answers

Why does my vector3 inside a custom class not serialize? 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