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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Packing a Texture Sprite Sheet in an Asset Bundle with already sliced subtextures, possible?

Discussion in '2D' started by LorenzoNuvoletta, Mar 23, 2015.

  1. LorenzoNuvoletta

    LorenzoNuvoletta

    Joined:
    Apr 28, 2014
    Posts:
    54
    I have been able to pack Textures with Sprite Mode: Single in an Asset Bundle and use them with no issues.

    Now, I am trying to pack a Texture with Sprite Mode: Multiple where I have already manually sliced it in multiple subtextures. The texture sheet itself is successfully packed in the bundle, but no information about my manual slices is packed.

    Is this a bug or a missing feature in Unity 5?
    Is the only workaround to re-slice everything once again a runtime?

    Thanks.
     
    Last edited: Mar 24, 2015
  2. Cranick

    Cranick

    Joined:
    Nov 20, 2011
    Posts:
    310
    I know that the information of the multiple sprites you are asking is pretty much in the .meta files that are created if you have enabled in (project settings->editor->version control->Visible Meta Files). It holds all the slicing information that is necessary to be used, but as for loading them in the game, I believe that information is built onto the game itself and if the texture sheet where to change, then it would simple still hold those values of where the sprites are located and would have to pull from there. If you were to update that information (slicing information) you would need to write some custom shader that is using the values of this information that is being build if it were needing to be updated at runtime, in which case it hasn't been done with asset bundles as of this moment yet. Most of the time the information that is needed is usually packaged in an asset bundle of all the images to be used rather than relying on a texture sheet with all the sprites in one sheet. It would be better for the time being to just have all the sprites individually packed in the asset bundle and load them from there as their size might change at runtime if you are making a dynamic game that changes based on updates.

    Will do further research on asset bundles to see what you're asking is already possible. Whether Unity 4.6, or Unity 5. I would like to think that asset bundles themselves should hold all the sprites it loads into it regardless of it being a Single Sprite or Multiple Sprite Sheet. (I will get back to you and respond on this after I find out).

    I hope this helps, let me know if you need any further information.
     
    Last edited: Mar 24, 2015
  3. LorenzoNuvoletta

    LorenzoNuvoletta

    Joined:
    Apr 28, 2014
    Posts:
    54
    Hi, Cranick, thanks for the response, at the moment I am simply trying to figure out if this is a missing feature in Unity 5, because if it is, I can re-splice the Texture Sprite Sheet at run time with an Atlas.
    Please let me know if you find otherwise,
    thanks.
     
  4. Cranick

    Cranick

    Joined:
    Nov 20, 2011
    Posts:
    310
    Here is what I did to load a specific sprite out of the spritesheet.

    Code (CSharp):
    1. Sprite[] sprites = Resources.LoadAll<Sprite>("folder/spritesheet");
    2.  
    3. foreach (Sprite sprite in sprites)
    4. {
    5.      if (sprite.name == SpecificSpriteName)
    6.      {
    7.          return sprite;
    8.      }
    9. }
    It seemed that worked fine for me and I hope that is able to help you out when building your asset bundles. Let me know if this works for you.
     
  5. LorenzoNuvoletta

    LorenzoNuvoletta

    Joined:
    Apr 28, 2014
    Posts:
    54
    This works for me when the Sprite is in the Resources folder, doens't work when the asset is in an Asset Bundle.
     
  6. LorenzoNuvoletta

    LorenzoNuvoletta

    Joined:
    Apr 28, 2014
    Posts:
    54
    So this is how I solved it:

    Code (CSharp):
    1. AssetBundle bundle = www.assetBundle;
    2.  
    3. Sprite[] spriteSheet = bundle.LoadAssetWithSubAssets<Sprite>( nameOfTheWholeSpriteSheet );
    4.  
    5. foreach (Sprite subSprite in spriteSheet) {
    6.     if (subSprite.name == nameofTheSubSprite) return subSprite;
    7. }
     
  7. Cranick

    Cranick

    Joined:
    Nov 20, 2011
    Posts:
    310
    Yes, I forgot you mention it bundled up. Would have modified my answer to match yours but great find nonetheless. :)
     
    LorenzoNuvoletta likes this.
  8. SimteractiveDev

    SimteractiveDev

    Joined:
    Jul 9, 2014
    Posts:
    97
    Just curious, how do you find the loading times with this? I am finding them to be quite long. All of the sprites in our bundle can be used at any time, so when the game launches we load them all into a dictionary but it is taking up to 10 seconds when using asset bundles (and by 10 seconds, I mean loading the asset from the bundle which is locally stored within the app). When they were in Resources folder, the loading time was much quicker.
     
  9. snobalpaul

    snobalpaul

    Joined:
    Jul 17, 2019
    Posts:
    6
    Thank you! 4 years later (unity 2019.1) and it worked first try :)