- Home /
Loading .bytes resource as TextAsset to get byte[]
I have a number of files that I need to embed into my application without modification (target platform is Android). I have Unity Pro.
At runtime the files need to be transferred to another device that I connect to using Bluetooth.
The files contain raw graphics in a custom format - the format is quite close to tiff but not quite tiff. I have no use for anything but the raw content of the file as a byte array.
I have 129 of these files - I have renamed the extension for all of them to .bytes (which should be supported by TextAsset) and put them in a Resources folder inside my Assets folder and load them using Resources.Load(path).
This works for 59 of them - they come back as TextAsset from which I can get the content of the resource as byte[]. But the remaining files I get as Texture2D??
According to the online manual Texture2D has a GetRawTextureData that should give me the byte array - but sadly this method is NOT available in Unity 4.6.7 and gives me a compile error if I try to use it. http://docs.unity3d.com/ScriptReference/Texture2D.GetRawTextureData.html
I don't ever want my files back as Texture2D - is there a way to force Resources.Load to give me a TextAsset?
Is there a backwards compatible way of getting the byte data for a Texture2D?
I only need the byte[] with the file contents - so other ways of doing just that for a Resource is most welcome.
Showing the actual code you use for this could be helpful. (depending on the size perhaps also the file in question, so that people can debug it themself).
I would say using something like this might work.
Resources.Load<TextAsset>("path");
Or this, which does besically the same but you'll still need to manually cast it to TextAsset:
Resources.Load("path", typeof(TextAsset));
Although I'm not sure what Unity does internally, so both might also return null when Unity can't cast the file which he thinks is a Texture2D to a TextAsset...
Thank you @troien - Both your suggestions work - I was pretty sure that I had tried tried your first suggestion (and was unaware of the syntax for the 2nd suggestion) - but that may have been before I realized that I had to alter the file extension to .bytes for TextAsset to support it.
Answer by TheFastLane · Mar 01, 2021 at 03:31 PM
I have had similar situations where I have a non standard asset that I want to load from resources and save to the devices folder structure at runtime. The comments on @frjtrifork 's question worked perfectly. To summarize:
Rename your asset's file extension to ".bytes" (If you're in Windows you may have to enable viewing file extensions)
Place your newly renamed asset in the Resources folder in your Unity project
Run "TextAsset asset = Resources.Load("asset/file/path");"
Then "File.WriteAllBytes("device/file/path.originalExtension", asset.bytes);"
Your answer
![](https://koobas.hobune.stream/wayback/20220611235210im_/https://answers.unity.com/themes/thub/images/avi.jpg)