- Home /
Do Animated GIFs store properly in an Asset Bundle? Or is it just the 1st frame?
I'm building Asset Bundles with the standard way through the Unity Tutorials and it generates the asset bundle file and manifest correctly - though when I reload the animated gifs from the asset bundle it seems that the GIFs are treated like a static texture. Is the whole file stored there? Or does Unity strip the animated frames and only store the 1st frame?
I'm trying to pass this to my decoder plugin to play - but loading GIFs this way as opposed to via URL doesn't work the same. Do asset bundles actually contain the whole file embedded? Is this a problem with Unity not natively recognizing animated GIFs?
Can Asset Bundles store entire files and act like storage? Or only imported types that Unity natively understands?
Does the new Addressable way of doing things help with this at all? Or should my GIF be stored as a Resource if I don't want it accessed via the web?
thanks for any ideas and help!
Answer by Bunny83 · Feb 24, 2020 at 09:51 AM
If you just drop your gif as an gif file in Unity, Unity will import that image as a single Texture2D. AssetBundles contain assets in Unity's internal asset format. So no, if you imported the gif as an image in the Unity editor you get a single Texture2D asset.
If you want to ship the gif file "as it is" you can place it in the StreamingAssets folder inside your Assets folder. Files inside that folder are essentially ignored by Unity and shipped as they are. On most target platforms the streaming assets are shipped in a subfolder of your build. Currently the only exception are Android builds where they are packed inside the APK file. Anyways you can use Application.streamingAssetsPath to get the proper path for the target platform. On Android and WebGL you have to use the UnityWebRequest to load an asset from the streaming assets. On other platforms you can use ordinary File io (System.IO.File).
StreamingAssets is a way to include assets just as binary data as it is into your game build. If you want to include such files in assetbundles you have to change the extention to .bytes
. This will make unity to interpret the file as a TextAsset (manual). To access the data you can access such assets as TextAsset. The bytes property gives you access to the raw bytes of the file. TextAssets can be included in AssetBundles or directly inside your project. They are just like any other asset, just that Unity doesn't interpret the data in any way.
Thank you very much for the run-down - it wasn't clear that just having a GIF in the Assets folder meant Unity only saw it as a single static 1st frame texture - but I was afraid that was the case.
So Strea$$anonymous$$gAssets is great for shipping GIFs with the game. But then your method of rena$$anonymous$$g to .bytes
and getting the data as a TextAsset from an internet requested AssetBundle, implies that I'd need to get the binary data of the GIF as a string from TextAsset and then have to have a decoder that can accept a string input for the binary file? Or am I missing something? Because I don't think the GIF decoder plugin I'm using accepts that to its playback. It just accepts a url path or local path for the binary file. Can you please clarify a little about that part of your suggestion in the case I need to provide non-shipped GIFs to my game?
Thanks again so much for your detailed help!! :)
You could get the bytes from the "Text Asset" and save to file. Then you've got a local path you can give to the plugin.
As Bonfire said (and as I have already mentioned in my answer) you should use the bytes property which give you access to the raw byte array (`byte[]`). Even it's called a "TextAsset" it's just a raw data asset. They could have named that class this way.
I don't know what gif loader you use, but I guess it there should be a way of feeding it a byte array. If not it should at least support reading from a Stream. You can create a $$anonymous$$emoryStream from your byte array if necessary. This is all just plain C# / .NET.
Your answer
![](https://koobas.hobune.stream/wayback/20220612223855im_/https://answers.unity.com/themes/thub/images/avi.jpg)