Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Is it possible to bundle raw JPGs and PNGs in Addressables WITHOUT converting them to textures?

Discussion in 'Addressables' started by Endahs, Dec 5, 2020.

  1. Endahs

    Endahs

    Joined:
    Sep 16, 2014
    Posts:
    94
    Hi folks, thanks for reading!

    I have an immutable requirement to build ~700 raw JPGs and PNGs of various sizes in my project. I do NOT want Unity to convert these JPGs and PNGs into a more native bitmap texture and include those textures in the build - I only want the raw files.

    Is this possible with Addressables and/or Unity? When I build the Addressables group it builds the converted textures into it, and not the raw JPG/PNGs files that I want.

    Any ideas of how to package the raw JPGs and PNGs in an Addressable bundle?

    Thanks!
     
  2. Endahs

    Endahs

    Joined:
    Sep 16, 2014
    Posts:
    94
    Storing files in Assets/StreamingAssets folder DOES prevent Unity from auto-converting your JPGs and PNGs into textures, and will build the files raw.

    I have not tested Addressables referencing StreamingAssets files, however. Since Addressables bundles end up in that folder, I'm thinking it may duplicate it, if it is allowed at all...
     
  3. Endahs

    Endahs

    Joined:
    Sep 16, 2014
    Posts:
    94
    Oof. So including a JPG from StreamingAssets failed with Addressables:
    "Cannot recognize file type for entry located at 'Assets/StreamingAssets/hints/91804_constructHandleBars.jpg'. Asset import failed for using an unsupported file type"

    Can Addressables support raw PNGs or JPGs at all? I would hate to have to re-architect my whole solution and rip Addressables out, because it is a great system and a lot of effort to implement.
     
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,444
    Endahs likes this.
  5. Endahs

    Endahs

    Joined:
    Sep 16, 2014
    Posts:
    94
    Hey Peter, thanks for the response. I saw that thread and your response there and the recommendations in there is my backup plan if I cannot get Addressables to work with raw JPGs and PNGs. If I go the StreamingAssets + direct read in at runtime it looks like I will have to abandon all of the Addressable benefits (asset bundling, downloading and updating of remote bundles, etc) and will need to strip Addressables out of my architecture.

    Has anyone been able to get Addressables to work with raw JPGs or PNGs?
     
    Peter77 likes this.
  6. magmagma

    magmagma

    Joined:
    Oct 27, 2017
    Posts:
    41
    I'm not sure if it is possible right now.
    In fact there was a problem recently where if you had files that weren't recognized by addressables it wouldn't even be possible to make a build. There was a recent patch so that now you can exclude some file extensions, but excluding them means they will not be put into your asset bundles.

    But I am also interested in knowing if there is a way to do this.
     
  7. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,444
    You can also change the file extension of your jpg and png files to
    .bytes
    . In this case Unity doesn't import these files as textures anymore. Then you can also have them anywhere in your "Assets" folder, add them to Addressable Groups, etc.

    Unity imports .bytes files as binary
    TextAsset
    :
    https://docs.unity3d.com/ScriptReference/TextAsset-bytes.html

    Then you should be able to load it via:
    Code (CSharp):
    1. var asyncOp = Addressables.LoadAssetAsync<TextAsset>(key);
    2. yield return asyncOp;
    3. var data = asyncOp.Result;
    4. var tex = new Texture2D(4, 4);
    5. tex.LoadImage(data.bytes);
    (above code might not compile, I just typed it in here)
     
    Endahs likes this.
  8. magmagma

    magmagma

    Joined:
    Oct 27, 2017
    Posts:
    41
    ahh yes that makes perfect sense actually!
    I have used binary files before (without addressables or asset bundles) and I was trying to think how this would work but wasn't sure.

    So thanks!
     
    Endahs and Peter77 like this.
  9. Endahs

    Endahs

    Joined:
    Sep 16, 2014
    Posts:
    94
    This is a promising lead, thank you!

    I'll report back if Addressables plays nice with .byes as the TextAsset (I believe I'm using TextAsset for XML with Addressables right now). If this works I'm buying you a pizza!
     
  10. Endahs

    Endahs

    Joined:
    Sep 16, 2014
    Posts:
    94
    @Peter77 , I owe you a pizza! Private messaging you after this message.

    Confirmed that renaming the files to .bytes allows them to be bundled by Addressables. The workflow Peter77 provided above is correct. Works with PNGs and JPGs! My build size for my 2D game went from 460mb to 80mb (albeit with runtime texture importing).

    Thanks everyone!
     
    Peter77 likes this.