気軽に遊べるミニゲームを制作しています

ブラウザゲームまとめ

Unity 時間を変更する

Unity C#

ポーズ画面 Time.timeScale=0;

Time.timeScaleを変化させることで時間の速さをすることができます。
デフォルトでは1になっており、これを0にすることでゲーム時間を止めることができます。

void Update() //この関数内の処理は止まらない
void FixedUpdate() //これは止まる

ボタンからTimeStop関数を呼び出した時に、時間を止めたり、動かしたりできるスクリプトを作成します。確認用にupdate内とfixupdate内で処理が止まるかテキストに表示します。

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

public class TimeControl : MonoBehaviour
{

    private bool timeStop = false;

    public void TimeStop()
    {
        if (!timeStop)
        {
            Time.timeScale = 0;
            timeStop = true;
        }

        else
        {
            Time.timeScale = 1;
            timeStop = false;
        }

    }

    //以下は、確認用
    public Text updateTxt;
    private int updateCount = 0;

    public Text fixUpdateTxt;
    private int fixUpdateCount = 0;

    private void Update()
    {
        updateCount++;
        updateTxt.text = updateCount.ToString();
    }

    private void FixedUpdate()
    {
        fixUpdateCount++;
        fixUpdateTxt.text = fixUpdateCount.ToString();
    }
}

テキストの表示からtimeScale=0では Update関数内の処理は止まらないことがわかります。

数秒後に処理を実行させる Invoke(“関数名”,秒数);

Invokeを使用することにより、数秒後に処理を実行させることができます。
以下は、2秒後にtimescaleを0にするスクリプトになります。

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

public class TimeControl : MonoBehaviour
{

    private void Start()
    {
        Invoke("TimeStop", 2);
    }

    private void TimeStop()
    {
        Time.timeScale = 0;
    }

    //以下は、確認用
    public Text updateTxt;
    private float timer;

    private void Update()
    {
        timer += Time.deltaTime;
        updateTxt.text = timer.ToString("f2");
    }

}

2秒後にゲーム時間がストップしました。

コルーチン private IEnumerator 関数名

数秒おきの処理を実行したい時、コルーチンという処理が便利です。
型が特殊でとっつきづらいですが、覚える必要があるのは以下です。

関数の型:private IEnumerator 関数名 //voidをIEnumeratorにする
関数を実行したいとき:StartCoroutine(関数名());

IEnumerator関数内で yield… の記述をすることで、以下の処理を行うことができる。
yield return null; //1フレーム待機
yield return new WaitForSeconds( float ); //x秒待機
yield break; //コルーチン停止 などなど…

コルーチン内で無限ループ(while)を使って、1秒おきにゲーム時間を1/4にさせます。

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

public class TimeControl : MonoBehaviour
{

    private bool timeSlow = false;
    public Text updateTxt;
    public Text timeTxt;
    private float updateTimer;
    private float timer;

    private void Start()
    {
        StartCoroutine(TimeSlow());
    }

    private IEnumerator TimeSlow()
    {
        yield return new WaitForSeconds(1.0f);

        while (true)
        {

            if (!timeSlow)
            {
                timeSlow = true;
                Time.timeScale = 0.25f;
                updateTimer = 0;
            }
            yield return new WaitForSeconds(1.0f);
        }

    }


    private void Update()
    {
        updateTimer += Time.deltaTime;
        updateTxt.text = updateTimer.ToString("f2");

        if (updateTimer > 0.25f)
            {
                timeSlow = false;
                Time.timeScale = 1;
            }

    }

}

コメント

タイトルとURLをコピーしました