Unity에서 제한 시간 3분을 추가하여 카운트다운하는 방법

1586 단어 Unitytech

OS 사용


Mac

본 문서에 사용된 Unity 버전


Version 2020.3.18f1

하고 싶은 일

  • Unity 화면에 남은 시간을 카운트다운하는 문자열
  • 제한 시간은 03:00으로 시작하여 "02:59"→"00:00"
  • 메서드

  • 텍스트를 추가하여 적당한 위치와 글꼴 크기로 조정
  • 아래 스크립트를 만들고 Text
  • 를 첨부합니다.
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class TimeCounter : MonoBehaviour
    {
        public int countdownMinutes = 3;
        private float countdownSeconds;
        private Text timeText;
    
        private void Start()
        {
            timeText = GetComponent<Text>();
            countdownSeconds = countdownMinutes * 60;
        }
    
        void Update()
        {
            countdownSeconds -= Time.deltaTime;
            var span = new TimeSpan(0, 0, (int)countdownSeconds);
            timeText.text = span.ToString(@"mm\:ss");
    
            if (countdownSeconds <= 0)
            {
                // 0秒になったときの処理
            }
        }
    }
    
  • 게임을 시작하면 다음과 같다

  • 보태다

  • countdown Minutes에서 제한 시간을 몇 분으로 설정하시겠습니까?기본 설정은 3분입니다.
  • countdownSeconds는 실제 초수입니다.분을 지정했기 때문에 60을 썼다.제한 시간이 1분 이내인 경우 초를 직접 지정하십시오.
  • "mm:ss"의 장소는 각자의 게임에 따라 사용자 정의합니다.
  • 참고 자료


    이것을 참고하게 해 주세요.감사합니다.
    https://qiita.com/Nossa/items/70487b765ec9332e0db0

    좋은 웹페이지 즐겨찾기