2D Toolkit Forum

2D Toolkit => FAQs => Topic started by: unikronsoftware on January 26, 2012, 07:07:01 pm

Title: How do I create a Sprite in script?
Post by: unikronsoftware on January 26, 2012, 07:07:01 pm
The sprite classes aren't meant to be instantiated in code. This is still possible, check tk2dSpriteEditor.DoCreateSpriteObject() for how a sprite is created in the interface.

We recommend you set up prefabs for the sprites you need to instantiate in game and instantiate them instead. That way, you can visually set up your sprite, and you'll be guaranteed everything is set up correctly when you instantiate it in game.

If you need to generate large numbers of sprites by name, simply create one prefab for your sprite collection, instantiate it, and use GetSpriteIdByName() and switch the sprite. This way, you can pool one sprite type per Sprite Collection, and instantiate them the way you need it.