2D Toolkit Forum

2D Toolkit => Support => Topic started by: V1ncam on January 24, 2014, 03:55:55 pm

Title: Adding new sprite to collection
Post by: V1ncam on January 24, 2014, 03:55:55 pm
Hi,

I want to download and draw an image every now and then. Therefore I need to add an image to a SpriteCollection. The example in the demo uses an already pre-worked image. I need to be able to add an plain .png to the collection. Does someone know how to do this? (And is it possible to save these in that collection so I can use these the next time the program is started?)

[EDIT]: Okay, so I've found how to load and draw an image with 2DToolkit:

Code: [Select]
Texture2D tex = (Texture2D)Resources.Load("Image");
GameObject go = tk2dBaseSprite.CreateFromTexture<tk2dSprite>(tex, tk2dSpriteCollectionSize.Explicit(1, tex.height), new Rect(0, 0, tex.width, tex.height), new Vector2(0, 0));
Instantiate(go);

But I feel like this isn't the 'right' way to go. It is not really using the mechanics of 2dToolkit. How can I integrate this more? For instance I really need the dimension in pixel units function, which seems to be missing right now.

V1ncam
Title: Re: Adding new sprite to collection
Post by: unikronsoftware on January 25, 2014, 09:57:11 am
You need to call a different SpriteCollectionSize function, specifically tk2dSpriteCollectionSize.PixelsPerMeter( 100 )
Also you don't need to Instantiate (go) after that, as its already instantiated.

Check the runtime sprite collections sample - it shows you how you can do more complicated things with this - eg. extract sprites from an atlas, or create sprites from an atlas packed using Sprite Packer. Tk2d can't pack atlases at runtime, it would be far too slow from c# to be useful as a general tool.
Title: Re: Adding new sprite to collection
Post by: V1ncam on January 25, 2014, 08:10:56 pm
Thank you very much! ;D

Does the demo scene also contains storing a sprite to a collection (maybe when the program closes) so the next time the program is started it is there? Sorry right now I am not anywhere near the code.
Title: Re: Adding new sprite to collection
Post by: unikronsoftware on January 25, 2014, 11:07:08 pm
You can't store the sprite to a collection, you'd have to save that manually as you would in Unity.
Something like this.
http://forum.unity3d.com/threads/31268-SOLVED-Save-a-www-loaded-texture-to-the-local-hard-drive