- Home /
Load binary files from Resources
I am trying to load some binary files from my Resources folder as TextAsset.
Currently I am doing this:
TextAsset ta = Resources.Load("myAsset.bytes", typeof(TextAsset)) as TextAsset;
byte[] bytes = ta.bytes;
However this doesn't seem to work correctly. I have a file that is supposed to be 176kb long, but bytes.Length reports it's only 85524 bytes long. Also I found out using a hex editor, that when parsing the file the first byte of the file is not read. Instead it starts with the second byte. I have another file that is a lot shorter (~36kb), which is read correctly with the same code.
So I'm wondering what might cause this behaviour. Is there a limit on how big a TextAsset can be? Or is there maybe an error because of non-ASCII character stripping of the importer? I thought I dealt with that problem by naming the file myAsset.bytes.
Answer by Eric5h5 · Oct 31, 2010 at 04:08 PM
Unity 2.6 cannot use TextAsset.bytes correctly; you need to use Unity 3. (It works fine in Unity iPhone 1.7 though.)
@spree: Works fine here. Note that you need to use ".bytes" and not ".txt" for the extension. I don't think you can use "myAsset.bytes" in Resources.Load, but just should use "myAsset" (whatever extension it has is ignored).
It was a weird coincidence, that misled me. I changed the extension from .txt to .bytes after I read the entry in the docs. Somehow that didn't fix it. But rena$$anonymous$$g the file to myAsset.bytes before importing it into Unity and then using Resources.Load("myAsset") did work. Thanks.