Github - Unity3d-Timers

本文介绍了一个用于Unity3D的灵活定时器系统,该系统允许轻松创建和扩展复杂的定时行为。通过示例代码展示了如何使用这个系统启动重复任务及倒计时,并详细解释了CountdownBehavior类的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://github.com/pointcache/Unity3d-Timers

Unity3d-Timers

Timer class with various behaviors

About: Have a system allowing to easily create and extend complex timer behaviors.

Usage:

//Start simple repeater 
Timer.Repeater(5f, () => Debug.Log("Repeater test"));
//Start countdown and call Draw in 2.5 seconds Timer.Countdown(2.5f, Draw);

Concept: A manager class pools and constructs new timers, timer store up to 4 handlers, and arbitrarely raise events in concrete implementation.

private class CountdownBehavior : TimerBehaviorBase, ITimerBehavior
{
    float exitTime; public void Initialize() { exitTime = f1; } public void Update(float deltaTime) { TimePassed += deltaTime; TotalTimeActive += deltaTime; if (TimePassed > exitTime) { Completed = true; c1(); } } } 

Countdown inherits TimerBehaviorBase and implements ITimerBehavior. On top of that only concrete behavior in implemented that uses data and callbacks provided in constructor:

public static Timer Countdown(float exitTime, Action callback) { Timer timer = TimerManager.getTimer(); timer.SetBehavior<CountdownBehavior>(); timer.behaviorBase .SetFloats(exitTime, 0, 0, 0) .SetCallbacks(callback, null, null, null); timer.behavior.Initialize(); return timer; } 

as you can see you can set 4 floats, and 4 callbacks and use them in your implementation of behavior as you like. Examples 

For concrete real example look at Ability class, it implements typical game ability, with cooldown.

 

转载于:https://www.cnblogs.com/mimime/p/6995427.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值