- Home /
How can I read binary files from Resources
I am trying to read a binary file from the resources. To do this I call the Resources.Load(..) function getting a TextAsset. Since here everything clear (or maybe not...). But then how can I open it to read? I tried all combinations to be able to read it with the BinaryReader tool but no way. Anybody knows the exact combination?
Answer by barbur · Dec 01, 2009 at 08:42 AM
I already solved this issue. The problem was that the binary file had to have the .txt extension to be able to read it as a TextAsset :)
TextAsset asset = Resources.Load("enemy_seq_bin") as TextAsset;
Stream s = new MemoryStream(asset.bytes);
BinaryReader br = new BinaryReader(s);
Answering your question, the reason to have that file in binary is to speed up the resources loading.
Isn't it ironical that the way to store a BINARY asset in Unity is by using a class called TEXTAsset?
This will not work for all binary assets due to encoding rules applied to the text when loaded. Try doing that code on a JPG image. It will corrupt the data.
That's not true. "asset.bytes" will return the raw bytes of the file so nothing gets corrupted when you read the bytes property. Of course you shouldn't use the "text" property of the TextAsset as it will encode the bytes as text and return a string.
The file extension doesn't dictate any kind of encoding, it's just part of the filename. Of course you shouldn't "open" or "edit" a JPG file in a text editor.
The TextAsset now supports other extensions as well. For arbitrary binary data you can use the ".bytes" extension. However as i said it doesn't matter what extension the file has as long as it's recognised as TextAsset by Unity.
I ran a test case that showed the bytes were different, but I no longer have the code as it was in my test case. There must be something different that I missed then.
Either way, I ended up going with this solution: http://answers.unity3d.com/questions/591545/not-able-to-load-binary-file-through-resourcesload.html
Running Unity 2020.3.26 and this worked great. Runs in Editor and Build. Thanks. I never would have guest ...
Answer by caroparo · Mar 01, 2012 at 04:01 AM
Fun fact I noticed from another answer is that you have to use ".bytes" extension instead of ".txt" for a real binary asset to be loaded with full length. Ref: http://unity3d.com/support/documentation/Components/class-TextAsset.html
Worst. Documentation. Ever.
Seriously, what do they expect me to do? Read every single doc page until I happen to come across the way Unity needs me to do things?
Anyway, thanks to you both for this answer -- you saved me a lot of grief.
That's it. Thank you for pointing out this documentation
Answer by eppz · Sep 09, 2016 at 08:34 PM
Rename binary files to use bytes extension, then something like:
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
TextAsset textAsset = Resources.Load(fileNameWithoutExtension) as TextAsset;
Stream stream = new MemoryStream(textAsset.bytes);
BinaryFormatter formatter = new BinaryFormatter();
MyClass myInstance = formatter.Deserialize(stream) as MyClass;
Then simply may wrap it all into a factory method, like:
public static MyClass LoadFromResources(string fileName)
{ ... }
Answer by unity-marcus · Oct 06, 2019 at 06:29 PM
Since I just had the same problem even though this is a quite old topic:
If you cannot rename the files anymore, because it would mean a huge architectural change, you can consider reencoding them "correctly".
string utf8String = Encoding.UTF8.GetString(textAsset.bytes);
bytes = Encoding.ASCII.GetBytes(utf8String);
Your answer
Follow this Question
Related Questions
AI Programing Resources 9 Answers
How to identify/get the file name of resources loaded with Resources.LoadAll 2 Answers
iPhone build doesn't copy existing files in Application.dataPath or .persistantDataPath?! 0 Answers
how do I close unity - and release resources 0 Answers
How can I load textures, sounds, and prefabs from users hard disk? 2 Answers