Hello Guest

Author Topic: Create tk2dSpriteCollection on runtime  (Read 4060 times)

ivomarel

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Create tk2dSpriteCollection on runtime
« on: August 13, 2013, 09:58:58 am »
Hey all,

This seems to be a pretty obvious problem. I have no experience with creating atlases on runtime and I fear it will be incredibly slow on mobile devices. However, it would save an immense number of drawcalls for me (currently on 200) so I am willing to look into it.

Question 1:
Can I create an atlas of several PNG's on runtime, like I can in the editor? I'm looking into tk2dSpriteCollectionData.CreateFromTexture, but it seems to only be useful when there's one texture which is the atlas.

Question 2:
Can I even, instead of recreating the complete atlas, replace only certain elements in the PNG?

Shot in the dark, but perhaps you guys can help me out.

Cheers!

Ivo

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Create tk2dSpriteCollection on runtime
« Reply #1 on: August 13, 2013, 10:15:12 am »
Hi,

You can't build atlases at runtime at the moment, but you've got full source code to do it if you want to. You'd probably want to skip all the time consuming parts, or even consider using Texture2D.PackTextures to do it, as it'll probably be faster than anything written in c#.

Once you have a packed atlas, you can use tk2dSpriteCollectionData.CreateFromTexture to create a sprite collection from your atlas. Check the Runtime Sprite Collection sample to see how you can do this.

You can replace elements in a PNG sure, but remember that reading and writing to pixels in a texture at runtime MASSIVELY bloats your memory usage. Best to avoid it where possible.