Hello Guest

Author Topic: Text Mesh loop through each character  (Read 5667 times)

peturg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Text Mesh loop through each character
« on: October 02, 2012, 03:04:08 pm »
Hi, how can I loop through all the characters in a text mesh and change their position e.g?

So this is basically what I got.
Code: [Select]
GameObject item = Instantiate(prefab, transform.position, transform.rotation) as GameObject;
var textMesh = item.GetComponent<tk2dTextMesh>();
string text = "Hello 2D Toolkit";
textMesh.maxChars=text.Length;
textMesh.text = text;
textMesh.Commit();

Any input appreciated.
Petur


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Text Mesh loop through each character
« Reply #1 on: October 02, 2012, 04:22:46 pm »
You want to change the position of individual characters in a textmesh? Alas that isn't possible without changes to the codebase. Any particular reason you need to do this?

peturg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Text Mesh loop through each character
« Reply #2 on: October 02, 2012, 04:46:45 pm »
Yes I would like to change the position and possibly the scale of individual char in a text mesh.
The idea was to animate each char to a different Z position and at some point back to the original position.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Text Mesh loop through each character
« Reply #3 on: October 02, 2012, 04:50:10 pm »
Just so I understand this correctly, you want to animate the z position of each character? I can tell you where to add the code if that would be helpful

peturg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Text Mesh loop through each character
« Reply #4 on: October 02, 2012, 04:54:41 pm »
Yes, that would be helpful.
Thanks

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Text Mesh loop through each character
« Reply #5 on: October 02, 2012, 05:04:28 pm »
In tk2dTextMesh.cs,
Look for the function FillTextData()
In there, look for vertices[target * ...] = new Vector3(...);

That block gets run once per character, so you're free to tweak it per drawn character. You can easily change the position per character or per line, or whatever you choose.

Hope that helps.

peturg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Text Mesh loop through each character
« Reply #6 on: October 02, 2012, 06:22:26 pm »
Thank you, that guided me in the right direction.

If anyone is interested in this.
Code: [Select]
GameObject item = Instantiate(prefab, transform.position, transform.rotation) as GameObject;
var textMesh = item.GetComponent<tk2dTextMesh>();
string text = "Hello 2D Toolkit";
textMesh.maxChars=text.Length;
textMesh.text = text;
textMesh.Commit();

MeshFilter mf = textMesh.GetComponent<MeshFilter>();
int n;
Vector3[] vertices = mf.mesh.vertices;
for(int k=0; k<text.Length; k++)
{
n = Random.Range(-3,3);

for(int j=0; j < 4; j++)
{
vertices[k*4 + j].z += n;
}
}

mf.mesh.vertices = vertices;
mf.mesh.RecalculateBounds();