Zanimo
Zanimo

Reputation: 53

How to load XML file on iPhone using Unity?

I want to load XML files to generate levels of my game but on iPhone (or Android) it crash but on the editor it works fine.

Here is how I try to load my XML:

XmlDocument xml = new XmlDocument();
xml.Load("Assets/XML/level1.xml");
level_root_node = xml.DocumentElement;

Please someone have already solved similar issues ? I've tried to put my XMLs into "Assets/Resources/XML/level1.xml" but it doesn't work better.

Upvotes: 0

Views: 1435

Answers (1)

peterept
peterept

Reputation: 4427

The reason it fails is that on Android the resources are inside the APK file and can't be access directly as you are attempting.

http://docs.unity3d.com/Manual/LoadingResourcesatRuntime.html

You can use Resources.Load (path) to access a resource in a platform independent way.

Using Resources.Load() to load your XML asset, convert this to a string and pass it to the XMLDocument for parsing:

TextAsset textAsset = Resources.Load ("XML/level1.xml") as TextAsset;
xml.LoadXml(textAsset.text);
level_root_node = xml.DocumentElement;

Upvotes: 1

Related Questions