UFL10-1——TimerItem 具体实现

TimerItem 具体实现

由于计时器对象类是由计时器模块管理器管理,而且常常创建和销毁,因此该类对象可以由缓存池管理

1
2
3
4
5
6
7
8
9
using UnityEngine.Events;

/// <summary>
/// 计时器对象,里面存储了计时器的相关数据
/// </summary>
public class TimerItem : IPoolObject
{
public void ResetInfo() { }
}
  • 需要声明的关键成员

    1. 唯一ID
    2. 延时执行回调委托
    3. 间隔执行回到委托
    4. 总时间(毫秒)
    5. 间隔时间(毫秒)
    6. 是否开启

    等等

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    /// <summary>
    /// 唯一ID
    /// </summary>
    public int keyID;
    /// <summary>
    /// 计时结束后的委托回调
    /// </summary>
    public UnityAction overCallBack;
    /// <summary>
    /// 间隔一定时间去执行的委托回调
    /// </summary>
    public UnityAction callBack;

    /// <summary>
    /// 毫秒 表示计时器总的计时时间 毫秒:1s = 1000ms
    /// </summary>
    public int allTimeMiniSec;
    /// <summary>
    /// 记录一开始计时d的总时间,用于时间重置
    /// </summary>
    public int maxAllTimeMiniSec;
    /// <summary>
    /// 间隔执行的回调的时间 毫秒:1s = 1000ms
    /// </summary>
    public int intervalTimeMiniSec;
    /// <summary>
    /// 记录一开始的间隔时间,用于时间重置
    /// </summary>
    public int maxIntervalTimeMiniSec;
    /// <summary>
    /// 是否在进行计时
    /// </summary>
    public bool isRunning;
  • 需要实现的关键方法

    1. 初始化数据

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      /// <summary>
      /// 初始化计时器数据
      /// </summary>
      /// <param name="keyID">唯一ID</param>
      /// <param name="allTime">总的时间</param>
      /// <param name="overCallBack">总时间计时结束后执行的回调</param>
      /// <param name="intervalTime">间隔时间</param>
      /// <param name="callBack">间隔时间结束后执行的回调</param>
      public void InitInfo(int keyID, int allTimeMiliSec, UnityAction overCallBack, int intervalTimeMiliSec = 0, UnityAction callBack = null)
      {
      this.keyID = keyID;
      this.maxAllTimeMiniSec = this.allTimeMiniSec = allTimeMiliSec;
      this.overCallBack = overCallBack;
      this.maxIntervalTimeMiniSec = this.intervalTimeMiniSec = intervalTimeMiliSec;
      this.callBack = callBack;
      this.isRunning = true;
      }
    2. 重置时间

      1
      2
      3
      4
      5
      6
      7
      8
      9
      /// <summary>
      /// 重置计时器
      /// </summary>
      public void ResetTimer()
      {
      this.allTime = this.maxAllTime;
      this.intervalTime = this.maxIntervalTime;
      this.isRunning = true;
      }
    3. 缓存池管理需要的重置方法

      1
      2
      3
      4
      5
      6
      7
      8
      /// <summary>
      /// 缓存池回收时,取出相关引用数据
      /// </summary>
      public void ResetInfo()
      {
      overCallBack = null;
      callBack = null;
      }

    等等

具体代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using UnityEngine.Events;

/// <summary>
/// 计时器对象,里面存储了计时器的相关数据
/// </summary>
public class TimerItem : IPoolObject
{
/// <summary>
/// 唯一ID
/// </summary>
public int keyID;
/// <summary>
/// 计时结束后的委托回调
/// </summary>
public UnityAction overCallBack;
/// <summary>
/// 间隔一定时间去执行的委托回调
/// </summary>
public UnityAction callBack;

/// <summary>
/// 毫秒 表示计时器总的计时时间 毫秒:1s = 1000ms
/// </summary>
public int allTimeMiniSec;
/// <summary>
/// 记录一开始计时d的总时间,用于时间重置
/// </summary>
public int maxAllTimeMiniSec;
/// <summary>
/// 间隔执行的回调的时间 毫秒:1s = 1000ms
/// </summary>
public int intervalTimeMiniSec;
/// <summary>
/// 记录一开始的间隔时间,用于时间重置
/// </summary>
public int maxIntervalTimeMiniSec;
/// <summary>
/// 是否在进行计时
/// </summary>
public bool isRunning;

/// <summary>
/// 初始化计时器数据
/// </summary>
/// <param name="keyID">唯一ID</param>
/// <param name="allTime">总的时间</param>
/// <param name="overCallBack">总时间计时结束后执行的回调</param>
/// <param name="intervalTime">间隔时间</param>
/// <param name="callBack">间隔时间结束后执行的回调</param>
public void InitInfo(int keyID, int allTimeMiliSec, UnityAction overCallBack, int intervalTimeMiliSec = 0, UnityAction callBack = null)
{
this.keyID = keyID;
this.maxAllTimeMiniSec = this.allTimeMiniSec = allTimeMiliSec;
this.overCallBack = overCallBack;
this.maxIntervalTimeMiniSec = this.intervalTimeMiniSec = intervalTimeMiliSec;
this.callBack = callBack;
this.isRunning = true;
}

/// <summary>
/// 重置计时器
/// </summary>
public void ResetTimer()
{
this.allTimeMiniSec = this.maxAllTimeMiniSec;
this.intervalTimeMiniSec = this.maxIntervalTimeMiniSec;
this.isRunning = true;
}

/// <summary>
/// 缓存池回收时,取出相关引用数据
/// </summary>
public void ResetInfo()
{
overCallBack = null;
callBack = null;
}
}