【Unity】UI面板:倒计时器

一、添加文本(UI -> text)

【Unity】UI面板:倒计时器

二、创建脚本(CountdownTimer)

第一种方法:

1、首先在方法外声明两个变量

private Text txtTimer;    //存放组件的变量
public int second = 120;    //倒计时时间120秒

2、在Start方法内获取文本组件(需要引入UI命名空间:using UnityEngine.UI; )

private void Start()
    {
        //查找组件的 方法一    (适合查找一个,多个的话需要对变量遍历)
        txtTimer = GetComponent<text>();
        //&#x67E5;&#x627E;&#x7EC4;&#x4EF6;&#x7684; &#x65B9;&#x6CD5;&#x4E8C;    &#xFF08;&#x6309;&#x7167;&#x6307;&#x5B9A;&#x540D;&#x79F0;&#x67E5;&#x627E;&#xFF09;
        txtTimer = GameObject.Find("TextTimer").GetComponent<text>();

    }</text></text>

3、定义第一种方法需要使用的变量(记录当前时间的下一秒,用来判断执行代码)

private float nextTime = 1;//&#x4E0B;&#x6B21;&#x4FEE;&#x6539;&#x65F6;&#x95F4;

4、定义Timer1方法

private void Timer1()
    {
        Debug.Log("Timer1 &#x88AB;&#x8C03;&#x7528;");    // &#x6253;&#x5370;&#x8C03;&#x8BD5;
        if (second >=0)    // second < 0 &#x505C;&#x6B62;&#x5012;&#x8BA1;&#x65F6;
        {
            if (second <= 10) 小于10秒则倒计时字体变红 { txttimer.color="Color.red;" } if (time.time>= nextTime)    // &#x5F53;&#x5F00;&#x8BA1;&#x65F6;&#x7684;&#x65F6;&#x95F4;&#x5927;&#x4E8E;&#x5B9A;&#x4E49;&#x7684;1&#x79D2;
            {
                second--;    //&#x51CF;&#x4E00;&#x79D2;&#x949F;
                txtTimer.text = string.Format("{0:d2}:{1:d2}", second / 60, second % 60);    //&#x683C;&#x5F0F;&#x5316;&#x8F93;&#x51FA;

                nextTime = Time.time + 1;   //&#x53D8;&#x6210;&#x5F53;&#x524D;&#x65F6;&#x95F4;&#x7684;&#x540E;&#x4E00;&#x79D2;&#xFF08;&#x4E3A;&#x5F53;&#x524D;&#x65F6;&#x95F4;&#x52A0;1&#x79D2;&#xFF0C;&#x5224;&#x65AD;&#x5F53;&#x524D;&#x65F6;&#x95F4;&#x540E;&#x4E00;&#x79D2;&#x518D;&#x6267;&#x884C;&#xFF09;
            }
        }
    }</=>

5、将Timer1方法放入Update内即可

private void Update()
    {
        Timer1();
    }

第二种方法:

1、定义方法二用到的记录累加时间变量

private float totalTime;

2、在Start方法内获取文本组件(需要引入UI命名空间:using UnityEngine.UI; )

private void Start()
    {
        //&#x67E5;&#x627E;&#x7EC4;&#x4EF6;&#x7684; &#x65B9;&#x6CD5;&#x4E00;    &#xFF08;&#x9002;&#x5408;&#x67E5;&#x627E;&#x4E00;&#x4E2A;&#xFF0C;&#x591A;&#x4E2A;&#x7684;&#x8BDD;&#x9700;&#x8981;&#x5BF9;&#x53D8;&#x91CF;&#x904D;&#x5386;&#xFF09;
        txtTimer = GetComponent<text>();
        //&#x67E5;&#x627E;&#x7EC4;&#x4EF6;&#x7684; &#x65B9;&#x6CD5;&#x4E8C;    &#xFF08;&#x6309;&#x7167;&#x6307;&#x5B9A;&#x540D;&#x79F0;&#x67E5;&#x627E;&#xFF09;
        txtTimer = GameObject.Find("TextTimer").GetComponent<text>();

    }</text></text>

