using UnityEngine;
using System.Collections;
using System;
public delegate void DeleFun_Int(int t);
public class UNTimeScript : MonoBehaviour
{
private DeleFun_Int m_Call;
private int m_Time;
private bool m_isUpPauseTime;
bool isBegin = false;
float buffer = 0f;
int step = 1;
long TickTime;
bool isPause=false;
bool isFocus=false;
void OnEnable()
{
isPause=false;
isFocus=false;
}
void OnApplicationPause()
{
if(!isPause)
{
TickTime = Environment.TickCount;
}
else
{
isFocus=true;
}
isPause=true;
}
void OnApplicationFocus()
{
//Debug.Log("OnApplicationFocus " + isPause + " " + isFocus);
if(isFocus)
{
// “启动”手机时,事件
isPause=false;
isFocus=false;
TickTime = Environment.TickCount - TickTime;
if(isBegin && m_isUpPauseTime)
{
buffer += TickTime/1000f;
}
}
if(isPause)
{
isFocus=true;
}
}
void Awake()
{
}
void Update ()
{
if(isBegin)
{
buffer += Time.deltaTime;
if(buffer >= step)
{
do
{
buffer -= step;
m_Time -= step;
}while(buffer >= step);
if(m_Time <= 0)
{
m_Time = 0;
isBegin = false;
}
TimeCall();
}
}
}
public void InitData(int t, DeleFun_Int call, bool isUpPauseTime)
{
m_Time = t;
m_Call = call;
m_isUpPauseTime = isUpPauseTime;
buffer = 0f;
isBegin = false;
if(m_Time <= 0)
{
Debug.Log("初始时间不能为负数 = " + m_Time);
return;
}
TimeCall ();
}
public void Play()
{
isBegin = true;
}
public void Pause()
{
isBegin = false;
}
void TimeCall()
{
m_Call (m_Time);
}
}
unity倒计时
最新推荐文章于 2024-03-09 09:56:50 发布