03、三种简单的计时器

1、计时器在游戏中的使用次数很多,以下是三种简单的计时器写法

2、代码:

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 using UnityEngine.UI;
 5
 6 public class Timer : MonoBehaviour
 7 {
 8     private Text textTime;
 9     private int second = 20;
10
11     private void Awake()
12     {
13         textTime = this.GetComponent<Text>();
14     }
15     private void Update()
16     {
17         //Timer_01();
18        // Timer_02();
19     }
20
21     //第一种方式的计时器
22     private float nextTimer_1 = 1;   //下次修改时间
23     private float nextTime_1 = 1;    //一次间隔是多少
24     private void Timer_01()
25     {
26         if (nextTimer_1 <= Time.time)
27         {
28             //到了1秒
29             second--;
30             if (second >= 0)
31             {
32                 textTime.text = string.Format("{0:d2}:{1:d2}", (int)second / 60, (int)second % 60);
33                 nextTimer_1 += nextTime_1;  //将计时器当前的存储加上程序运行的时间
34             }
35         }
36     }
37
38     //第二种方式的计时器
39     private float nextTime_2_1 = 0;
40     private float nextTime_2_2 = 1;  //时间间隔
41     private void Timer_02()
42     {
43         nextTime_2_1 += Time.deltaTime;
44         if (nextTime_2_1 >= nextTime_2_2)
45         {
46             //到了1秒了
47             second--;
48             if (second >= 0)
49             {
50                 textTime.text = string.Format("{0:d2}:{1:d2}", second / 60, second % 60);
51                 nextTime_2_1 = 0;
52             }
53         }
54     }
55
56     //第三种是使用InvokeRepeating,来指定起始时间,重复调用方法的间隔
57     private void Start()
58     {
59         InvokeRepeating("Timer_03", 0, 1);
60     }
61     private void Timer_03()
62     {
63         second--;
64         if(second<=0)
65         {
66             CancelInvoke("Timer_03");   //如果时间为0了,则就要终止循环调用了
67         }
68         textTime.text = string.Format("{0:d2}:{1:d2}", second / 60, second % 60);
69     }
70 }

原文地址:https://www.cnblogs.com/zhh19981104/p/9574381.html

时间: 2024-08-26 15:18:39

03、三种简单的计时器的相关文章

文顶顶 iOS开发UI篇—iOS开发中三种简单的动画设置

iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所

iOS开发UI篇—iOS开发中三种简单的动画设置

iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所

三种简单的html网页自动跳转方法

三种简单的html网页自动跳转方法,可以让你在打开一个html网页时自动跳转到其它的页面. 方法/步骤 <html> <head> <title>正在跳转</title> <meta http-equiv="Content-Language" content="zh-CN"> <meta HTTP-EQUIV="Content-Type" CONTENT="text/ht

iOS开发中三种简单的动画设置

iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所有动画提交并生成动

算法:三种简单排序算法

排序算法比较常见的有:冒泡排序.简单选择排序.直接插入排序:希尔排序.堆排序.归并排序和快速排序算法等.今天先学习一下前面三种比较简单的算法.排序的相关概念: ①排序的稳定性:两个或多个元素相等,排序过后仍然是原来的顺序则为稳定排序. ②内部排序:排序过程都在内存中进行:外部排序:需要对外存进行访问的排序过程. ③内排序算法性能因素:1.时间性能,比较与移动:2.辅助空间:3.算法复杂性 实例:冒泡排序.简单选择排序与直接插入排序 #include "stdio.h" #define

面试中常常问的三种简单排序方法

/** * 三种简单的排序 * 本类中全部举例都是依照从小到大进行排序 * @author caohaicheng * @time 2014-07-24 */ public class SortDemo { //int[] score={7,10,35,21,78,2,1,9}; public static void main(String[] args) { SortDemo sd=new SortDemo(); System.out.println("********************

Verilog中的三种简单触发器

时序逻辑中的三种简单触发器,使用Verilog语言编写,用来熟悉语法最好不过了. D触发器 module D_LOCK(D,CLK,Q,NQ); //正边沿D触发 output Q; output NQ; input D; input CLK; //时序赋值 reg Q; assign NQ=~Q; //上升沿触发 always @(posedge CLK) begin Q<=D; end endmodule //a simple testbench module d_lock_tb(); re

【iOS开发-75】iOS数据存储的三种简单方式:plist、preference以及用NSCoding存储对象

实际开发中,存储数据主要是用SQLite.而在练习中,我们主要用如下三种存储方式. (1)利用plist存储简单地NSString.NSArray.NSDictionary等. (2)利用preference存储,和上面的类似,存储的是简单的数据,本质上还是一个plist文件. (3)利用NSCoding存储对象这些复杂的数据,本质上是一个data文件,需要被存储的类遵守NSCoding协议并实现init和encode方法. 代码如下: --在ViewController.m中 - (void)

三种简单写法教你Javascript对象封装

这里的内容没有太多深入的东西,更多的内容请移步麦子学院. Javascript在HTML中变得越来越强大, HTML5中的WebGL等.但是我们书写Javascript的时候往往很随意,使用对象的封装是极好的.这里介绍Javascipt三种创建对象的方法.  使用关键字new创建对象 Js代码   1. 2.   function Person(name, age) { 3.     this.name = name; 4.     this.age = age; 5.   } 6.   var