Unity解説 コルーチンを用いたストップウォッチの作成
Unityのコルーチンを使ったストップウォッチをコードで作ったよ。

 
 
空のオブジェクトにアタッチして、ゲーム再生をすると起動する💨

 
 
【Counter001.cs】
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Counter001 : MonoBehaviour
{
    private GUIStyle fontStyle001;
    private GUIStyleState styleState001;
    private float Seconds = 0.0f;
    public bool CountupFlag = false;
    private string TimerText;
    void Start()
    {
        //タイマーテキストのフォント設定
        fontStyle001 = new GUIStyle();
        fontStyle001.fontSize = 100;
        styleState001 = new GUIStyleState();
        styleState001.textColor = Color.black;
        fontStyle001.normal = styleState001;
    }    
    //Updateでは、timeScaleで一時停止できないため、FixedUpdateとする。
    void FixedUpdate()
    {
        if(CountupFlag == false)
        {
            StartCoroutine(Countup());
        }
    }
    IEnumerator Countup()
    {
        CountupFlag = true;
        Seconds += 0.1f;
        yield return new WaitForSeconds(0.1f);
        CountupFlag = false;
    }
    void OnGUI()
    {
        TimerText = Seconds.ToString("F1");
        GUI.Label (new Rect (50, 50, 150, 50), TimerText,fontStyle001);
        if (GUI.Button(new Rect(250, 75, 100, 50), "停止・再開"))
        {
            //クリック時の応答・処理
            if(Time.timeScale != 0){
                Time.timeScale = 0;
            }else{
                Time.timeScale = 1.0f;
            }           
        }        
    }    
}
【実行結果】

【参考】
お名前

