• 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
2
Question by bearcano · Jan 25, 2011 at 09:12 AM · serializationsaveloadxmlstandalone

Saving and Loading XML in Standalone

In my game, I have an arbitrary number of XML files that are used to load in map data and populate the map. Additionally, I want to automatically save to XML changes that players make to the map.

I currently have this working in the editor mode, but I can't get it to work in the standalone version.

The way I am saving and loading XML currently is using the System.Xml method. Here's a sample of my code:

/* WORLD SAVE DATA */ public void SerializeData(WorldSaveData saveData) { Debug.Log("Saving world data..."); XmlSerializer serializer = new XmlSerializer(typeof(WorldSaveData)); TextWriter textWriter = new StreamWriter(Application.dataPath + "/data/world/worldsave.xml"); serializer.Serialize(textWriter, saveData); textWriter.Close();

 Debug.Log("Saved to: " + Application.dataPath + "/data/world/worldsave.xml");

}

public WorldSaveData DeserializeData(WorldSaveData saveData) { Debug.Log("Loading world data..."); XmlSerializer serializer = new XmlSerializer(typeof(WorldSaveData)); TextReader textReader = new StreamReader(Application.dataPath + "/data/world/worldsave.xml"); saveData = (WorldSaveData)serializer.Deserialize(textReader); textReader.Close();

 Debug.Log("Loaded world data successfully.");
 return saveData;

}

This is functional and works well for me. Looking around, I saw that in standalone versions you might have to use the Resources folders and Resources.Load, but doing so only gives me a TextAsset, which I am unable to convert into the StreamWriter/TextWriter format I'd like to use if at all possible. Also, in my searches around I was only able to find people discussing loading XML data, but I need to ensure I can save data as well.

Hopefully some kind folks around here can help! Thanks for reading my question.

Comment
Add comment · Show 1
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 yoyo · Jan 25, 2011 at 04:55 PM 0
Share

System.Xml (and System.IO) should work in a standalone build. Note that when you create a build, only files referenced by your scene will be included in the data for the build, plus any files under a "Resources" folder. This is potentially why you can't load the data in a standalone build -- it may not be there. Deter$$anonymous$$e where Application.dataPath points to in your standalone build, and see what files are present. (For reading you could use TextAsset.text, and open it with a StringReader, but that doesn't solve your saving problem.)

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by bearcano · Jan 25, 2011 at 10:51 PM

This isn't exactly a great "answer" to the question as much as a workaround, but here's what I've found:

  • You can't put the data into the "Resources" folder prior to building, due to what Unity does to it.
  • Each application.dataPath is different based on the target: http://unity3d.com/support/documentation/ScriptReference/Application-dataPath.html

So the workaround I'm using now is this:

  1. Copy the source folder for my xml files (in my case above, the entire "data" folder)
  2. Paste it into the dataPath folder for the build (in Windows, it's applicationName_Data)

I've found that it works in this way, even if it's not the most ideal solution. I'd still be happy to hear other suggestions.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Saving Game Problem 1 Answer

Save and Load from XML U3 Collections Problem 1 Answer

Coding help: How to use xml serialization 1 Answer

Easy way to save rigidbody state. 1 Answer

Save/Load system is saving but not loading. 1 Answer


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