Hello Guest

Author Topic: encapsulating the tk2dStaticSpriteBatcher commit behavior  (Read 6068 times)

storm33229

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 23
    • View Profile
encapsulating the tk2dStaticSpriteBatcher commit behavior
« on: March 14, 2012, 12:36:19 am »
Here's a snippet from the editor script:

Code: [Select]
if (batcher.batchedSprites == null || batcher.batchedSprites.Length == 0)
{
if (GUILayout.Button("Commit"))
{
                batcher.Commit();
EditorUtility.SetDirty(target);
}
}

Then there's the actual code inside the tk2dStaticSpriteBatcher script:

Code: [Select]
    public void Commit()
    {
        List<tk2dSprite> sprites = new List<tk2dSprite>();
        tk2dSpriteCollectionData scd = null;

        for (int i = 0; i < this.transform.childCount; ++i)
        {
            Transform t = this.transform.GetChild(i);
            tk2dSprite s = t.GetComponent<tk2dSprite>();
            if (s)
            {
                if (scd == null) scd = s.collection;
                if (scd != s.collection)
                {
                    Debug.LogError("Error: Multiple sprite collections found");
                    return;
                }

                if (scd.allowMultipleAtlases)
                {
                    Debug.LogError("Error: Sprite collections with multiple atlases not allowed");
                    return;
                }

                sprites.Add(s);
            }
        }

        // sort sprites, smaller to larger z
        sprites.Sort((a, b) => b.transform.localPosition.z.CompareTo(a.transform.localPosition.z));

        if (sprites.Count == 0)
        {
            Debug.LogError("Error: No child sprite objects found");
            return;
        }

        this.spriteCollection = scd;
        this.batchedSprites = new tk2dBatchedSprite[sprites.Count];
        int currBatchedSprite = 0;
        foreach (var s in sprites)
        {
            tk2dBatchedSprite bs = new tk2dBatchedSprite();

            bs.name = s.gameObject.name;
            bs.color = s.color;
            bs.localScale = new Vector3(s.scale.x * s.transform.localScale.x, s.scale.y * s.transform.localScale.y, s.scale.z * s.transform.localScale.z);
            bs.position = s.transform.localPosition;
            bs.rotation = s.transform.localRotation;
            bs.spriteId = s.spriteId;
            bs.alwaysPixelPerfect = s.pixelPerfect;

            this.batchedSprites[currBatchedSprite++] = bs;

            GameObject.DestroyImmediate(s.gameObject);
        }

        this.Build();
    }

This was useful for me because at editor-time I was able to auto-batch my tile layers instead of clicking "Commit" for each one.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: encapsulating the tk2dStaticSpriteBatcher commit behavior
« Reply #1 on: March 14, 2012, 01:15:40 pm »
The tilemap editor (beta version due very soon!) uses a slightly different method to construct batched objects. I am considering expanding that to the static sprite batcher - where it becomes a more fully featured 2D level editor instead of a simple batch constructor as it is now. I'll try and get that out ASAP (I'm still aiming for the end of the week) - I would be very grateful it if you could take a look at it and tell me what you think of that workflow compared to the static sprite batcher.

storm33229

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: encapsulating the tk2dStaticSpriteBatcher commit behavior
« Reply #2 on: March 14, 2012, 06:03:13 pm »
That sounds good to me. I'll look at it whenever you're ready.  :)