Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Loading texture atlas from bundle

Discussion in '2D' started by HraKK, Jan 10, 2014.

  1. HraKK

    HraKK

    Joined:
    Oct 13, 2013
    Posts:
    12
    Hello guys!

    I really got a trouble. Is there any way to load texture atlas from asset bundle?

    Let me explain my steps:

    1) Create two texures with Photoshop: texture_a, texture_b
    2) Set its as Multiple Sprites and slice each to two sprites: "sprite_a_0", "sprite_a_1" and "sprite_b_0", "sprite_b_1"
    3) Create from its asset bundle : bundle_with_texture_a_and_b
    4) Okey, right now i load this asset bundle
    5) And at this step i got a trouble. Asset bundle has no method to load Sprite[] from texutere_a and texture_b

    Can any body help me?

    With best regards, Alex
     
  2. HraKK

    HraKK

    Joined:
    Oct 13, 2013
    Posts:
    12
    Anyone? Please!
     
  3. HraKK

    HraKK

    Joined:
    Oct 13, 2013
    Posts:
    12
    Hey! It makes asset bundle totally useless. I hope there is should be a way to resolve this problem! Help me, please :(
     
  4. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    Code (csharp):
    1.  
    2. AssetBundleRequest request = bundle.LoadAsync ("sprite_a_0", typeof(Sprite));
    3.  
    Does this work?
     
  5. HraKK

    HraKK

    Joined:
    Oct 13, 2013
    Posts:
    12
    Doesn't. But if i gonna do bundle.LoadAll() i will see "sprite_a_0" with type Sprite. But I can't load it separatly.
     
  6. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    Just loading the sprite may not work, you might have to load the texture too (or first). The Sprite class only contains reference information, not the image itself. Either that or you could make a gameobject/prefab and bundle/import that.
     
  7. HraKK

    HraKK

    Joined:
    Oct 13, 2013
    Posts:
    12
    zombiegorilla
    Exactly! I did it as well.

    var texture2D = package.GetBundle().Load("texture_a", typeof(Texture2D)) as Texture2D;
    var spite = package.GetBundle().Load("sprite_a_0", typeof(Sprite)) as Sprite;

    UnityEngine.Debug.Log(texture2D ); // Texture2D
    UnityEngine.Debug.Log(spite ); // NULL


    Please! Can you explain a little more. Unfortunately, I tried several times but always got fault. So maybe I do something wrong.
     
    Last edited: Jan 12, 2014
  8. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    Ah, so it is coming null, not empty.

    If you take your Sprite, drag it to the stage, the drag that back into the project window, it will create a prefab. Granted this prefab will be gameobject with a spriterenderer, but that is typically how you use sprites anyway. Then bundle the prefab, and you should be able to load it as a gameobject. If you still need to reference the sprite, you can get it via GameObject.SpriteRenderer.sprite.
     
  9. HraKK

    HraKK

    Joined:
    Oct 13, 2013
    Posts:
    12
    But, it will be undependant sprite! Not sprite in atlas! It will have undependant material etc.
    It's breake completly the atlas idea.

    Also, this way looks like road to the hell. One prefab to one sprite, many sprite renderer. Kinda helly, isn't ?

    I can't belive what Unity3D developers hate me.

    I JUST WANT TO USE AN ATLAS MECHANIC AS THEY SUGGEST ME IN THE TUTORIAL.

    Am I ask too much? Just wanna puts it to bundle:sad:

    Whatta hell :(
     
  10. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    Just to clarify, you are using Pro, right?
     
  11. HraKK

    HraKK

    Joined:
    Oct 13, 2013
    Posts:
    12
    Exactly
     
  12. sonee

    sonee

    Joined:
    Sep 2, 2012
    Posts:
    18
  13. HraKK

    HraKK

    Joined:
    Oct 13, 2013
    Posts:
    12
    So sad. I can't create games without it.

    Okay, let me try another framework for my game, maybe Cocos2D or marmelade
     
  14. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    Why can't you create games with out it? Lol.
     
  15. HraKK

    HraKK

    Joined:
    Oct 13, 2013
    Posts:
    12
    Because I have to choose from two evils:

    1) Store all atlas in main package and size of my game will be grow so high
    2) Don't use an atlas and draw call of my game will be grow so high

    I think this is unacceptable, isn't it?
     
    Last edited: Jan 13, 2014
  16. benhumphreys

    benhumphreys

    Joined:
    Jan 20, 2013
    Posts:
    30
    As far as I can tell this is still an unfixed bug right?

    It's impossible to load Sprites from AssetBundles. They get stored as Texture2D, and the sprite information is lost.
     
  17. HraKK

    HraKK

    Joined:
    Oct 13, 2013
    Posts:
    12
    Yep. They are confirmed that it is a bug and it will be fixed soon™ But I did't get any news after.
     
  18. Exort

    Exort

    Joined:
    Apr 6, 2014
    Posts:
    1
    This is a late response, but you need to include metadatas in your Asset Bundle ("complete assets" option) when building it so that your Sprite import settings are properly saved. This will increase the size of the bundle, but allows you to load sprites using the Load(mySprite, typeof(Sprite)) method or it's async counterpart.

    BuildAssetBundleOptions options = BuildAssetBundleOptions.CompleteAssets;
    if(!useCompression)
    BuildAssetBundleOptions |= BuildAssetBundleOptions.UncompressedAssetBundle;

    BuildPipeline.BuildAssetBundle( mainAsset,
    assets,
    outputPath,
    out crc,
    options,
    BuildTarget.iPhone);
     
  19. silentneedle

    silentneedle

    Joined:
    Mar 14, 2013
    Posts:
    280
    Was someone able to do this with Unity 5? I still receive null when loading the sprite from an asset bundle.