Hello Guest

Author Topic: How to tk2dSystem  (Read 4990 times)

Maserat

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
How to tk2dSystem
« on: December 28, 2012, 04:56:13 pm »
How do you use the tk2dSystem to load collections? I want to load and unload sprite collections so that I can free up texture memory when I am not using particular sprites. How would I go about doing this? Also, when I destroy a sprite collection does it also release the texture/material from memory?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to tk2dSystem
« Reply #1 on: December 28, 2012, 07:35:00 pm »
If you want to free up memory when not using particular sprites, delete them (to the point where you have NO live references to them, including prefabs, etc.) and then call Resources.UnloadUnusedAssets. This will unload the texture / material from memory.

There is a bug stopping you from being able to properly use custom asset loading in 1.8x, this is fixed in 1.9 - I will be releasing a beta soon. It does mostly work, but there isn't any way to reconstruct the data if you delete/lose the resources/tk2d directory, which makes it a bit more unstable than anything else.

For the time being though, you could simply create a sprite, create a prefab of that, store it in a resources directory and load that using Resources.Load...

Maserat

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: How to tk2dSystem
« Reply #2 on: December 28, 2012, 09:53:50 pm »
Thanks for the quite response Unikron.
I have rewritten this message several times, and have diagnosed myself with newbieness. So I will just write out everything.
I have a character whose total animations' texture memory crashes the application on an iOS device. I have devised a method of having an idle animation and several special animations. While the idle animation plays the other animations' spritesheet will be unloaded or loaded based on what the character might do next. How in code would I do this? If I get the actual pointer to the spritesheet and delete/reload it would that work?

Now directly addressing your response, when and how exactly do I call Resources.UnloadUnusedAssets? Through debugging I have noticed that two copies of an atlas0 is kept in memory if one lies on/is loaded to the screen and one lies in the resource folder. So obviously, I am calling it at an inappropriate time.

If all this will be answered with 1.9 just reply with "wait until 1.9"(code example are welcomed). I have many other things to work on and can reasonably wait until the next update.

Can you supply a System demo? Like, are all the collections and sprite held within it via a guid?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to tk2dSystem
« Reply #3 on: December 29, 2012, 04:42:20 am »
If you're using the resources directory (only put the sprite collection data object in there, not anything else), it should load/unload fine. Loading textures into memory isn't quick on iOS though, so if you need to stream this in quickly, it might not work for you. There will be a rather huge spike when the texture is first visible/displayed on screen - this is likely to correspond to when your animation starts to play.

For animations, you don't even need to mess about with the sprite collections, etc. Just create a second SpriteAnimation object with the appropriate frames in there, put THIS into the resources directory, and then load this in at runtime using Resources.Load. For unloading the asset you can use Resources.UnloadAsset to specifically unload the specific animation/texture/etc. Make sure you kill all references to it after unloading though. Resources.UnloadUNusedAssets will unload all unused assets and if you've removed all references by then, it should unload everything. Again though, GC will likely trigger here and spike again.

The alternatives are lower resolution textures / compression (dithered 16bit may work) / less frames, etc, but I'm sure you've thought of these already.