您的位置:首页 > 移动开发 > Unity3D

Unity3D C# 简单的倒计时

2016-06-30 17:23 686 查看
using System;  

using UnityEngine;  

using System.Collections;  

using UnityEngine.UI;  

  

  

public class TimeCountdown : MonoBehaviour  

{  

  

  

    //之前的一个时间点  

    public long startTime = 1435870304;  

    //限定时间秒  

    private long fixedTime = 200000;  

    private long nowTime;  

  

  

    bool isOver = false;  

  

  

  

  

    // Use this for initialization  

    void Start()  

    {  

        nowTime = (System.DateTime.Now.Ticks - System.DateTime.Parse("1970-01-01").Ticks) / 10000000;  

        Debug.Log(nowTime.ToString());  

        InvokeRepeating("CountDown", 0, 1);  

    }  

  

  

    // Update is called once per frame  

    void Update()  

    {  

        if (!isOver && nowTime - startTime >= fixedTime)  

        {  

            Debug.Log("倒计时结束");  

            isOver = true;  

        }  

    }  

  

  

    void CountDown()  

    {  

        fixedTime -= 1;  

        gameObject.GetComponent<Text>().text = (fixedTime / (60 * 60 * 24)).ToString() + "天"  

             + ((fixedTime / 60 - fixedTime / (60 * 60 * 24) * 24 * 60) / 60).ToString() + "小时"  

             + ((fixedTime / 60) % 60).ToString() + "分"  

             + (fixedTime % 60).ToString() + "秒";  

    }  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: