[Unity] Unity3D 计时/倒计时管理类

查看:1820 |回复:18 | 2015-7-17 15:07:51

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

x
实用小方法,原帖地址见末端

在平时的开发过程中,计时、倒计时经常在游戏中用到,想必大家都有自己的处理高招,为了以后用方便使用,
抽象出一个 TimerManager 类,功能很简单,
这儿采用协程的 WaitForSeconds 来处理计时、倒计时,希望对你有所帮助!


20150514161221_6686_new.jpg

  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;

  5. public class TimerManager
  6. {
  7.     private static Dictionary<string, TimerItem> dictList = new Dictionary<string, TimerItem>();

  8.     /// <summary>
  9.     /// 注册计时
  10.     /// </summary>
  11.     /// <param name="timerKey">Timer key.</param>
  12.     /// <param name="totalNum">Total number.</param>
  13.     /// <param name="delayTime">Delay time.</param>
  14.     /// <param name="callback">Callback.</param>
  15.     /// <param name="endCallback">End callback.</param>
  16.     public static void Register(string timerKey, int totalNum, float delayTime, Action<int> callback, Action endCallback)
  17.     {
  18.         TimerItem timerItem = null;
  19.         if(!dictList.ContainsKey(timerKey))
  20.         {
  21.             GameObject objectItem = new GameObject ();
  22.             objectItem.name = timerKey;

  23.             timerItem = objectItem.AddComponent<TimerItem> ();
  24.             dictList.Add(timerKey, timerItem);
  25.         }else
  26.         {
  27.             timerItem = dictList[timerKey];
  28.         }

  29.         if(timerItem != null)
  30.         {
  31.             timerItem.Run(totalNum, delayTime, callback, endCallback);
  32.         }
  33.     }

  34.     /// <summary>
  35.     /// 取消注册计时
  36.     /// </summary>
  37.     /// <param name="timerKey">Timer key.</param>
  38.     public static void UnRegister(string timerKey)
  39.     {
  40.         if(!dictList.ContainsKey(timerKey)) return;

  41.         TimerItem timerItem = dictList [timerKey];
  42.         if(timerItem != null)
  43.         {
  44.             timerItem.Stop ();
  45.             GameObject.Destroy(timerItem.gameObject);
  46.         }
  47.     }
  48. }

  49. class TimerItem : MonoBehaviour
  50. {
  51.     private int totalNum;
  52.     private float delayTime;
  53.     private Action<int> callback;
  54.     private Action endCallback;

  55.     private int currentIndex;

  56.     public void Run(int totalNum, float delayTime, Action<int> callback, Action endCallback)
  57.     {
  58.         this.Stop ();

  59.         this.currentIndex = 0;

  60.         this.totalNum = totalNum;
  61.         this.delayTime = delayTime;
  62.         this.callback = callback;
  63.         this.endCallback = endCallback;

  64.         this.StartCoroutine ("EnumeratorAction");
  65.     }

  66.     public void Stop()
  67.     {
  68.         this.StopCoroutine ("EnumeratorAction");
  69.     }

  70.     private IEnumerator EnumeratorAction()
  71.     {
  72.         yield return new WaitForSeconds (this.delayTime);

  73.         this.currentIndex ++;
  74.         if(this.callback != null) this.callback(this.currentIndex);

  75.         if(this.totalNum != -1)
  76.         {
  77.             if(this.currentIndex >= this.totalNum)
  78.             {
  79.                 if(this.endCallback != null) this.endCallback();
  80.                 yield break;
  81.             }
  82.         }
  83.         this.StartCoroutine("EnumeratorAction");
  84.     }
  85. }
点击此处复制文本
原帖地址:http://www.omuying.com/article/124.aspx

2015-7-17 15:07:51  
 赞 赞 1

使用道具 登录

18个回答,把该问题分享到群,邀请大神一起回答。
2#
你们大神真会玩!
回复 收起回复
2015-7-17 15:08:32   回复
 赞 赞 1

使用道具 登录

3#
带你赚币带你飞,元素里面有正妹!
回复 收起回复
2015-7-17 15:13:28   回复
 赞 赞 1

使用道具 登录

4#
感谢楼主分享!!!
回复 收起回复
2015-7-17 16:37:01   回复
 赞 赞 1

使用道具 登录

5#
啥也不说了,楼主就是威武!
回复 收起回复
2015-7-17 16:48:39   回复
 赞 赞 1

使用道具 登录

6#
给力!元素有你更精彩
回复 收起回复
2015-7-18 09:42:09   回复
 赞 赞 1

使用道具 登录

7#
元素那么大,我想来看看!
回复 收起回复
2015-7-19 12:07:20   回复
 赞 赞 1

使用道具 登录

8#
立刻提起了精神。
回复 收起回复
2015-7-26 01:25:15   回复
 赞 赞 1

使用道具 登录

9#
{:1_147:}
回复 收起回复
2015-7-26 23:17:20   回复
 赞 赞 1

使用道具 登录

10#
高端大气上档次,低调奢华有内涵!
回复 收起回复
2015-8-8 12:23:01   回复
 赞 赞 1

使用道具 登录

11#
资源发布哪家强?元素首发称大王!
回复 收起回复
2015-8-12 11:38:20   回复
 赞 赞 1

使用道具 登录

12#
很好的内容 学习了
回复 收起回复
2016-12-4 23:16:43   回复
 赞 赞 1

使用道具 登录

13#
技术贴
回复 收起回复
2016-12-8 14:13:13   回复
 赞 赞 1

使用道具 登录

14#
过来转转
回复 收起回复
2016-12-9 17:41:37   回复
 赞 赞 1

使用道具 登录

15#
{:1_145:}
回复 收起回复
2016-12-11 12:20:15   回复
 赞 赞 1

使用道具 登录

16#
不错
回复 收起回复
2016-12-27 15:12:42   回复
 赞 赞 1

使用道具 登录

17#
{:1_145:}
回复 收起回复
2017-1-11 02:31:02   回复
 赞 赞 1

使用道具 登录

18#
封装的挺不错的,用C#的StopWatch(好像这么写的),也可以吧。
回复 收起回复
2017-4-26 16:47:27   回复
 赞 赞 1

使用道具 登录

19#
回复 收起回复
2023-2-28 11:11:01   回复
 赞 赞 1

使用道具 登录

CG 游戏行业专业问题

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

本版积分规则

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