[Unity] unity教程-自定义Clock定时器类

查看:621 |回复:14 | 2015-11-3 17:02:58

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 醉酒風 于 2019-4-28 22:48 编辑

这篇Unity 教程我们来学习下自定义Clock定时器类!
unity中有协程可以提供延迟的功能等。 但是很多时候我们并不想使用,那就自己在Update中控制时间呗。
于是我封装了这个类。
若要使用这个时钟,首先将其实例化,调用Reset函数设置正确的时间值,调用Update每一帧更新。
任何想要被事件通知的类需要实现 IClockListener 接口,
和使用AddListener方法订阅事件。可以用RemoveListener移除侦听器(很强大吧!)
时钟能够使用Pause方法独立于 Time.timeScale 被暂停 (和使用 Unpause恢复继续)
using System.Collections.Generic;
namespace Gamelogic
{
public class Clock
{
private float time;
private int timeInSeconds;
private readonly List listeners; // 监听列表
#region
public bool IsPaused
{
get; private set;
}
public bool IsDone
{
get; private set;
}
public float Time
{
get
{
return time;
}
}
public int TimeInSeconds
{
get
{
return timeInSeconds;
}
}
#endregion
// 构造函数
public Clock()
{
listeners = new List();
IsPaused = true;
Reset(0);
}
public void AddClockListener(IClockListener listener)
{
listeners.Add(listener);
}
public void RemoveClockListener(IClockListener listener)
{
listeners.Remove(listener);
}
public void Reset(float startTime)
{
time = startTime;
IsDone = false;
CheckIfTimeInSecondsChanged();
}
public void Unpause()
{
IsPaused = false;
}
public void Pause()
{
IsPaused = true;
}
// 时间每帧更新
public void Update()
{
if (IsPaused) return;
if (IsDone) return;
time -= UnityEngine.Time.deltaTime;
CheckIfTimeInSecondsChanged();
if (time <= 0)
{
time = 0;
IsDone = true;
for (int i = 0;i< listeners.Count;i++)
{
listeners.OnTimeOut();
}
}
}
// 判断是否发生秒的改变
private void CheckIfTimeInSecondsChanged()
{
var newTimeInSeonds = (int)time;
if (newTimeInSeonds == timeInSeconds) return;
timeInSeconds = newTimeInSeonds;
for (int i = 0;i< listeners.Count;i++)
{
listeners.OnSecondsChanged(timeInSeconds);
}
}
}
// 时钟监听者类型接口
public interface IClockListener
{
void OnSecondsChanged(int seconds);
void OnTimeOut();
}
}
然后我简单测试了一下,在unity4.6中。 如下倒计时:
Center.jpg 下面是客户端测试代码
using UnityEngine.UI;
namespace Gamelogic.Examples
{
public class ClockTest : IClockListener
{
public Text clockText;
public Text messageText;
private Clock clock; // 时钟对象
public void Start()
{
clock = new Clock();
clock.AddClockListener(this); // 对时钟监听
Reset();
}
public void Update()
{
clock.Update();
}
public void Pause()
{
clock.Pause();
}
public void Unpause()
{
clock.Unpause();
}
public void Reset()
{
clock.Reset(5);
clock.Unpause();
}
#region IClockListener methods // 实现接口方法
public void OnSecondsChanged(int seconds)
{
clockText.text = clock.TimeInSeconds.ToString();
}
public void OnTimeOut()
{
messageText.gameObject.SetActive(true);
}
#endregion
}
}

评分

参与人数 1元素币 +10 展开 理由
元素界王神 + 10

查看全部评分

2015-11-3 17:02:58  
 赞 赞 1

使用道具 登录

14个回答,把该问题分享到群,邀请大神一起回答。
2#
正是本尊想要的!
回复 收起回复
2015-11-3 17:36:02   回复
 赞 赞 1

使用道具 登录

3#
好资源~!点赞
回复 收起回复
2015-11-3 22:06:20   回复
 赞 赞 1

使用道具 登录

5#
想要成大触,天天上元素!
回复 收起回复
2015-11-5 22:04:01   回复
 赞 赞 1

使用道具 登录

6#
回复 收起回复
2018-11-16 09:18:24   回复
 赞 赞 1

使用道具 登录

7#
不错的资源,谢谢分享
回复 收起回复
2018-11-16 09:20:22   回复
 赞 赞 1

使用道具 登录

8#
元素大法呱呱叫
回复 收起回复
2018-11-16 10:11:14   回复
 赞 赞 1

使用道具 登录

9#
我和我的小伙伴们都惊呆了!
回复 收起回复
2018-11-16 10:16:14   回复
 赞 赞 2

使用道具 登录

10#
厉害了我的亲
回复 收起回复
2018-11-16 10:33:39   回复
 赞 赞 2

使用道具 登录

11#
感谢楼主分享!!!
回复 收起回复
2018-11-17 19:27:06   回复
 赞 赞 2

使用道具 登录

12#
路过看看 感谢分享
回复 收起回复
2018-11-17 20:59:55   回复
 赞 赞 1

使用道具 登录

13#
正是本尊想要的!正是本尊想要的!
回复 收起回复
2019-3-27 09:07:15   回复
 赞 赞 1

使用道具 登录

14#
很好很强,即使四年过去了,技术还是不过时的, c#的timer功能在unity中实现
回复 收起回复
2019-3-27 09:32:56   回复
 赞 赞 1

使用道具 登录

15#
感謝LZ,哈哈哈
回复 收起回复
2019-4-3 00:29:31   回复
 赞 赞 1

使用道具 登录

CG 游戏行业专业问题

Unity3D技术手机游戏引擎手游引擎
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表