コード上からもtk2dTextMeshに簡単にアクセスでき、パラメーターを制御できます。この例では、Qキー押が押されているときに加算されるスコアカウンタを作ります。
projectパネルにC#スクリプトを作って「TextMeshExample」という名前にし、以下のコードをコピペしてください。
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
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();
}
}
}
シーン中のテキストメッシュに、このスクリプトをアタッチし、ゲームを実行してみてください。
スコアQキーを押している間、スコアが増加していくのを確認してください。
スケール変更も可能です。「dynamic batching 」を壊さずにスケーリングできます。
textMesh.scale = Vector3(xScale, yScale, zScale);
色の変更も可能です
textMesh.color = Color.red;
もし「Use Gradient」を有効にしているなら、グラデーション用の「color2」も変更可能です。
textMesh.color2 = Color.green;
注) コード中で「maxChars」も変更できますが、メモリの再割り当てが起きるため、ランタイム中でするのは避けたほうがいいです。
最終更新:2011年11月12日 16:42