x


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 http://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

more ▼

asked Apr 02 '12 at 07:30 PM

Ordosx gravatar image

Ordosx
5 5 9 11

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

Nov 19 '12 at 12:42 PM Berenger
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
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.

more ▼

answered Apr 02 '12 at 07:45 PM

rutter gravatar image

rutter
5.2k 2 11

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Apr 02 '12 at 11:51 PM

Ordosx gravatar image

Ordosx
5 5 9 11

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

Apr 03 '12 at 12:01 AM rutter
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x419
x256

asked: Apr 02 '12 at 07:30 PM

Seen: 1062 times

Last Updated: Nov 19 '12 at 12:42 PM