unity3D更多资源教程免费下载,群193521697 邀请码:10026.(有问题找管理员)
usingUnityEngine;
using System.Collections;
///<summary>
/// C# timer 改编自Jeff 'PsychicParrot' Murray
的js的timer
/// </summary>
public class Timer : MonoBehaviour {
privatefloat timeElapsed = 0.0f;
private float currentTime = 0.0f;
private float lastTime = 0.0f;
private float cdTime = 0.0f;
private float timeScaleFactor = 1.0f;
private bool isTimerRunning;
private bool doneCallback;
private float parseTime;
private GameObject callback;
private float aHour;
private float aMinute;
private float aSecond;
private float aMillis;
private string seconds;
private string minutes;
private string hour;
private string mills;
private string timeString;
voidUpdate ()
{
timeElapsed=Time.time-lastTime;
if(isTimerRunning){
currentTime+=timeElapsed*timeScaleFactor;
}
lastTime=Time.time;
}
publicvoid StartTimer()
{
isTimerRunning=true;
doneCallback=false;
lastTime=Time.time;
}
publicvoid StopTimer()
{
isTimerRunning=false;
}
publicvoid ResetTimer()
{
doneCallback=false;
timeElapsed=0.0f;
lastTime=0.0f;
currentTime=0.0f;
lastTime=Time.time;
}
publicvoid SetCountdownTime(float aTime)
{
cdTime=aTime;
}
publicvoid SetCallback(GameObject aRef)
{
callback=aRef;
doneCallback=false;
}
publicfloat GetTime()
{
return currentTime;
}
publicstring GetFormattedTime(int returnType)
{
if(cdTime>0){
// if a countdown time has been set, we parse the reverse time value
parseTime=cdTime-currentTime;
if(parseTime<0){
if(!doneCallback){
// SOUND THE ALARM!!!!! WE HIT 0!
callback.SendMessage("alarmDone");
// set our doneCallback flag so as not to repeat call alarmDone()
doneCallback=true;
}
parseTime=0;
}
} else {
// if no countdown time has been set, we just parse the regular time
parseTime=currentTime;
}
// grabhours
aHour = parseTime/3600;
aHour=aHour%24;
// grabminutes
aMinute=parseTime/60;
aMinute=aMinute%60;
// grabseconds
aSecond=parseTime%60;
// grabmilliseconds
aMillis=(parseTime*100)%100;
//format string into mm:ss:mm
seconds=Mathf.Round(aSecond).ToString();
if(seconds.Length<2)
seconds="0"+seconds;
minutes=Mathf.Round(aMinute).ToString();
if(minutes.Length<2)
minutes="0"+minutes;
hour=Mathf.Round(aHour).ToString();
if(hour.Length<2)
hour="0"+hour;
mills=Mathf.Round(aMillis).ToString();
if(mills.Length<2)
mills="0"+mills;
switch(returnType){
case 1:
timeString=minutes+":"+seconds+":"+mills;
break;
case 2:
timeString=minutes+":"+seconds;
break;
default:
timeString=hour+":"+minutes+":"+seconds+":"+mills;
break;
}
returntimeString;
}
publicfloat GetHours()
{
if(cdTime>0){
// if a countdown time has been set, we parse the reverse time value
parseTime=cdTime-currentTime;
if(parseTime<0){
parseTime=0;
}
} else {
// if no countdown time has been set, we just parse the regular time
parseTime=currentTime;
}
// grabhours
aHour = parseTime/3600;
returnaHour;
}
publicfloat GetMinutes()
{
if(cdTime>0){
// if a countdown time has been set, we parse the reverse time value
parseTime=cdTime-currentTime;
if(parseTime<0){
parseTime=0;
}
} else {
// if no countdown time has been set, we just parse the regular time
parseTime=currentTime;
}
// grabminutes
aMinute=parseTime/60;
aMinute=aMinute%60;
returnaMinute;
}
publicfloat GetSeconds()
{
if(cdTime>0){
// if a countdown time has been set, we parse the reverse time value
parseTime=cdTime-currentTime;
if(parseTime<0){
parseTime=0;
}
} else {
// if no countdown time has been set, we just parse the regular time
parseTime=currentTime;
}
// grabseconds
aSecond=parseTime%60;
returnaSecond;
}
publicint getClockTimeHour()
{
int aTime=System.DateTime.Now.Hour;
return aTime;
}
publicint getClockTimeMinute()
{
int aTime=System.DateTime.Now.Minute;
return aTime;
}
publicint getClockTimeSeconds()
{
int aTime=System.DateTime.Now.Second;
return aTime;
}
publicstring GetClockFormattedTime(int returnType)
{
int aHour =System.DateTime.Now.Hour;
int aMinute =System.DateTime.Now.Minute;
int aSecond =System.DateTime.Now.Second;
int aMillis =System.DateTime.Now.Millisecond;
//format string into mm:ss:mm
seconds=Mathf.Round(aSecond).ToString();
if(seconds.Length<2)
seconds="0"+seconds;
minutes=Mathf.Round(aMinute).ToString();
if(minutes.Length<2)
minutes="0"+minutes;
hour=Mathf.Round(aHour).ToString();
if(hour.Length<2)
hour="0"+hour;
mills=Mathf.Round(aMillis).ToString();
if(mills.Length<2)
mills="0"+mills;
if(mills.Length>2)
mills=mills.Substring(0,2);
switch(returnType){
case 1:
timeString=minutes+":"+seconds+":"+mills;
break;
case 2:
timeString=minutes+":"+seconds;
break;
default:
timeString=hour+":"+minutes+":"+seconds+":"+mills;
break;
}
returntimeString;
}
}