2D Toolkit之 TextMesh绑定脚本

本例用Q键来增加分数,代码如下:

using UnityEngine;
using System.Collections;
public class TextMeshExample : MonoBehaviour {
        tk2dTextMesh textMesh;
        int score = 0;
        // Use this for initialization
        void Start () {
                textMesh = GetComponent<tk2dTextMesh>();
        }
        // Update is called once per frame
        //ngui教程:http://www.misoft.com.cn/
        void Update () {
                if (Input.GetKey(KeyCode.Q))
                {
                        score++;
                        textMesh.text = "SCORE: " + score.ToString();
                        // This is important, your changes will not be updated until you call Commit()
                        // This is so you can change multiple parameters without reconstructing
                        // the mesh repeatedly
                        textMesh.Commit();
                }
        }
}

   将脚本绑定到场景中的TextMesh上,执行,测试效果。

   另外,缩放代码:

   textMesh.scale = Vector3(xScale, yScale, zScale);

   颜色代码:

   textMesh.color = Color.red;

   如果允许渐变,设置第二颜色如下:

   textMesh.color2 = Color.green;

   注意:虽然你可以在代码中改变Max Chars的值,但是你应该避免这样做,因为运行时会重新分配内存。