1
Support / Re: Chunk z-position disparity
« on: October 07, 2016, 11:58:59 am »
Ah, am amazing. Thank you
d
d
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
using UnityEngine;
public class BatchableSprite : MonoBehaviour {
// this just stores a Sprite, but I've created a whole class for it so
// it can be easily extended out to store other data:
public tk2dBaseSprite theSprite;
}
using UnityEngine;
using System.Collections.Generic;
public class SpriteBatch : MonoBehaviour
{
public tk2dStaticSpriteBatcher ourSpriteBatcher;
public void Setup(List<BatchableSprite> bList)
{
// we need a list of BatchedSprites
List<tk2dBatchedSprite> batchedSprites = new List<tk2dBatchedSprite>();
// go through all the sprites in our list, and nab the pertinent information:
foreach (BatchableSprite b in bList)
{
tk2dBatchedSprite bs = new tk2dBatchedSprite();
bs.spriteCollection = b.theSprite.Collection;
bs.spriteId = b.theSprite.spriteId;
bs.relativeMatrix.SetTRS(b.transform.position, Quaternion.Euler(0, 0, 0), Vector3.one);
bs.color = b.theSprite.color;
batchedSprites.Add(bs);
}
// BUILD IT:
BuildSpriteBatch(batchedSprites);
// we can now safely destroy our source GameObjects:
foreach(BatchableSprite b in bList)
Destroy(b.gameObject);
}
private void BuildSpriteBatch(List<tk2dBatchedSprite> batchedSprites)
{
// get the batched sprites into tk2d's SpriteBatcher. First, get the array to the right size:
ourSpriteBatcher.batchedSprites = new tk2dBatchedSprite[batchedSprites.Count];
// go through them all, assigning to the appropriate slot
for (int i = 0; i < batchedSprites.Count; ++i)
ourSpriteBatcher.batchedSprites[i] = batchedSprites[i];// bs;
// set flags:
ourSpriteBatcher.SetFlag(tk2dStaticSpriteBatcher.Flags.GenerateCollider, false);
ourSpriteBatcher.SetFlag(tk2dStaticSpriteBatcher.Flags.FlattenDepth, true);
ourSpriteBatcher.SetFlag(tk2dStaticSpriteBatcher.Flags.SortToCamera, true);
// Do the actual Build:
ourSpriteBatcher.Build();
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SpriteBatchManager : Manager {
public SpriteBatch spriteBatchPrefab;
public void BatchSprites(List<BatchableSprite> theBatchableSprites)
{
// this function takes a group of 2dtoolkit sprites, and batches them to a single drawcall.
// first, we need to create a Sprite Batch to contain our supplied List of sprites
SpriteBatch theSpriteBatch = Instantiate(spriteBatchPrefab, transform.position, Quaternion.identity) as SpriteBatch;
theSpriteBatch.transform.SetParent(transform);
// tell the sprite batcher to create a single Mesh Renderer based on the data from the List of sprites:
theSpriteBatch.Setup(theBatchableSprites);
}
}
// we need to pass the BatchableSprites to the SpriteBatcher, so let's keep a List
List<BatchableSprite> theBatchableSprites = new List<BatchableSprite>();
// TEST, 1000 sprites all laid out nicely in a single batch:
for (int i = 0; i < 1000; i++)
{
BatchableSprite b = Instantiate(batchableSpritePrefab, new Vector3(i, 0, 0), Quaternion.identity) as BatchableSprite;
b.theSprite.color = Color.red; // SET THE COLOUR!
theBatchableSprites.Add(b);
}
spriteBatchMgr.BatchSprites(theBatchableSprites);