- Home /
Load issues
Hi!,
I'm trying to load a XML file and so far I got it, but I get the infamous "text node can't appear in this state". Something to do with the BOM encoding.
Anyways, I tried this:
//--------------------------------------------------------------------------
import System.Xml;
import System.IO;
//--------------------------------------------------------------------------
function GetTextWithoutBOM(textAsset : TextAsset) {
var memoryStream : MemoryStream = new MemoryStream(textAsset.bytes);
var streamReader : StreamReader = new StreamReader(memoryStream, true);
var result : String = streamReader.ReadToEnd();
streamReader.Close();
memoryStream.Close();
return result;
}
//--------------------------------------------------------------------------
function loadLevel(archXML : String){
var xmlDoc : XmlDocument = new XmlDocument();
var ta : TextAsset = Resources.Load("/Assets/Materials/XML/Levels/10/test.xml");
Debug.Log(ta); // --- Returns NULL always :(
var noBom: String = GetTextWithoutBOM(ta);
xmlDoc.LoadXml(noBom);
...
//--------------------------------------------------------------------------
But now I geting an "Object reference not set to an instance of an object" error, and I realize with the "Debug.Log" line that "Resources.Load" never load the file (it's null always).
But, I know that the path to the file is Ok, because, when I use just this:
xmlDoc.LoadXml("test.xml"); - or - xmlDoc.LoadXml("/Assets/Materials/XML/Levels/10/test.xml");
It works fine but I get the "Text node canot appear in this state" error in the first character.
What I'm doing wrong?? I'm stuck.
Thanks
sudha says : Hai,my problem is that i'm unable to retrieve data through xml for webbuild but if i run the same in unity editor it's building successfully...need help...Thanks in advance
Answer by rutter · Apr 02, 2012 at 07:45 PM
var ta : TextAsset = Resources.Load("/Assets/Materials/XML/Levels/10/test.xml");Z
Debug.Log(ta); // --- Returns NULL always :(
If your text asset is null, I'm not sure why you would expect anything after that to work.
According to Unity's script reference, `Resources.Load()` takes an asset name only, as opposed to a complete path. If you want to load a TextAsset named "Foo.txt", you need to place it in a folder named "Resources" (under any path in your assets folder) and make a call like this:
var foo : TextAsset = Resources.Load("Foo");
There's a little bit more explanation here. If you're used to specifying paths directly, this can seem like a bit of a hurdle, but setting this up carefully can make it much easier to port your game to other platforms, if that's something you have in mind.
From there, Unity might expect that a text asset's file extension is "txt", but you're welcome to experiment with that on your own.
Answer by Ordosx · Apr 02, 2012 at 11:51 PM
I see, doh!, I checked the script reference but didn't realize that the folder should be named Resources, I just thought it was an example.
Anyways, I want to place a huge number of text files there, so I would like to have my folders a bit more organized, but it doesn't seem possible unless I use "LoadAssetAtPath", that doesn't work in standalone or web players.
So this raises another question: is it possible to load textAssets with "Resources.Load" that is inside a subfolder that is located within the "Resources" folder? Example: text file is in: Assets/Resources/Levels/test.txt Is there a way to load that file other than using "LoadAssetAtPath"?. I tried with Resources.Load and it didn't work.
Thanks
You can have any number of resource folders, organized however you want, as long as they're all named "Resources". Just remember that you're ultimately loading them all by name and that, as a result, any dynamically loaded asset will need a unique name.
If we suppose you have a bunch of maps in folders like "Chapter1", "Chapter2", and so on; you could create a "Resources" folder in each of those groups. Not especially clean, but it might be a little cleaner than dumping all of them in one folder.
Or, you could use some sort of alphabetical scheme. A file named "maps" could be named "ch01_maps".
Your answer
Follow this Question
Related Questions
Refresh assets during build. 1 Answer
XmlException: Text node canot appear in this state 5 Answers
Loading XML asset after build 0 Answers
A node in a childnode? 1 Answer
How to refresh assets during run time of a build ? 0 Answers