Unity xml/Resource Management javascript tutorial
I have been busy working on my isometric game recently and i have stumbled uppon a problem that is not again easy to find answer for. As it stand in the Unity3d Forum, i have not seen a splendid tutorial on how to make XML reading a snap with resources. Also i happen to had to refactor my entire solution to make certain resource loading more dynamic and in line with the Unity3d mindset. This tutorial is split into two : first is how to set the resource dynamic loading properly in Unity3d, and then use that to load an xml file on the fly in javascript intuitively.
STEP 1 ) Dynamic resource loading
In the Unity3d documentation pertaining to resource management (found here) it is said : “All assets that are in a folder named “Resources” anywhere in the Assets folder can be accessed via the Resources.Load functions.”.
How the resource folder works in practice
As one of my reader noted : “Using absolute path names when running the game in the editor works perfectly, but when you compile the game nothing works…”. This seems to be because everything in the Resouces folder is copied directly in the output folder “/Resources” in your Unity3d build output.
The solution to data management for a Unity3d project seems to be simple. Instead of having a single “/Resources” folder, you can actually have multiple folders, each containing a “/Resources” folder such as the following :
- Assets/Textures/Resources
- Assets/Models/Resources
- Assets/Level1/Sounds/Resources
WARNING : You cannot have two resource/file with the same name. For example : blue.png and blue.jpeg get the same name from the Resources Manager. This is because when you use Resources.Load(“stuff”) you do not put any file extension. This means Unity3d will tag your resources with the name before the file extension. THIS MEANS YOU HAVE TO HAVE A FILE NOMENCLATURE, PREVENTING DUPLICATE FILE NAMES.
Ok now you have a folder in your root asset folder so the structure would resemble something like : <myprojectfolder>/Assets/Resources. Create a simple text file put the following text inside and rename it “xmltest.xml” :
<?xml version=”1.0″ encoding=”utf-8″ ?>
<system>
<item1 id=”1″ label=”hello”/>
</system>
Simple enough
you should then make sure this file is in the /Resources folder. It should go into view in the Project pane and show up as “xmtest” without the .xml extention with a nice text icon.
STEP 2) Javascript Code to make the magic !
First of all, since by default the Unity3d API do not have the XML support, we need to load it from the C# Mono assemblies. This is done writing the following at the top of the javascript file :
import System.Xml;
import System.IO;
The code is pretty trivial in itself and should you need further help for the XML classes see the Microsoft links bellow the code and file download.
var asset:TextAsset = Resources.Load("testxml");
if(asset != null)
{
var reader:XmlTextReader = new XmlTextReader(new StringReader(asset.text));
while(reader.Read())
{
if(reader.Name == "item1")
{
Debug.Log(reader.Name + " label = " + reader.GetAttribute("label"));
}
}
}
file can be found here
TextAsset is used by Unity to load a file in raw or text form, you could invent your own text loader for speed purpose, but I highly recommend that you prototype your system first and do such thing before releasing your game. It is to note also that some people seem to have had problem with XML assemblies on the iPhone, i think it is simply impossible to successfully build your Unity3d application with Mono assembly references that are not supported by the Unity3d SDK you are using for the iPhone.
References for XML (Microsoft .NET links) and Unity3d help
- http://msdn.microsoft.com/en-us/library/system.xml.xmltextreader.aspx
- Tutorials for XMLTextReader
- http://msdn.microsoft.com/en-us/library/system.io.stringreader.aspx
- Unity3d TextAsset
- Unity3d Forum Help
excellent article, is just what I was looking for, concise and direct, thank you very much.
Sir i have a query…i made an xml file in the “Assets/Resources” folder and created a js file in the Assets folder as well, now what next ?? shall i assign the js script to plane ?? what to do ? now when i’l compile it, what will happen coz i wana change the text inside the xml dynamically..
please guide
just to elaborate… i am able to see Hello in my debug log….i want that after compiling i can change the text in xml….how is this possible ? after this is possible i wana show this text in the scene..plz guide
Change the “label” tag from Hello to whatever you want in the XML file :
You should check the methods exposed by the System.Xml assembly