본문 바로가기
유니티강좌

스톱워치 만들기 | Start(시간증가), Pause(일시정지), Reset(시간 초기화) | - 유니티 2D게임 개발(Unity & C#) 튜토리얼

by Ncube 2020. 12. 24.

 

 

Start버튼 클릭시 시간증가, Pause버튼 클릭시 일시정지, Reset버튼 클릭시 시간 초기화하는 스톱워치 만들기 강좌입니다. apk파일을 안드로이드 스마트폰에서 실행하여 테스트하는 영상추가. 

 

#스톱워치만들기  #Stopwatch  #유니티2D게임개발튜토리얼

 

 

[Stopwatch.cs]

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

public class Stopwatch : MonoBehaviour
{
    [SerializeField] float timeStart;
    [SerializeField] Text timeText, startPauseText;

    bool timeActive = false;

    // Start is called before the first frame update
    void Start()
    {
        timeText.text = timeStart.ToString("F2");
    }

    // Update is called once per frame
    void Update()
    {
        StartTime();
    }

    void StartTime()
    {
        if (timeActive)
        {
            timeStart += Time.deltaTime;
            timeText.text = timeStart.ToString("F2");
        }
    }

    public void StartPauseBtn()
    {
        timeActive = !timeActive;
        startPauseText.text = timeActive ? "PAUSE" : "START";
    }

    public void ResetBtn()
    {
        if(timeStart > 0)
        {
            timeStart = 0f;
            timeText.text = timeStart.ToString("F2");
        }
    }
}

 

댓글