text倒计时

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

public class Timert : MonoBehaviour
{
    public int second = 120;

    private Text txtTimer;
    private float nextTime = 1;//下一次改变时间
    // Start is called before the first frame update
    void Start()
    {
        txtTimer = GetComponent<Text>();
    }

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

    }
    private void Timer1()
    {
        //如果时间到了 
        if (nextTime <= Time.time)
        {
            second--;//119       1:59 
            txtTimer.text = string.Format("{0:d2}:{1:d2}", second / 60, second % 60);
            nextTime = Time.time + 1;//在当前时间上增加1s

            if (second <= 10) txtTimer.color = Color.red;
            if (second <= 0) enabled = false;
        }
    }

}
using UnityEngine;
using UnityEngine.UI;

public class Timert : MonoBehaviour
{
    public int second = 120;

    private Text txtTimer;
    private float totalTime=0;//下一次改变时间
    // Start is called before the first frame update
    void Start()
    {
        txtTimer = GetComponent<Text>();
    }

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

    }
    
    private void Timer2()
    {
        //累加每帧消耗时间
        totalTime += Time.deltaTime;

        //如果1s
        if (totalTime >= 1)
        {
            second--;//119       1:59 
            txtTimer.text = string.Format("{0:d2}:{1:d2}", second / 60, second % 60);
            totalTime = 0;//清空累加的时间
            if (second <= 10) txtTimer.color = Color.red;
            if (second <= 0) enabled = false;
        }
    }


}
using UnityEngine;
using UnityEngine.UI;

public class Timert : MonoBehaviour
{
    public int second = 120;

    private Text txtTimer;
    
    // Start is called before the first frame update
    void Start()
    {
        txtTimer = GetComponent<Text>();
        //重复调用(要执行的方法名称,开始调用时间,调用间隔)
        InvokeRepeating("Timer", 0, 1);
       //延迟调用
        //Invoke("需要调用的方法名称", 调用时间);
    }

  
    private void Timer()
    {
             second--;//119       1:59 
            txtTimer.text = string.Format("{0:d2}:{1:d2}", second / 60, second % 60);
           
            if (second <= 10) txtTimer.color = Color.red;
            // 取消调用
            if (second <= 0) {CancelInvoke("Timer");}
       
    }


}

### 创建倒计时 Text 组件 要在 Unity 中创建并配置一个能够显示倒计时的 `Text` 组件,需遵循几个重要步骤。首先,在项目中添加必要的 UI 元素,并通过脚本逻辑来驱动这些元素的行为。 #### 添加必需的命名空间 为了访问和操作 UI 控件,如 `Text` 组件,必须在 C# 脚本顶部声明对 `UnityEngine.UI` 命名空间的支持[^2]: ```csharp using UnityEngine; using UnityEngine.UI; // 导入UI库以便使用Text组件和其他UI控件 ``` #### 设定公共属性 定义公开字段允许从 Inspector 面板轻松分配资源给脚本实例。对于倒计时应用而言,至少应包含两个成员变量:一个是表示总倒计时时长的浮点数;另一个是指向负责展示剩余时间的文字标签的对象引用[^4]。 ```csharp public class Countdown : MonoBehaviour { public float countTime = 10f; // 设置初始倒计时长度(单位为秒) public Text countTimeText; // 关联到场景里的Text对象 } ``` #### 编协程函数处理定时任务 利用协程可以简化异步编程模型下的延时执行需求。下面的例子展示了如何构建一个名为 `CountdownCoroutine` 的迭代器模式方法,它会在每一秒钟减少一次计数值直到达到零为止,并同步更新界面上对应的文本内容[^5]。 ```csharp IEnumerator CountdownCoroutine() { int remainingSeconds; while (countTime >= 0) { remainingSeconds = Mathf.FloorToInt(countTime); if (countTimeText != null && !string.IsNullOrEmpty(countTimeText.text)) { countTimeText.text = remainingSeconds.ToString(); } yield return new WaitForSeconds(1); countTime -= 1; } Debug.Log("Countdown finished!"); } ``` #### 启动协程 最后一步是在合适的地方调用上述定义好的协程过程。通常情况下会选择在游戏开始时立即激活此功能,因此可以在 `Start()` 函数内部完成这一步骤。 ```csharp void Start(){ StartCoroutine(CountdownCoroutine()); } ``` 以上就是关于如何在 Unity 中建立简单的基于文本形式呈现出来的倒计时系统的介绍。当然实际开发过程中可能还会涉及到更多细节上的调整优化工作,比如美化界面样式、增加额外交互特性等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值