タイムを表示する

経過時間を表示する

Time.realtimeSinceStartupで、スタートしてからの経過時間を取得できるので、あとはそれを画面に表示させるだけです。

取得できる値はfloat型なので、整数の値を表示させたい場合にはintに型変換(キャストと言います)を行います。 さらに、テキストとして表示する際にstringに型変換します。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TimeText : MonoBehaviour {
	void Update () {
		//ゲームを開始してからの時間を取得
		float time = Time.realtimeSinceStartup;

		//小数点を削除
		int count = (int) time;

		//テキストを反映するテキストエリアを取得
		Text textArea = gameObject.GetComponent<Text> ();

		//テキストを表示する
		textArea.text = count.ToString();
	}
}

残り時間、タイムアップを表示する

残り時間を表示したい場合にはタイムリミットを先に設定しておき、差分の値を画面上に表示するようにプログラミングします。

ifの条件分岐で振り分けて、タイムアップ後の処理も記載しておきましょう。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LimitText : MonoBehaviour {

	public int limit = 10;//タイムアップの秒数

	void Update () {
		//ゲームを開始してからの時間を取得
		float time = Time.realtimeSinceStartup;

		//残り時間を調べる
		float last = limit - time;

		//テキストを反映するテキストエリアを取得
		Text textArea = gameObject.GetComponent<Text> ();

		if (last > 1) { //残り時間が1より大きい場合には

			//小数点を削除
			int count = (int) last;

			//テキストを表示する
			textArea.text = count.ToString();

		} else { //それ以外は

			//タイムアップのテキストを表示する
			textArea.text = "Timeup";
		}
	}
}