[Unity] Unity3d时间、计时管理器简介

查看:1260 |回复:16 | 2015-11-2 16:13:06

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

x
本帖最后由 大西几 于 2021-2-24 15:30 编辑

在之前文章我们提到过定时器的做法《unity教程-自定义Clock定时器类 》,在平时的游戏开发过程中,我们或多或少的会用到时间类,比如技能的冷却时间,角色的生命回复等等都会用到,而处理方式也有很多种,我们可以在每个技能上面挂载一个时间类,也可以提供一个时间管理器,来统一管理技能的冷却,个人推荐第二种做法,因为这样会更加精准!
时间管理器的类主要包括 TimerManager.cs,代码如下:
using Unity Engine;
using System;
using System.Collections.Generic;
///
/// 移动管理
///
public class TimerManager
{
public static float time;
public static Dictionary timerList = new Dictionary();
public static void Run()
{
// 设置时间值
TimerManager.time = Time.time;
TimerItem[] objectList = new TimerItem[timerList.Values.Count];
timerList.Values.CopyTo(objectList, 0);
// 锁定
foreach(TimerItem timerItem in objectList)
{
if(timerItem != null) timerItem.Run(TimerManager.time);
}
}
public static void Register(object objectItem, float delayTime, Action callback)
{
if(!timerList.ContainsKey(objectItem))
{
TimerItem timerItem = new TimerItem(TimerManager.time, delayTime, callback);
timerList.Add(objectItem, timerItem);
}
}
public static void UnRegister(object objectItem)
{
if(timerList.ContainsKey(objectItem))
{
timerList.Remove(objectItem);
}
}
}
TimerItem.cs,代码如下:
using UnityEngine;
using System;
public class TimerItem
{
///
/// 当前时间
///
public float currentTime;
///
/// 延迟时间
///
public float delayTime;
///
/// 回调函数
///
public Action callback;
public TimerItem(float time, float delayTime, Action callback)
{
this.currentTime = time;
this.delayTime = delayTime;
this.callback = callback;
}
public void Run(float time)
{
// 计算差值
float offsetTime = time - this.currentTime;
// 如果差值大等于延迟时间
if(offsetTime >= this.delayTime)
{
float count = offsetTime / this.delayTime - 1;
float mod = offsetTime % this.delayTime;
for(int index = 0; index < count; index ++)
{
this.callback();
}
this.currentTime = time - mod;
}
}
}
测试用例代码如下:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Demo : MonoBehaviour
{
public GameObject roleObject;
void Awake()
{
TimerManager.Register(this, 0.3f, ()=>
{
Debug.Log("0.3 -> " + System.DateTime.Now.ToString("hh:mm:ss.fff"));
});
TimerManager.Register(this.gameObject, 1f, ()=>
{
Debug.Log("1 -> " + System.DateTime.Now.ToString("hh:mm:ss.fff"));
});
}
void Update()
{
TimerManager.Run ();
}
}
好了,本篇unity3d教程到此结束,下篇我们再会! image.png


评分

参与人数 1活跃度 +1 展开 理由
幺九 + 1 【封面】或展示不规范,请尽快补充,否则会被移出资源区!

查看全部评分

2015-11-2 16:13:06  
 赞 赞 2

使用道具 登录

16个回答,把该问题分享到群,邀请大神一起回答。
2#
好资源~点赞
回复 收起回复
2015-11-3 00:56:14   回复
 赞 赞 2

使用道具 登录

5#
为了元素币,拼了!
回复 收起回复
2015-11-3 11:49:21   回复
 赞 赞 2

使用道具 登录

6#
谢谢楼主的分享!这个绝对要顶!!!
回复 收起回复
2015-11-4 10:42:09   回复
 赞 赞 2

使用道具 登录

7#
资源甚好,发帖艰辛,且阅且珍惜!
回复 收起回复
2015-11-4 11:33:45   回复
 赞 赞 2

使用道具 登录

8#
天下武功出少林,世界资源入元素!
回复 收起回复
2015-11-10 18:35:56   回复
 赞 赞 2

使用道具 登录

9#
元素帖子强,满满正能量!
回复 收起回复
2015-11-10 18:36:53   回复
 赞 赞 2

使用道具 登录

10#
学习了···
回复 收起回复
2015-11-20 22:07:10   回复
 赞 赞 2

使用道具 登录

11#
学习了。。。。。。。。。。
回复 收起回复
2015-11-21 13:29:16   回复
 赞 赞 2

使用道具 登录

12#
谢谢分享~!
回复 收起回复
2016-2-28 08:49:43   回复
 赞 赞 2

使用道具 登录

13#
tgujygh
回复 收起回复
2016-4-1 21:26:57   回复
 赞 赞 2

使用道具 登录

14#
回复 收起回复
2023-2-28 11:12:27   回复
 赞 赞 2

使用道具 登录

15#
千点万点,不如微元素指点。
回复 收起回复
2023-4-10 09:27:07   回复
 赞 赞 1

使用道具 登录

16#
回复 收起回复
2023-4-10 09:27:11   回复
 赞 赞 1

使用道具 登录

17#
回复 收起回复
2023-4-10 09:27:15   回复
 赞 赞 1

使用道具 登录

18#
回复 收起回复
2023-4-10 09:27:18   回复
 赞 赞 1

使用道具 登录

19#
回复 收起回复
2023-4-10 15:14:07   回复
 赞 赞 1

使用道具 登录

CG 游戏行业专业问题

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

本版积分规则

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