Reading from XML Works in Edit but not in Build?

I'm reading game configuration parameters from an XML file that I created. I made a folder called "Resources" in my project's Assets folder, and placed the XML file in it. When working in Edit Mode, I can modify the XML parameters, run the game, and observe the changes that I have made. However, when I build and release it, editing the XML file does nothing.

I'm building as a Windows exe.

Is it a linker problem?

As a reference, here is the code that I'm using (yes, it's rough, but I just wanted to get a proof of concept working)

var asset:TextAsset = Resources.Load("xmltest");
if(asset != null)
{
    var reader:XmlTextReader = new XmlTextReader(new StringReader(asset.text));
    while(reader.Read())
    {
        if(reader.Name == "item")
        {
            if (reader.GetAttribute("id") == "editedJump")
            {
                jump.extraHeight = parseInt(reader.GetAttribute("label"));
            }

            if (reader.GetAttribute("id") == "editedWalk")
            {
                movement.walkSpeed = parseInt(reader.GetAttribute("label"));
            }

        }
    }
}

1 Answer

1

[EDIT]

Everything in the Resources folder will get included in the build, it isn't a link to that file you are getting. So modifying the file in your project will only affect that copy, not the copy included in your game. To get a file which can be editable after you have compiled the game you will have to load the file from a path (see the [Old Stuff]) by using for example the System.IO.File class (google), to create a System.IO.FileStream which can be used to create a XMLReader by new XMLReader (myFileStream). Or you can use Unity's WWW classes

[Old Stuff] Check the player log too see what kind of error you are getting (I suppose you are getting something). You can access the player log by opening the Console window in Unity and clicking on the "Open Player Log" button in the top-right corner of that tab/window.

The error might come from what directory the application assumes is the "local" directory. To get it more stable you can construct the path with variables from the Application class. Especially Application.dataPath