Here's a snippet from the editor script:
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:
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.