1
\$\begingroup\$

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?

\$\endgroup\$

5 Answers 5

3
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Why the downvote? I mentioned the Resources folder, which is the accepted answer. \$\endgroup\$
    – jonas
    Nov 30, 2012 at 12:15
0
\$\begingroup\$

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;
}
\$\endgroup\$
0
\$\begingroup\$

Just add XML in Unity create in the Game_Data folder when it builds the game exe, not Resource.

\$\endgroup\$
-3
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – jhocking
    Jan 21, 2014 at 21:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .