本例用Q鍵來增長分數,代碼以下:html
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的值,可是你應該避免這樣作,由於運行時會從新分配內存。
ide