浅谈Unity之 秒表的实现 / 倒计时的实现

本文涉及的产品
交互式建模 PAI-DSW,5000CU*H 3个月
简介: 秒表的实现 / 倒计时的实现

前言:每日记录自己学习unity的心得和体会,小弟才疏学浅,如有错误的地方,欢迎大佬们指正,感谢~



1.计时器的实现   第一种方法

/// <summary>

   /// 秒表的实现方法      创建一个Text  挂在上面即可

   /// </summary>

   private float timeSpend = 0;

   private int hour;   //小时

   private int minute; //分钟

   private int second; //秒

   private int millisecond; //毫秒

   private Text recealText;  //显示的text

   private void Start()

   {

       recealText = this.transform.GetComponent<Text>();

   }

   // Update is called once per frame

   void Update()

   {

       timeSpend += Time.deltaTime;

       hour = (int)timeSpend / 3600;

       minute = ((int)timeSpend - hour * 3600) / 60;

       second = (int)timeSpend - hour * 3600 - minute * 60;

       millisecond = (int)((timeSpend - (int)timeSpend) * 1000);

       recealText.text = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D3}", hour, minute, second, millisecond);

   }

第二种方法实现  

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

public class Timer_Clock : MonoBehaviour

{

   /// <summary>

   /// 秒表的实现方法      创建一个Text  挂在上面即可

   /// </summary>

   private float timeSpend = 0;

   private int hour;   //小时

   private int minute; //分钟

   private int second; //秒

   private int millisecond; //毫秒

   public Text recealText;  //显示的text

   public bool mator = false;

   private void Start()

   {

       recealText = this.transform.GetComponent<Text>();

       StartCoroutine(timerr ());

   }

   IEnumerator timerr()

   {

       while (!mator)

       {

           timeSpend += Time.deltaTime;

           hour = (int)timeSpend / 3600;

           minute = ((int)timeSpend - hour * 3600) / 60;

           second = (int)timeSpend - hour * 3600 - minute * 60;

           millisecond = (int)((timeSpend - (int)timeSpend) * 1000);

           recealText.text = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D3}", hour, minute, second, millisecond);

           yield return new WaitForFixedUpdate();

       }          

   }

   public void OnDestyoy_OBJ()

   {

       Destroy(this.gameObject);

   }

}


2.倒计时的实现  

创建一个Text 脚本挂在上面即可

using System;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

/// <summary>

/// 倒计时的实现

/// </summary>

public class CountDownTime : MonoBehaviour

{

   private Text text;

   int Shi;

   int Fen;

   int miao;

   private bool aa;

   public float time;

   // Start is called before the first frame update

   void Start()

   {

       text = this.GetComponent<Text>();

       StartCoroutine("enumerator");

       //Endkisijd();

   }

 

   public void Endkisijd()

   {            

       aa = true;      

   }

   #region   第一种方法

   IEnumerator enumerator()

   {      

       while (time >= 0)

       {

           Shi = (int)time / 3600;

           Fen = (int)time % 3600 / 60;

           miao = (int)time % 60;

           text.text = string.Format ("{0}:{1}:{2}",Shi,Fen ,miao );

           yield return new WaitForSeconds(1);

           time--;

       }

   }

   #endregion

   // Update is called once per frame

   void Update()

   {

       #region 第二种方法

       /*          

       if (aa)

       {

           Shi = (int)time / 3600;

           Fen = (int)time % 3600 / 60;

           miao = (int)time % 60;

           text.text = string.Format("{0}:{1}:{2}", Shi, Fen, miao);

       }

       if (time > 0)

       {

           time -= Time.deltaTime;

       }

       else

       {

           text.text = string.Format("{0}:{1}:{2}", 00, 00, 00);

           aa = false;

       }      

       */

       #endregion

   }

}


相关文章
|
3天前
|
C#
C# 如何使用倒计时
C# 如何使用倒计时
12 0
|
17天前
简单倒计时
简单倒计时
14 0
|
4月前
|
开发者 智能硬件
倒计时器
倒计时器
71 1
倒计时功能制作
今天制作一个商城项目距离抢购还剩多长时间的一个小功能 首先要知道这个倒计时是不断的变化的,所以需要用到定时器来自动变化(setInterval),有三个标签存放时分秒,再利用innerHTML将计算出来的时间放入三个标签内,第一次执行也是间隔毫秒数,因此刷新页面会有空白,我们最好采取封装函数的方式,这样可以先调用一次这个函数,防止刚开始刷新页面有空白问题。
|
iOS开发
iOS倒计时按钮闪烁问题解决
iOS倒计时按钮闪烁问题解决
133 0
实现计时器和倒计时工具(Unity3D)
今天分享一下如何基于Unity3D做计时器工具,为了方便演示,使用了UGUI的Text,代码简单具有拓展性,然后有什么错误或者意见也欢迎大家给我提出来。微信二维码已经显示在博客主页,有想要沟通学习的,项目外包的都可以加一下。 分享一下我另一篇关于时间计时的文章: 【Unity3D】获取到游戏时间,并显示出来