2D Toolkit Forum
2D Toolkit => Support => Topic started by: peturg 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.
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
-
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?
-
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.
-
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
-
Yes, that would be helpful.
Thanks
-
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.
-
Thank you, that guided me in the right direction.
If anyone is interested in this.
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();