3、定义Timer2方法

private void Timer2()    //&#x6BCF;&#x7D2F;&#x52A0;&#x5230;1&#x79D2;&#xFF0C;&#x6267;&#x884C;&#x4E00;&#x6B21;&#xFF0C;&#x7136;&#x540E;&#x6E05;&#x7A7A;&#x7D2F;&#x52A0;&#x65F6;&#x95F4;&#xFF0C;&#x4ECE;0&#x5F00;&#x59CB;&#x91CD;&#x65B0;&#x7D2F;&#x52A0;&#xFF08;&#x8BEF;&#x5DEE;&#x5C0F;&#xFF09;
    {
        //Debug.Log("Timer2 &#x88AB;&#x8C03;&#x7528;");
        //&#x53D6;&#x6D88;&#x8C03;&#x7528;(&#x5012;&#x8BA1;&#x65F6;&#x4E3A;0&#x65F6;)
        if (second == 0)
        {
            CancelInvoke("Timer2");
        }
        if (second > 0)
        {
            //Debug.Log("&#x8FDB;&#x5165;");
            //&#x7D2F;&#x52A0;&#x6BCF;&#x5E27;&#x95F4;&#x9694;
            totalTime += Time.deltaTime;
            if (totalTime >= 1)
            {
                if (second <= 10) { txttimer.color="Color.red;" } debug.log("开始计时"); second--; txttimer.text="string.Format("{0:d2}:{1:d2}"," second 60, % 60); debug.log("计时结束"); totaltime="0;" 清空累加的时间 }< code></=>

4、放入Update方法执行

private void Update()
    {
        Timer2();
    }

第三种方法:

使用InvokeRepeating()或I nvoke()调用1或2方法

1、在Start方法内调用Timer1或Timer2方法

private void Start()
    {
        //&#x67E5;&#x627E;&#x7EC4;&#x4EF6;&#x7684; &#x65B9;&#x6CD5;&#x4E00;
        txtTimer = GetComponent<text>();
        //&#x67E5;&#x627E;&#x7EC4;&#x4EF6;&#x7684; &#x65B9;&#x6CD5;&#x4E8C;
        txtTimer = GameObject.Find("TextTimer").GetComponent<text>();

        //&#x91CD;&#x590D;&#x8C03;&#x7528;&#xFF08;&#x88AB;&#x6267;&#x884C;&#x7684;&#x65B9;&#x6CD5;&#x540D;&#x79F0;&#xFF0C;&#x7B2C;&#x4E00;&#x6B21;&#x6267;&#x884C;&#x65F6;&#x95F4;&#xFF0C;&#x6BCF;&#x6B21;&#x6267;&#x884C;&#x95F4;&#x9694;[&#x5F00;&#x59CB;&#x8FD0;&#x884C;&#x7A0B;&#x5E8F;&#x540E;&#x7B2C;&#x51E0;&#x79D2;&#x5F00;&#x59CB;&#x6267;&#x884C;]&#xFF09;
        InvokeRepeating("Timer1", 1, 2);

        //&#x8C03;&#x7528;&#xFF08;&#x65B9;&#x6CD5;&#x540D;&#x79F0;&#xFF0C;&#x7B2C;&#x4E00;&#x6B21;&#x6267;&#x884C;&#x65F6;&#x95F4;[&#x5F00;&#x59CB;&#x8FD0;&#x884C;&#x7A0B;&#x5E8F;&#x540E;&#x7B2C;&#x51E0;&#x79D2;&#x5F00;&#x59CB;&#x6267;&#x884C;]&#xFF09;
        //Invoke("Timer2", 1);
    }</text></text>

三、包含三种方法的总源代码

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

/// <summary>
///&#x5012;&#x8BA1;&#x65F6;
/// </summary>
public class CountdownTimer : MonoBehaviour
{
    private Text txtTimer;
    public int second = 120;

    private void Start()
    {
        //&#x67E5;&#x627E;&#x7EC4;&#x4EF6;&#x7684; &#x65B9;&#x6CD5;&#x4E00;
        txtTimer = GetComponent<text>();
        //&#x67E5;&#x627E;&#x7EC4;&#x4EF6;&#x7684; &#x65B9;&#x6CD5;&#x4E8C;
        //txtTimer = GameObject.Find("TextTimer").GetComponent<text>();

        //&#x91CD;&#x590D;&#x8C03;&#x7528;&#xFF08;&#x88AB;&#x6267;&#x884C;&#x7684;&#x65B9;&#x6CD5;&#x540D;&#x79F0;&#xFF0C;&#x7B2C;&#x4E00;&#x6B21;&#x6267;&#x884C;&#x65F6;&#x95F4;&#xFF0C;&#x6BCF;&#x6B21;&#x6267;&#x884C;&#x95F4;&#x9694;&#xFF09;
        //InvokeRepeating("Timer1", 1, 2);

        //&#x540D;&#x79F0;&#xFF0C;&#x51E0;&#x79D2;&#x4E4B;&#x540E;&#x5F00;&#x59CB;&#x6267;&#x884C;
        //Invoke("Timer2", 1);
    }

    private float nextTime = 1;//&#x4E0B;&#x6B21;&#x4FEE;&#x6539;&#x65F6;&#x95F4;

    private void Update()
    {
        //Timer1();
        Timer2();

    }

    ////////////////////////////////////&#x7B2C;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;///////////////////////////
    private void Timer1()
    {
        //Debug.Log("Timer1 &#x88AB;&#x8C03;&#x7528;");

        //&#x53D6;&#x6D88;&#x8C03;&#x7528;(&#x5012;&#x8BA1;&#x65F6;&#x4E3A;0&#x65F6;)[&#x7B2C;&#x4E09;&#x79CD;&#x65B9;&#x6CD5;&#x65F6;&#x4F7F;&#x7528;]
        if (second == 0)
        {
            CancelInvoke("Timer2");
        }

        if (second >=0)
        {
            if (second <= 10) { txttimer.color="Color.red;" } if (time.time>= nextTime)
            {
                second--;
                txtTimer.text = string.Format("{0:d2}:{1:d2}", second / 60, second % 60);

                nextTime = Time.time + 1;   //&#x53D8;&#x6210;&#x5F53;&#x524D;&#x65F6;&#x95F4;&#x7684;&#x540E;&#x4E00;&#x79D2;
            }
        }
    }

    ////////////////////////////////////&#x7B2C;&#x4E8C;&#x79CD;&#x65B9;&#x6CD5;///////////////////////////
    private float totalTime;
    private void Timer2()
    {
        //Debug.Log("Timer2 &#x88AB;&#x8C03;&#x7528;");

        //&#x53D6;&#x6D88;&#x8C03;&#x7528;(&#x5012;&#x8BA1;&#x65F6;&#x4E3A;0&#x65F6;)[&#x7B2C;&#x4E09;&#x79CD;&#x65B9;&#x6CD5;&#x65F6;&#x4F7F;&#x7528;]
        if (second == 0)
        {
            CancelInvoke("Timer2");
        }

        if (second > 0)
        {
            //Debug.Log("&#x8FDB;&#x5165;");
            //&#x7D2F;&#x52A0;&#x6BCF;&#x5E27;&#x95F4;&#x9694;
            totalTime += Time.deltaTime;
            if (totalTime >= 1)
            {
                if (second <= 10) { txttimer.color="Color.red;" } debug.log("开始计时"); second--; txttimer.text="string.Format("{0:d2}:{1:d2}"," second 60, % 60); debug.log("计时结束"); totaltime="0;" }< code></=></=></text></text>

PS:

将定义好的文本从层级菜单拖到项目菜单可以生成预制件,再从项目菜单拖回层级菜单就可以快速制作多个倒计时器

【Unity】UI面板:倒计时器

以上

Original: https://www.cnblogs.com/HanaKoo/p/16201617.html
Author: HanaKoo
Title: 【Unity】UI面板:倒计时器

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/568340/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球