Hello Guest

Author Topic: How to programmatically create a static sprite batcher under tk2d 2.0  (Read 4104 times)

Benny

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
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();

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
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.

Benny

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
That's really a quick reply, cheers!  ;)