2D Toolkit Forum

2D Toolkit => Support => Topic started by: Benny on June 24, 2013, 10:17:06 am

Title: How to programmatically create a static sprite batcher under tk2d 2.0
Post by: Benny on June 24, 2013, 10:17:06 am
I need to build a static sprite batcher from code. This was working before 2.0.

The new sprite batcher in 2.0 supports more kinds of sprites using different sprite collections. This is super cool, however, my code stopped working.

So how to create a sprite batcher from code?

Here is my code, it does create the batcher, but the batched sprites' positions and scales are all wrong, any clue?

Code: [Select]
            tk2dStaticSpriteBatcher batcher = new GameObject("spritebatcher").AddComponent<tk2dStaticSpriteBatcher>();
            batcher.spriteCollection = spriteCollection; // I bet this is useless under 2.0

            tk2dBatchedSprite[] batchedSpriteList = new tk2dBatchedSprite[count];
            Vector3 position = batcher.transform.position;
            for (int i = 0; i < count; ++i)
            {
                tk2dBatchedSprite batchedSprite = new tk2dBatchedSprite();
                batchedSprite.spriteCollection = spriteCollection; // i added this line for 2.0
                batchedSprite.name = "batched sprite";
                batchedSprite.spriteId = spriteCollection.GetSpriteIdByName(spriteName);
                batchedSprite.localScale = Vector3.one * 10;
                batchedSprite.position = position;
                position += new Vector3(10, 0, 0);
                batchedSpriteList[i] = batchedSprite;
            }

            batcher.batchedSprites = batchedSpriteList;
            batcher.Build();
Title: Re: How to programmatically create a static sprite batcher under tk2d 2.0
Post by: unikronsoftware on June 24, 2013, 10:21:09 am
A quick sample here:
http://unikronsoftware.com/2dtoolkit/doc/2.00/advanced/scripting_static_sprite_batcher.html
Its changed a bit. Only a tiny bit, should be easy to fix.
Title: Re: How to programmatically create a static sprite batcher under tk2d 2.0
Post by: Benny on June 24, 2013, 10:23:16 am
That's really a quick reply, cheers!  ;)