I have an xml file that stores dialog used for my game. This file is stored inside of the resources folder and is read in by specifying the exact path name: "XmlData\strings"
This works just fine when running through the editor, but when I try to run after I built the project, it doesn't work.
I have found a few solutions that say to append .dll to the .xml file and load it in using dllimport, but I still can't get this to work. Does anyone have a good solution for reading in this .xml file from the built resources directory?
The solution is to use Ressources.Load(filename without extention)
e.g. to load your XML file from inside the Ressource Folder
// if your original XML file is located at
// "Ressources/MyXMLFile.xml"
TextAsset textAsset = (TextAsset) Resources.Load("MyXMLFile");
XmlDocument xmldoc = new XmlDocument ();
xmldoc.LoadXml ( textAsset.text );
Not really a solution to read from the built resource ( I am not sure if you actually know the difference between the Resource folder and the project resources that are made available automatically by unity).
Instead of storing it in the Resource folder or anything fancy, simply declare a public var in your script interested by this xml file:
public var xmlDefinition:TextAsset;
And in the inspector, simply drag and drop the xml file onto the "xmlDefinition" field now available.
Then it will be included during publishing now.
If I misunderstood your problem, can you elaborate more on how you actually have it to work within the editor? what function to you use to read the xml.
return xmlDoc;
}
public static void writeXml(string filepath, XmlDocument xmlDoc)
{
if (File.Exists(filepath))
{
using (TextWriter sw = new StreamWriter(filepath, false, System.Text.Encoding.UTF8)) //Set encoding
{
xmlDoc.Save(sw);
}
}
}
}
Keep in mind that you can only write to the text asset in edit mode - AND that the xml file has to be in resources - so you can load it at runtime…
here a short example how to use it:
After the build, put that folder as you said: Resources \ XmlData \ strings in the same directory as the executable or does not work inside the folder: name of the game _data
Hope this helps ..
Please could you pass me the script to read information from an XML? and how to tell the GUI that will read a specific line of XML?
I’m not 100% sure what your problem is but If what you want to do is load in an xml file after build than you just need to use Application.datapath + “filename.xml”. Application.datapath will return the path to the assets folder while in the Editor and the path to the data folder when running a windows build. Also will get you the correct path on whatever other device you build it on.
Resource Folders must be added in the editor and can’t be added after build time.
There’s already a working solution, but you might want to try using Unity’s XML serializer, because it lets you access XML data much easier.
It let’s you load an XML as if you were loading an object, and you can directly access the nodes in your XML like a normal C# variable.