Hello Guest

Author Topic: Sprites, Atlases and memory management  (Read 19182 times)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprites, Atlases and memory management
« Reply #15 on: April 24, 2013, 03:26:39 pm »
Yes. It should work as long as there are absolutely no references to the sprites / materials or textures left.

Grebenots

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Sprites, Atlases and memory management
« Reply #16 on: April 24, 2013, 03:58:49 pm »
Good to know!
One last thing.  Does merely having a tk2dSpriteCollectionData variable in the inspector actually load the sprite collection into memory?

For instance,

My single sprite has,

var myColorCollection : tk2dSpriteCollectionData;
var myAnimalCollection : tk2dSpriteCollectionData;
etc etc.

However, this single sprite only ever has any one of those collections loaded at any given time.  Will this keep only the active collection in memory so long as I unload unused assets, or does having those variables gum things up?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprites, Atlases and memory management
« Reply #17 on: April 24, 2013, 04:03:00 pm »
Yes. You should load using Resources.Load if you wish to dynamically swap stuff.

Grebenots

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Sprites, Atlases and memory management
« Reply #18 on: April 24, 2013, 05:12:20 pm »
Making some progress here, however:

When I use , mySpriteCollectionData = tk2dSystem.LoadResourceByName<tk2dSpriteCollectionData>("ColorTiles");

I get the error:

Cannot infer generic arguments for method 'tk2dSystem.LoadResourcesByName.<T>(String)'.  Provide stronger type information through arguments, or explicitly state the generic arguements.

I'm not sure what that means.  Loadable asset is ticked and committed in the sprite collection as well.

Grebenots

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Sprites, Atlases and memory management
« Reply #19 on: April 24, 2013, 06:24:37 pm »
At any rate, it's not a big deal.  I have a standard Resources.Load working fine.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprites, Atlases and memory management
« Reply #20 on: April 24, 2013, 11:37:58 pm »
That is quite bizzare.
What type is mySpriteCollectionData? I trust it is a tk2dSpriteCollectionData and not a var?

The GUID variant is used just like this, and I take it you're not getting any errors about that?
platformSpecificData = tk2dSystem.LoadResourceByGUID<tk2dSpriteCollectionData>(guid);

fattie

  • 2D Toolkit
  • Full Member
  • *
  • Posts: 106
    • View Profile
Re: Sprites, Atlases and memory management
« Reply #21 on: September 11, 2013, 12:26:43 pm »
"There will be a explicit way to unload a specific sprite collection in 2D Toolkit 2.0."

So to be perfectly clear, this is ..

http://unikronsoftware.com/2dtoolkit/forum/index.php?topic=2101.0

scroll down to the example "scdToUnload.UnloadTextures()" ..

am I correct?

Where's my best bet for any doco or examples on this?  Cheers

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprites, Atlases and memory management
« Reply #22 on: September 11, 2013, 01:07:14 pm »
UnloadTextures is it. It definitely works, but you need that funky reload trick to load things back in. Thats sadly a Unity bug.

fattie

  • 2D Toolkit
  • Full Member
  • *
  • Posts: 106
    • View Profile
Re: Sprites, Atlases and memory management
« Reply #23 on: September 15, 2013, 02:37:26 pm »
"For now, if you destroy all sprites using the sprite collection, and then do Resources.UnloadUnusedAssets(); that should unload all unused spritecollections."

One finding, based on a tremendous amount of testing on a vast number of iOS devices from 3GS onwards.  I have found that it DOES IN FACT (i.e., Unity does in fact), CORRECTLY get rid of the memory from texture memory, when you use TK's UnloadTextures.

This is great news.

(Typically you can not really use UnloadUnusedAssets during gameplay, as it is slooow of course in Unity. So normally that's not really an option, I'd say.)

So in short I have found that the UnloadTextures approach works really well, perfectly - you can sit there watching the texture memory go up and down - absolutely reliably - in the profiler.  (This on iOS anyways - who knows on android!)

-- HOWEVER --: I have found, furthermore, that if you are running up against the memory limit in iOS, "you're screwed".  Say you unload collection "AA".  The problem is this: later, you want to load "AA" again, unity WILL NOT DO IT - essentially a bug or weakness in Unity. (Unity should, at worst, stop everything and do a slow UnloadUnusedAssets and load your new request for you, but it doesn't, you're screwed.) Again this is only when you're getting memory warnings in iOS.  But it's a killer problem because instead of your textures you'll get - black.  :)  (Annoyingly in a way, Unity, I think generally, sort of magics most memory warnings in iOS and does its very best to keep going anyway, that's great but you can get a bit sloppy about texture memory usage when developing for iOS with Unity.)

So anyway, I have found with vast testing that UnloadUnusedAssets works really fantastically, it works perfectly. You can load the collection later again when you need it.

Certainly, any time you have "backgrounds" on a 2D game, this would basically be a must, you could only work this way (you couldn't leave different backgrounds, which are enormous on retina, and relatively enormous even on old phones, hanging around).

I use precisely this code ..

Code: [Select]
function __specialReleaseTechnique( ccc:tk2dSpriteCollectionData )
{
var mtl:Material;
for ( mtl in ccc.inst.materials )
{
Resources.UnloadAsset( mtl.mainTexture );
}
}