LoginSignup
4
1

More than 5 years have passed since last update.

【Unity】TextMeshにカウントアップを表示する

Posted at

やったこと

  • 3D空間にText Meshを使って、テキストを表示
  • 秒でカウントアップ
  • ある秒数に達したら、"FINISH"を表示

前提

  • UnityがPCに入っている かつ ビルドできるようになっている

作る

TextMeshを作成する
  • 左ペインで右クリックメニューを選択し、空のObjectを作成する(Create Empty)

    スクリーンショット 2018-07-11 18.52.31.png

  • 空のObjectのInspectorでText MeshをAdd Componentする(textで検索するとすぐ見つかる)

    スクリーンショット 2018-07-18 18.45.29.png

  • Textに初期表示の"TIME"を入れる(Font Sizeを大きくしておくと、文字がにじまない)

    スクリーンショット 2018-07-18 18.41.17.png

  • Sceneに"TIME"というText Meshが表示される

Scriptを書く
  • Assetsの下で右クリックし、Create<C# Scriptを選択

    スクリーンショット 2018-07-18 18.54.46.png

  • コードを書く

    C#
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class MeasureTime : MonoBehaviour {
    GameObject time;
    int timeLimit;
    float deltaTime;
    int intNowTime;
    string strNowTime;
    // Use this for initialization
    void Start () {
        Debug.Log(":::::START:::::");
        time = GameObject.Find("Time");   // 時間計測用GameObjectの取得
        timeLimit = 10;   // 制限時間
    }
    // Update is called once per frame
    void Update () {
        deltaTime += Time.deltaTime;   // 経過時間
        intNowTime = (int)deltaTime;   // 経過時間の整数部分
        timeLimit = timeLimit - intNowTime;   // 実際の秒数
        strNowTime = intNowTime.ToString();   // TextMeshGameObjectに代入するためにString型にする
        time.GetComponent<TextMesh> ().text = strNowTime;
        // 制限時間経過時の設定
        if(intNowTime > 10){
            time.GetComponent<TextMesh> ().text = ":::::FINISH:::::";
        }
    }
    }
    
ScriptをText Meshにアタッチする
  • Assetsの下のC#のファイルをHierarchyのObjectにdrag & dropする
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1