Hello Guest

Author Topic: Help with dynamic TextMesh batching  (Read 5520 times)

peturg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Help with dynamic TextMesh batching
« on: August 26, 2013, 04:14:05 pm »
Iīm having trouble understanding how I can batch some textMesh objects. I have looked at the code example http://www.unikronsoftware.com/2dtoolkit/docs/2.00/advanced/scripting_static_sprite_batcher.html but I canīt seem to figure out how i can apply that to my current code.

For demo purposes this is what I got.
Code: [Select]
public GameObject go;
public GameObject prefab;

private tk2dStaticSpriteBatcher batcher;

void Start ()
{
batcher = go.AddComponent<tk2dStaticSpriteBatcher>();

GameObject item;

for(int i = 0; i < 100; i++)
{
Vector3 p0 = new Vector3(UnityEngine.Random.Range(-10,10), UnityEngine.Random.Range(-10,10), UnityEngine.Random.Range(-10,10));
item = Instantiate(prefab, p0, Quaternion.identity) as GameObject;
item.transform.parent = go.transform;

int num = UnityEngine.Random.Range(0,26);
char letter = (char)('a' + num);
var textMesh = item.GetComponent<tk2dTextMesh>();

textMesh.text = letter.ToString();
textMesh.Commit();
}
}

At this point I can hit the "Commit" button on the Sprite Batcher script and the objects get baked into one mesh.
How can I do the Commit dynamically?

Thanks.
Petur

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Help with dynamic TextMesh batching
« Reply #1 on: August 26, 2013, 04:16:57 pm »
Why do you want to do that? Unity will dynamically batch text meshes even without the static sprite batcher. While you CAN batch using the static sprite batcher dynamically, that isn't what it was designed for - it was for things that don't change dynamically, hence the "static sprite batcher".

peturg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Help with dynamic TextMesh batching
« Reply #2 on: August 26, 2013, 04:48:15 pm »
Thank you for the quick reply.

The thing is I actually have a few thousand TextMeshes in different "container" GameObjects. The drawCalls is around 80 now. I just noticed that when I added the spriteBatcher to few of the containers and hit Commit the drawCalls dropped. So I was interested in trying it.

Each textMesh has a random text, scale, alpha, position and rotation.

Any help would be appreciated.
Petur

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Help with dynamic TextMesh batching
« Reply #3 on: August 26, 2013, 06:12:39 pm »
A few thousand text meshes all visible at the same time? You could merge text meshes into the static sprite batcher, but the API isn't "public" and its likely to change. Its not exactly "user friendly" either. It may sound like I'm dissuading you from doing this, and thats because the you'll be relying on non public interfaces which are likely to change in future versions.

Also, the static sprite batchers aren't particularly optimized for frame by frame manipulation, so be careful in working with it - you could end up with a completely different set of bottlenecks after you've done this.

One thing you could do to get an almost immediate gain now, is to set the maxChars on the text mesh to 1. Since you're only creating textmeshes with one character, this should improve things greatly.

peturg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Help with dynamic TextMesh batching
« Reply #4 on: August 26, 2013, 07:12:30 pm »
I looked into the tk2dStaticSpriteBatcherEditor class. I was able to fish out what I needed.
But I appreciate the warning.