Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by barbur · Nov 23, 2009 at 05:06 PM · resources

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
20
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Marzoa · Jan 07, 2015 at 01:20 PM 2
Share

Isn't it ironical that the way to store a BINARY asset in Unity is by using a class called TEXTAsset?

avatar image uraLebowski · Jun 29, 2016 at 01:22 AM 1
Share

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.

avatar image Bunny83 uraLebowski · Jun 29, 2016 at 02:08 AM 0
Share

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.

avatar image uraLebowski Bunny83 · Jun 29, 2016 at 02:52 AM 0
Share

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

avatar image tlskillman · Apr 14 at 06:05 AM 0
Share

Running Unity 2020.3.26 and this worked great. Runs in Editor and Build. Thanks. I never would have guest ...

avatar image
14

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MikeTea · Jul 06, 2013 at 05:47 PM 1
Share

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.

avatar image WorldManager · Nov 25, 2013 at 07:57 AM 0
Share

That's it. Thank you for pointing out this documentation

avatar image
3

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)
 { ... }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Quartsik · Jun 26, 2019 at 02:37 PM 0
Share

This worked perfectly for me.

avatar image
2

Answer by Eruure · Sep 09, 2013 at 02:52 AM

By the way, there's a way for the Editor to do the renaming automatically for you (useful if you e.g. use a different editor to edit this files).

See this post for more info.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gilead7 · Apr 02, 2020 at 04:55 PM 0
Share

What if your binary file contains a list?

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges