Unity のカウントダウン タイマーのチュートリアル

カウントダウン タイマー は、設定された時間から 0 までカウントする仮想時計です。

Unity でカウントダウン タイマーを作成するには、カウントダウンする時間を保存し、00:00 形式で表示するスクリプトを作成する必要があります。

Unity のカウントダウン タイマーの左上隅。

タイマーには次の形式が含まれます。

  • 日:時:分:秒:ミリ秒
  • 時:分:秒:ミリ秒
  • 分:秒:ミリ秒
  • 秒:ミリ秒
  • 上記すべてに加えて、ミリ秒なし

ステップ

Unity でカウントダウン タイマーを作成するには、次の手順に従います。

  • という新しいスクリプトを作成し、'SC_CountdownTimer' という名前を付け、そこからすべてを削除して、以下のコードを貼り付けます。
  • カウントダウン タイマー C# スクリプトは、0 に達するまで合計値から減算し、書式設定された時間を Text 要素に適用します。

SC_カウントダウンタイマー.cs

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

public class SC_CountdownTimer : MonoBehaviour
{
    public enum CountdownFormatting { DaysHoursMinutesSeconds, HoursMinutesSeconds, MinutesSeconds, Seconds };
    public CountdownFormatting countdownFormatting = CountdownFormatting.MinutesSeconds; //Controls the way the timer string will be formatted
    public bool showMilliseconds = true; //Whether to show milliseconds in countdown formatting
    public double countdownTime = 600; //Countdown time in seconds

    Text countdownText;
    double countdownInternal;
    bool countdownOver = false;

    // Start is called before the first frame update
    void Start()
    {
        countdownText = GetComponent<Text>();
        countdownInternal = countdownTime; //Initialize countdown
    }

    void FixedUpdate()
    {
        if (countdownInternal > 0)
        {
            countdownInternal -= Time.deltaTime;

            //Clamp the timer value so it never goes below 0
            if (countdownInternal < 0)
            {
                countdownInternal = 0;
            }

            countdownText.text = FormatTime(countdownInternal, countdownFormatting, showMilliseconds);
        }
        else
        {
            if (!countdownOver)
            {
                countdownOver = true;

                Debug.Log("Countdown has finished running...");

                //Your code here...
            }
        }
    }

    string FormatTime(double time, CountdownFormatting formatting, bool includeMilliseconds)
    {
        string timeText = "";

        int intTime = (int)time;
        int days = intTime / 86400;
        int hoursTotal = intTime / 3600;
        int hoursFormatted = hoursTotal % 24;
        int minutesTotal = intTime / 60;
        int minutesFormatted = minutesTotal % 60;
        int secondsTotal = intTime;
        int secondsFormatted = intTime % 60;
        int milliseconds = (int)(time * 100);
        milliseconds = milliseconds % 100;

        if (includeMilliseconds)
        {
            if (formatting == CountdownFormatting.DaysHoursMinutesSeconds)
            {
                timeText = string.Format("{0:00}:{1:00}:{2:00}:{3:00}:{4:00}", days, hoursFormatted, minutesFormatted, secondsFormatted, milliseconds);
            }
            else if (formatting == CountdownFormatting.HoursMinutesSeconds)
            {
                timeText = string.Format("{0:00}:{1:00}:{2:00}:{3:00}", hoursTotal, minutesFormatted, secondsFormatted, milliseconds);
            }
            else if (formatting == CountdownFormatting.MinutesSeconds)
            {
                timeText = string.Format("{0:00}:{1:00}:{2:00}", minutesTotal, secondsFormatted, milliseconds);
            }
            else if (formatting == CountdownFormatting.Seconds)
            {
                timeText = string.Format("{0:00}:{1:00}", secondsTotal, milliseconds);
            }
        }
        else
        {
            if (formatting == CountdownFormatting.DaysHoursMinutesSeconds)
            {
                timeText = string.Format("{0:00}:{1:00}:{2:00}:{3:00}", days, hoursFormatted, minutesFormatted, secondsFormatted);
            }
            else if (formatting == CountdownFormatting.HoursMinutesSeconds)
            {
                timeText = string.Format("{0:00}:{1:00}:{2:00}", hoursTotal, minutesFormatted, secondsFormatted);
            }
            else if (formatting == CountdownFormatting.MinutesSeconds)
            {
                timeText = string.Format("{0:00}:{1:00}", minutesTotal, secondsFormatted);
            }
            else if (formatting == CountdownFormatting.Seconds)
            {
                timeText = string.Format("{0:00}", secondsTotal);
            }
        }

        return timeText;
    }
}
  • [階層] ビューを右クリックして、[UI] -> [テキスト] を選択し、新しい UI テキストを作成し、名前を付けます。 'Countdown'

Unity で新しい UI テキストを作成する

  • 'Countdown' Rect Transform の位置合わせを左上に、ピボットを (0, 1) に、位置 X と位置 Y を 5 に、幅を 300 に、高さを 60 に変更します。

  • 'Countdown' テキストのフォント スタイルを太字、フォント サイズを 34、配置を左中央、色を白に変更します。

Unity テキスト コンポーネント インスペクター Arial 太字フォント サイズ 34

  • SC_CountdownTimer スクリプトを Text コンポーネントを持つ 'Countdown' オブジェクトにアタッチします。

スクリプトにはいくつかの変数があることがわかります。

  • カウントダウン形式 は、文字列形式に含める時間単位を制御します。
  • Show Milliseconds は、ミリ秒カウントを表示するかどうかを制御します。
  • カウントダウン時間 はカウントダウンの継続時間を秒単位で表します。たとえば、値 600 は 10 分に相当します。

Play を押すと、テキストにカウントダウン タイマーが入力されていることがわかります。

0 秒で、スクリプトはコンソールに 1 行を出力し、カウントダウンが終了したことを通知します。スクリプトのその部分を使用して独自の機能を追加します。

おすすめの記事
ゾーン コントローラー プロ - Unity アセット ストア パッケージ
Unity で新しい HDRP 給水システムを使用する方法
FPC Swimmer - 没入型水環境向けの包括的な Unity アセット
Ultimate Spawner 2.0 - 革新的な資産
Unity 用のマウスの外観スクリプト
Weather Maker - Unity 環境を新たな高みに引き上げる
Unity で Xbox コントローラーを使用する方法