Unity3D 计时/倒计时管理类
2016-08-29 20:18
204 查看
本文永久地址:http://www.omuying.com/article/124.aspx,【文章转载请注明出处!】
在平时的开发过程中,计时、倒计时经常在游戏中用到,想必大家都有自己的处理高招,为了以后用方便使用,抽象出一个 TimerManager 类,功能很简单,这儿采用协程的 WaitForSeconds 来处理计时、倒计时,希望对你有所帮助!
主要代码如下:
view
sourceprint?
在平时的开发过程中,计时、倒计时经常在游戏中用到,想必大家都有自己的处理高招,为了以后用方便使用,抽象出一个 TimerManager 类,功能很简单,这儿采用协程的 WaitForSeconds 来处理计时、倒计时,希望对你有所帮助!
主要代码如下:
view
sourceprint?
001 | using UnityEngine; |
002 | using System; |
003 | using System.Collections; |
004 | using System.Collections.Generic; |
005 |
006 | public class TimerManager |
007 | { |
008 | private static Dictionary< string , TimerItem> dictList = new Dictionary< string , TimerItem>(); |
009 |
010 | /// <summary> |
011 | /// 注册计时 |
012 | /// </summary> |
013 | /// <param name="timerKey">Timer key.</param> |
014 | /// <param name="totalNum">Total number.</param> |
015 | /// <param name="delayTime">Delay time.</param> |
016 | /// <param name="callback">Callback.</param> |
017 | /// <param name="endCallback">End callback.</param> |
018 | public static void Register( string timerKey, int totalNum, float delayTime, Action< int > callback, Action endCallback) |
019 | { |
020 | TimerItem timerItem = null ; |
021 | if (!dictList.ContainsKey(timerKey)) |
022 | { |
023 | GameObject objectItem = new GameObject (); |
024 | objectItem.name = timerKey; |
025 |
026 | timerItem = objectItem.AddComponent<TimerItem> (); |
027 | dictList.Add(timerKey, timerItem); |
028 | } else |
029 | { |
030 | timerItem = dictList[timerKey]; |
031 | } |
032 |
033 | if (timerItem != null ) |
034 | { |
035 | timerItem.Run(totalNum, delayTime, callback, endCallback); |
036 | } |
037 | } |
038 |
039 | /// <summary> |
040 | /// 取消注册计时 |
041 | /// </summary> |
042 | /// <param name="timerKey">Timer key.</param> |
043 | public static void UnRegister( string timerKey) |
044 | { |
045 | if (!dictList.ContainsKey(timerKey)) return ; |
046 |
047 | TimerItem timerItem =dictList [timerKey]; |
048 | if (timerItem != null ) |
049 | { |
050 | timerItem.Stop (); |
051 | GameObject.Destroy(timerItem.gameObject); |
052 | } |
053 | } |
054 | } |
055 |
056 | class TimerItem : MonoBehaviour |
057 | { |
058 | private int totalNum; |
059 | private float delayTime; |
060 | private Action< int > callback; |
061 | private Action endCallback; |
062 |
063 | private int currentIndex; |
064 |
065 | public void Run( int totalNum, float delayTime, Action< int > callback, Action endCallback) |
066 | { |
067 | this .Stop (); |
068 |
069 | this .currentIndex = 0; |
070 |
071 | this .totalNum = totalNum; |
072 | this .delayTime = delayTime; |
073 | this .callback = callback; |
074 | this .endCallback = endCallback; |
075 |
076 | this .StartCoroutine ( "EnumeratorAction" ); |
077 | } |
078 |
079 | public void Stop() |
080 | { |
081 | this .StopCoroutine ( "EnumeratorAction" ); |
082 | } |
083 |
084 | private IEnumerator EnumeratorAction() |
085 | { |
086 | yield return new WaitForSeconds ( this .delayTime); |
087 |
088 | this .currentIndex ++; |
089 | if ( this .callback != null ) this .callback( this .currentIndex); |
090 |
091 | if ( this .totalNum != -1) |
092 | { |
093 | if ( this .currentIndex >= this .totalNum) |
094 | { |
095 | if ( this .endCallback != null ) this .endCallback(); |
096 | yield break ; |
097 | } |
098 | } |
099 | this .StartCoroutine( "EnumeratorAction" ); |
100 | } |
101 | } |
相关文章推荐
- 【Unity3d】设计自己的计时类
- Android实现计时与倒计时(限时抢购)的几种方法
- unity3d游戏开发 —— 倒计时
- JS计时与倒计时
- 刷屏倒计时:[超级马里奥Run]蓄势待发_广州达内Unity3D培训机构
- Unity3d背包系统(四)—— 设计物品管理类——InventoryManager
- unity3d 倒计时中最后几秒颜色改变的方法
- 使用js倒计时还有几天及计时过了几天
- Unity3D脚本:C#计时类脚本
- 正计时、倒计时
- 正计时与倒计时
- Android实现计时与倒计时(限时抢购)的几种方法
- Android中 完美实现 计时 倒计时 时间间隔处理等功能实现 by CountDownTimer
- Android实现计时与倒计时(限时抢购)的几种方法
- Unity3D C# 简单的倒计时
- Android实现计时与倒计时的几种方法
- AndroidTimer 简单的倒计时、正计时切换
- Html5 倒计时/正计时 功能完成(一)
- unity3d游戏开发 —— 倒计时
- Unity3D脚本:C#计时类脚本