Search Unity

Unloading Spritesheets loaded from asset bundle?

Discussion in 'Asset Bundles' started by jchowdown-asla, May 3, 2019.

  1. jchowdown-asla

    jchowdown-asla

    Joined:
    May 10, 2017
    Posts:
    26
    Hi Everyone, there's a spritesheet that's resident in memory for our whole game. I'm looking at how we can unload it completely and free up its texture memory when it's not needed. The way we're loading it is via an AssetBundleRequest:

    Code (CSharp):
    1.         var request = ab.LoadAssetWithSubAssetsAsync<T>(path);
    2.         yield return request;
    3.         var t = request.allAssets as T[];
    4.         callback(t);
    't' is now a Sprite[] that's passed back up into game code for storage and lookups.

    How would one forcibly unload the whole spritesheet in this case? By the time we're done with the spritesheet, the AssetBundleRequest is long gone and we only have references to the individual sprites at this point. Also, the bundles in question contain assets that are used for other purposes, and those shouldn't be unloaded when it's time to unload this spritesheet.

    Thanks in advance,
    Jeff
     
  2. jchowdown-asla

    jchowdown-asla

    Joined:
    May 10, 2017
    Posts:
    26
    FYI our spritesheet is a .PNG file with some entries in the .meta file that describes the location/size of each sub sprite. It's generated by a tool called TexturePacker. It's not a SpriteAtlas asset
     
  3. Ryanc_unity

    Ryanc_unity

    Unity Technologies

    Joined:
    Jul 22, 2015
    Posts:
    332
    ab.Unload(true)

    This unloads all loaded objects from that specific bundle and is the recommended way to clean up. Depending on how you have packed your assets into bundles, you might need to shuffle that around a bit / break it up into more bundles to make it easier to load / unload incrementally.
     
    jchowdown-asla likes this.
  4. jchowdown-asla

    jchowdown-asla

    Joined:
    May 10, 2017
    Posts:
    26
    That's what I was gonna ask next. If making one bundle for each spritesheet is the only way to unload them in isolation then this is what we'll do. We have the technology!