Before I start, let me insist that I do not want to put my files in StreamingAssets. I know it would work but that’s not part of the requirements.
I need to read a binary resource. It’s an animated GIF file. So I need to load it from the resources and access its bytes.
I thought I could rename it to Assets/Resources/myfile.bytes and then use
var myfile = Resources.Load<TextAsset>("myfile");
But this returns null. I tried many variants with different extensions too.
I suspect that whenever I drag a GIF file to Unity assets, it always recognizes the file type even if the extension is changed. I think that because it generates the file “myfile.bytes.gif.meta”.
At this point I don’t know what to do and I’m about to give up. Please give me a hand.
EDIT 1: This only happens on MacOS, not on Windows. Please let me know how to make it work on MacOS too.
3
I found the solution!
It turns out, when you change file extension on MacOS, that the type of the file is not really changed but hidden. The trick is to open the file’s information panel (command+i), and go to the Name and Extension section:
Then, just uncheck the box “Hide extension” and remove “.gif”. A confirmation dialog will pop up, so just agree with the new extension. That’s all!
Not every asset is a TextAsset
. A gif image is recognised by Unity as an image and imported as Texture2D
. So the actual asset is already the imported Texture2D
. If you want to ship arbitrary data as TextAsset
, you have to append either .txt
, .bytes
or any of the extensions mentioned here to your file name so Unity will actually treat them as TextAssets
.
Keep in mind that the asset / resource name is always the filename without the (final) extension. So when you append .bytes
and your file name becomes "MyFile.gif.bytes"
the resource name should be "MyFile.gif"
.
1