본문 바로가기
Project/<team Not Same> 꿈의 왕국 : 영원한 보금자리

[R&D] Energy bar Slider

by 왹져박사 2023. 4. 11.
728x90

기기의 시간을 가져오는 DateTime과 DateTime간의 간격을 저장하는 TimeSpan

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

public class UIEnergy : MonoBehaviour
{
    public Slider energySlider;
    public TMP_Text txtTime;
    public TMP_Text txtEnergy;

    public Button btnUseEnergy;

    private int energy = 5;
    private int maxEnergy = 5;
    private int energyTime = 3;
    private bool chargeOn = false;

    private DateTime chargeStartTime;

    void Start()
    {
        this.btnUseEnergy.onClick.AddListener(() =>
        {
            Debug.Log("btnUseEnergy clicked");
            if(this.energy!=0) this.energy--;

            if (this.chargeOn == false)
            {
                this.chargeOn = true;
                this.chargeStartTime = DateTime.Now;
            }
        });
    }

    private void Update()
    {
        if (this.chargeOn == true)
        {
            TimeSpan timeCal = DateTime.Now - this.chargeStartTime;
            int timeSeconds = timeCal.Seconds;
            int min = (this.energyTime - timeSeconds) / 60;
            int sec = (this.energyTime - timeSeconds) % 60;
            this.txtTime.text = String.Format("{0:00}:{1:00}", min, sec);

            if (timeSeconds >= energyTime)
            {
                this.energy++;
                if (this.energy == this.maxEnergy)
                {
                    this.txtTime.text = "00:00";
                    Debug.Log("charge off");
                    this.chargeOn = false;
                }
                this.chargeStartTime = DateTime.Now;

            }
        }

        this.txtEnergy.text = String.Format("{0}/{1}", this.energy, this.maxEnergy);

        this.energySlider.value = this.energy;
    }
}

https://learn.microsoft.com/ko-kr/dotnet/api/system.datetime?view=net-7.0 

 

DateTime 구조체 (System)

일반적으로 날짜와 시간으로 표시된 시간을 나타냅니다.

learn.microsoft.com

https://learn.microsoft.com/ko-kr/dotnet/api/system.timespan?view=net-7.0 

 

TimeSpan 구조체 (System)

시간 간격을 나타냅니다.

learn.microsoft.com

분 단위까지 표시

 

시간이 지나면 +1

https://narmhye.tistory.com/97

 

[R&D] Ticket charge

앞의 Energy Slider에서 오프라인에서 접속하였을 경우 추가, ticket에 맞게 변경 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using System; public class UIEnergy : Mon

narmhye.tistory.com

 

728x90