不多讲,直接上代码
void Start()
{
CreatingTimerHandles();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyUp(KeyCode.Space))
PauseRace();
if (Input.GetKeyUp(KeyCode.A)) CancelingTimerEvents();
}
//调度事件
void Scheduling()
{
//1秒后调用SomeMethod()
vp_Timer.In(1.0f, SomeMethod);
}
void SomeMethod()
{
Debug.Log("调用");
}
//重复调用
void Iterations()
{
//1秒后调用,间隔1秒调用5次
vp_Timer.In(1.0f, SomeMethod, 5);
//0.1秒后调用,间隔1秒调用3次
vp_Timer.In(0.1f,SomeMethod,3, 1);
//0.1秒后调用,间隔1秒调用无限次
vp_Timer.In(0.1f, SomeMethod, 0, 1);
}
//带参数调用
void Arguments()
{
vp_Timer.In(1.0f, MethodWithSingleArgument, "Hello World!");
}
void MethodWithSingleArgument(object o)
{
string s = (string)o;
Debug.Log(s);
}
//带多个参数调用
void MultipleArguments()
{
vp_Timer.In(1.0f, MethodWithMultipleArguments,new object[] { "December", 31, 2012 });
}
void MethodWithMultipleArguments(object o)
{
object[] arg = (object[])o;
Debug.Log("Month: " + (string)arg[0]
+ ", Day:" + (int)arg[1]
+ ", Year:" + (int)arg[2]);
}
//委托
void Delegates()
{
//方式一
vp_Timer.In(1.0f, delegate () { Debug.Log("Hello World!"); });
//方式二
vp_Timer.In(0.01f, new vp_Timer.Callback(() =>
{
Debug.Log("Hello World!");
}), 0, Timer);
}
//具有参数的委托
void DelegatesWithArguments()
{
float incomingDamage = 242.0f;
vp_Timer.In(1.0f, delegate (object o)
{
Debug.Log((string)o);
}, incomingDamage);
}
//具有多个参数的委托
void DelegatesWithMultipleArguments()
{
string month = "December";
int day = 31;
int year = 2012;
vp_Timer.In(1.0f, delegate (object o)
{
object[] arg = (object[])o;
Debug.Log("Month: " + (string)arg[0]
+ ", Day:" + (int)arg[1]
+ ", Year:" + (int)arg[2]);
}, new object[] { month, day, year });
}
//取消计时器事件
void CancelingTimerEvents()
{
// vp_Timer.CancelAll();
vp_Timer.CancelAll("SomeMethod");
}
//开始计时器事件
private vp_Timer.Handle m_RaceTimer = new vp_Timer.Handle();
void BeginRace()
{
vp_Timer.Start(Timer1);
}
//暂停计时器事件
private vp_Timer.Handle m_RaceTimer1 = new vp_Timer.Handle();
void PauseRace()
{
if (Timer1.Paused)
{
Timer1.Paused = false;
}
else
{
Timer1.Paused = true;
}
}
//创建计时器句柄
private vp_Timer.Handle Timer1 = new vp_Timer.Handle();
private vp_Timer.Handle Timer = new vp_Timer.Handle();
void CreatingTimerHandles()
{
//---示例1:在20秒内执行“某种方法”, //,但如果用户按下空间,则取消
vp_Timer.In(0.1f, SomeMethod,0, Timer1);
//---示例2:执行“某种方法”的5次迭代, //,但在3秒后取消 私人vp_Timer。处理计时
//vp_Timer.In(0.1f, SomeMethod, 5, 1, Timer);
//vp_Timer.In(3, delegate () { Timer.Cancel(); });
}
//计时器判断
private vp_Timer.Handle Timer2 = new vp_Timer.Handle();
void blockingTimerEvents()
{
vp_Timer.In(0.4f, SomeMethod, Timer2);
if (!Timer2.Active) // /防止重新启动“计时器”
vp_Timer.In(1.5f, SomeMethod, Timer2);
}
}