码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3D 游戏计时功能实现

时间:2015-04-10 23:43:29      阅读:433      评论:0      收藏:0      [点我收藏+]

标签:

最近工作实在是太忙了,没办法认真写博客,但是还是要好好记录下日常的学习。

 

各类游戏中都大量运用到计时功能,不管是直接显示的在前端UI,还是后台运行。

 

Unity中提供了Time类可以方便的进行时间上的获取,下面的例子中,我对其进行的简单的封装,能够方便的运用到各类型的游戏中。

 

技术分享
 1 public class ClockManagement : MonoBehaviour {
 2 
 3     private float screenWidth;
 4     private float screenHeight;
 5     private float timeScale;
 6     public GUISkin clockGuiSkin;
 7     public string NowTime;
 8 
 9     void Start()
10     {
11         screenWidth = Screen.width;
12         screenHeight = Screen.height;
13         NowTime = "0";
14         timeScale = 1;
15     }
16 
17     void OnGUI()
18     {
19         GUI.skin = clockGuiSkin;
20         NowTime = TimeFormatBase(Mathf.FloorToInt(Time.time));
21         GUI.Label(new Rect(screenWidth*0.9f, screenHeight*0.05f, 120f, 30f), NowTime);
22 
23         if(GUI.Button(new Rect(10,10,100,30),"暂停"))
24         {
25             Debug.Log("Stop");
26             Stop();
27         }
28         if (GUI.Button(new Rect(10, 60, 100, 30), "加速"))
29         {
30             timeScale = Mathf.Clamp(timeScale*2,0,100);
31             
32             SetTimeScale(timeScale);
33             Debug.Log("Quick:" + timeScale);
34         }
35     }
36 
37     string TimeChineseFormat(float nTotalTime)
38     {
39         string time = string.Empty;
40         float day = Mathf.Floor(nTotalTime / 86400);
41         float hour = Mathf.Floor(nTotalTime % 86400 / 3600);
42         float min = Mathf.Floor(nTotalTime % 3600 / 60);
43         float sec = nTotalTime % 60;
44         if (day > 0)
45             time = string.Concat(day, "");
46         if(hour>0)
47             time = string.Concat(time,hour, "");
48         if(min>0)
49             time = string.Concat(time,min, "");
50         if(sec>0)
51             time = string.Concat(time,sec, "");
52         return time;
53     }
54 
55     string TimeFormatBase(float nTotalTime)
56     {
57         string time = string.Empty;
58         float hour = Mathf.Floor(nTotalTime / 3600);
59         float min = Mathf.Floor(nTotalTime % 3600 / 60);
60         float sec = nTotalTime % 60;
61         if(hour >= 0 && hour <10)
62             time = string.Concat("0",hour,":");
63         else
64             time = string.Concat(hour, ":");
65 
66         if (min >= 0 && min < 10)
67             time = string.Concat(time, "0", min, ":");
68         else
69             time = string.Concat(time, min, ":");
70 
71         if (sec >= 0 && sec < 10)
72             time = string.Concat(time, "0", sec);
73         else
74             time = string.Concat(time, sec);
75         return time;
76     }
77 
78     public void SetTimeScale(float scale)
79     {
80         Time.timeScale = scale;
81     }
82 
83     public void Stop()
84     {
85         Time.timeScale = 0;
86     }
87 }
Clock

 

Unity3D 游戏计时功能实现

标签:

原文地址:http://www.cnblogs.com/nightcat/p/4415868.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!