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

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

Problem with path in FileStream 2 Answers

Loading XML asset after build 0 Answers

Xml Serialization of "sub classes" 0 Answers

URGENT - Serializing error The type of argument is not primitive 0 Answers

[SAVING] LIST cannot be serialized because it does not have a default public constructor 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