2D Toolkit Forum

2D Toolkit => Support => Topic started by: camelot10 on December 04, 2012, 11:41:56 am

Title: Load images from url ?
Post by: camelot10 on December 04, 2012, 11:41:56 am
How i can load images (png or jpg) from remote website and use it with 2d-toolkit?

found only this solution: http://docs.unity3d.com/Documentation/ScriptReference/WWW-texture.html
Title: Re: Load images from url ?
Post by: unikronsoftware on December 04, 2012, 12:34:52 pm
Once you have a Texture2D from www, call

tk2dSprite.CreateFromTexture (http://unikronsoftware.com/2dtoolkit/doc/html/classtk2d_sprite.html#adab98009ebf070997ced21134492e536)
or
tk2dSpritecollectionData.CreateFromTexture
(http://unikronsoftware.com/2dtoolkit/doc/html/classtk2d_sprite_collection_data.html#a3170e1526cfd4a9c59c3b07e40ba6cd4)

to create a sprite or sprite collection from it.
Title: Re: Load images from url ?
Post by: camelot10 on December 04, 2012, 04:40:25 pm
then another question.
what if i have to load multiple images from web.
should i use different collection for each image? this lead to extra draw calls
or
can i create one collection for all loaded images? its possible to add images in collection at runtime?
thanks
Title: Re: Load images from url ?
Post by: unikronsoftware on December 04, 2012, 07:36:42 pm
Building atlases on the fly isn't supported and even if it were, it wouldn't be very efficient - you will need all source textures uncompressed in memory, and that alone can use up a LOT of resources. You can, however, use the second variant (tk2dSpriteCollectionData.CreateFromTexture - to load multiple sprites from a pre-generated atlas). That way, you can create (extract) different sprites from one texture.

If you really want to do this - you should invest in a native c/c++ atlas generator where you are in full control over the memory, bypassing EVERYTHING unity does with the textures.

And to answer your question - you could in theory create one collection from all images - collections may span atlases, but it won't really save you anything.
Title: Re: Load images from url ?
Post by: camelot10 on December 05, 2012, 04:55:07 pm
i just trying to implement facebook friends avatars inside game.
any ideas, how i can do it ?
Title: Re: Load images from url ?
Post by: unikronsoftware on December 05, 2012, 07:57:12 pm
Well in that case I think the easiest way will be -
1. Create your own atlas at runtime. friends avatars are likely to be fixed size, so you can import the images, and then manually fill your Texture2D using getpixel & setpixel. This is going to be slow, so make sure you budget the time for it.
2. When you have your texture, create a spritecollection at runtime using it. The rect values should be trivial as they'll all be the same size.
3. Create sprites using this sprite collection.