在Unity中实现倒计时功能

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// 
/// 倒计时
/// 
public class DaoJiShi_CeShi : MonoBehaviour
{
    [SerializeField]
    private int tmp = 10;
    //public Transform[] tf;

    void Start()
    {
        GetComponent().text = "倒计时" + tmp;
        StartCoroutine(ChangeTime());

        //tf = GetComponentsInParent();
    }

    private IEnumerator ChangeTime()
    {
        while (tmp > 0)
        {
            yield return new WaitForSeconds(1);
            tmp--;
            GetComponent().text = "倒计时" + tmp;
        }
        DaoJiShiJieShu();
    }

    private void DaoJiShiJieShu()
    {
        //Debug.Log("倒计时结束");
        GetComponent().text = "倒计时结束";
        //关闭子物体的父物体
        GetComponentsInParent()[1].gameObject.SetActive(false);
    }
}

在Unity中实现倒计时功能_第1张图片在Unity中实现倒计时功能_第2张图片

你可能感兴趣的:(在Unity中实现倒计时功能)