Unity 倒计时

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QFramework;
using UnityEngine.UI;
using System;
using TMPro;
public class TimeCountdown :MonoBehaviour
{
    public UnityEngine.Events.UnityEvent onTimeCountDown;
    public TextMeshProUGUI text;
    public float TotleTime = 30;

    float startTime;

 

    float EndTime;

    float CurTime;

    bool startT;

    public void SetTotleTime(float totleTime) {
        TotleTime = totleTime;
        text.text = StringFormat(0);
    }

    public void StartCountDown(float totleTime)
    {
        TotleTime = totleTime;
        startTime = Time.time;
        startT = true;
        SetCurTimeZero();
    }

    public float EndCountDown()
    {
        EndTime = CurTime;
 
        startT = false;
        return CurTime;
    }

    public void RefreshTime() {
        text.text = StringFormat(0);
    }

    public void CloseCountDown() {
        startT = false;
        RefreshTime();
    }
    void SetCurTimeZero()
    {
        CurTime = 0;
    }
    public void Update()
    {

        if (!startT) return;

        CurTime = Time.time - startTime;

        if (CurTime >= TotleTime)
        {
            CurTime = TotleTime;
            Debug.Log("时间到");
  
            startT = false;
            if (onTimeCountDown != null)
                onTimeCountDown.Invoke();
        }

        if (text != null)
        {
            if (CurTime<=0) CurTime = 0;
           
            text.text = StringFormat(CurTime);
        }
    }



    private string StringFormat(float time)
    {

        var temp = TotleTime - time;

        var t = TempFormat(temp);

        var m = Temp2Time((temp / 60));

        var s = Temp2Time((temp % 60));

        var str = string.Format("{0}:{1}", m, s);

        return str;

    }

    private string Temp2Time(float time)
    {
        return time > 10 ? TempFormat((time)) : "0" + TempFormat((time));
    }

    private string TempFormat(float f)
    {
        var t = Mathf.FloorToInt(f);
        return t.ToString();
    }

    public float GetCurTime()
    {
        return CurTime;
    }

    public int getLeftTime()
    {
        return (int)(TotleTime - CurTime); 
    }
}

 

StartCountDown  :  开始倒计时,参数为倒计时时间


GetCurTime   : 获取已用时间


getLeftTime  :   返回剩余时间

 



FR:海涛高软(Hunk Xu) 
QQ技术交流群:386476712

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity 中实现倒计时可以通过以下步骤: 1. 创建一个空的 GameObject,给它起一个名字比如 "CountdownTimer"。 2. 在 CountdownTimer 上新增一个 C# 脚本,比如叫做 "CountdownTimer.cs"。 3. 在 CountdownTimer.cs 中,定义一个 public 变量用来存储倒计时的时间,比如叫做 "timeLeft"。 4. 在 Start 方法中初始化 timeLeft 变量,比如设置为 60 秒。 5. 在 Update 方法中,每帧更新 timeLeft 变量,比如通过 Time.deltaTime 函数来减去经过的时间。 6. 在 Update 方法中,检查 timeLeft 是否已经小于等于 0,如果是,就执行倒计时结束后的操作,比如触发游戏失败等。 7. 在 UI 中显示倒计时的剩余时间,可以使用 Text 组件和 CountdownTimer.cs 中的 timeLeft 变量来实现。 下面是一个示例代码: ``` using UnityEngine; using UnityEngine.UI; public class CountdownTimer : MonoBehaviour { public float timeLeft = 60.0f; private Text countdownText; void Start() { countdownText = GetComponent<Text>(); } void Update() { timeLeft -= Time.deltaTime; countdownText.text = "Time Left: " + Mathf.RoundToInt(timeLeft).ToString(); if (timeLeft <= 0) { // do something when countdown is finished } } } ``` 在上面的示例代码中,我们使用了 Text 组件来显示倒计时的剩余时间,并且在 Update 方法中每帧更新了这个组件的文本内容。同时,在 timeLeft 小于等于 0 时,我们可以执行一些游戏失败的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值