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

[R&D] Ticket charge

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

앞의 Energy Slider에서 오프라인에서 접속하였을 경우 추가, ticket에 맞게 변경

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

public class UIEnergy : MonoBehaviour
{
    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)
            {
                if(timeSeconds-energyTime > energyTime)
                {
                    int n = 0;
                    while (timeSeconds < energyTime)
                    {
                        n++;
                        timeSeconds -= energyTime;
                    }
                    this.energy += n;
                }
                else
                {
                    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);
    }
}

https://narmhye.tistory.com/96

 

[R&D] Energy bar Slider

기기의 시간을 가져오는 DateTime과 DateTime간의 간격을 저장하는 TimeSpan using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using System; public class UIEnergy : MonoBehaviou

narmhye.tistory.com

 

728x90