Hello Guest

Author Topic: Setting Color to Text Mesh after modifying its vertices  (Read 4512 times)

peturg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Setting Color to Text Mesh after modifying its vertices
« on: October 03, 2012, 12:53:03 pm »
After changing vertices in Text Mesh I?m not able to set a new Color to it.

Am I missing something ?

Example:
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;
Color c = textMesh.color;
c = Color.red;
textMesh.color = c;
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();

Color c1 = textMesh.color;
c1 = Color.blue;
textMesh.color = c1;
textMesh.Commit();

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Setting Color to Text Mesh after modifying its vertices
« Reply #1 on: October 03, 2012, 01:02:36 pm »
When you do meshFilter.mesh it will copy the mesh. Don't do that, use sharedMesh instead.

peturg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Setting Color to Text Mesh after modifying its vertices
« Reply #2 on: October 03, 2012, 01:14:45 pm »
Thanks for quick reply. It works.