Opening an XML in Unity3D when the game is built

At the moment, my game can open up an XML file inside the editor when I run it. In my XMLReader.cs I'm loading in my file like so:

_xmlDocument.Load(Application.dataPath + "\\HV_Settings\\Settings.xml");


This class also deals with what the XML should do once it has been read in.

However, when I build the game and run the exe, this file isn't called. I know that I can store this file in the C drive, but I want to keep everything in one place so when I start to release what I'm working on, the user doesn't need to do anything.

Am I doing something silly which is causing the XML not to be read?

Instead of going for the manual copy solution, you can opt to go for the StreamingAssets route.

Create a folder within your Unity project named "StreamingAssets" and place your XML files in there. Upon build, Unity will then create a verbatim copy of this directory. Your XML files will be accessible at string path = Application.dataPath + "/StreamingAssets";

To load files on runtime you must use the Resources class. After placing your file inside a folder called "Resources" try something like this:

TextAsset propStoreTextAsset = (TextAsset)Resources.Load("resourcename", typeof(TextAsset));


Please note, that a TextAsset is not mutable.

• Why the downvote? I mentioned the Resources folder, which is the accepted answer. – jonas Nov 30 '12 at 12:15

StreamingAssets alternative (cross-plateform):

public static string BundleFilePath(string filename) {
string result;
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
result = Path.Combine(string.Format("file://{0}", Application.streamingAssetsPath), filename);
#elif UNITY_ANDROID
result = System.IO.Path.Combine(Application.streamingAssetsPath, filename);
#elif UNITY_IPHONE
result = System.IO.Path.Combine(Application.dataPath, string.Format("Raw/{0}", filename));
#elif UNITY_WEBPLAYER
result = System.IO.Path.Combine("StreamingAssets/", filename)); // untested
#endif
return result;
}


Found a work around:

When I build my project, I need to manually drag my XML file into the Resources folder Unity create in the Game_Data folder when it builds the exe.

• Technically this works, but it's quite easy to have Unity include the file automatically so that you don't have to remember to do this every build. – jhocking Jan 21 '14 at 21:14