From 5c6d49f48e70fd37919e948468a1c24d5aa2990e Mon Sep 17 00:00:00 2001
From: client_Wu Xijin <364452445@qq.com>
Date: 星期五, 12 四月 2019 21:09:24 +0800
Subject: [PATCH] 6498 【前端】【2.0】主界面技能转盘修改
---
Utility/Clock.cs | 119 +++++++++++++++++++++++++++++++----------------------------
1 files changed, 62 insertions(+), 57 deletions(-)
diff --git a/Utility/Clock.cs b/Utility/Clock.cs
index 71158d4..2998e8c 100644
--- a/Utility/Clock.cs
+++ b/Utility/Clock.cs
@@ -1,80 +1,85 @@
锘縰sing System;
using UnityEngine;
+using System.Collections.Generic;
using UnityEngine.Events;
-public class Clock : MonoBehaviour
+public class Clock
{
- public bool stopped { get; private set; }
-
- public DateTime alarmTime {
- get; set;
- }
-
+ DateTime alarmTime;
UnityAction alarmCallBack;
- public void AddListener(UnityAction _action)
+ public Clock(DateTime alarmTime, UnityAction callBack)
{
- alarmCallBack += _action;
+ this.alarmTime = alarmTime;
+ this.alarmCallBack = callBack;
}
- public void Stop()
+ public bool CanAlarm()
{
- stopped = true;
- GameObject.Destroy(this.gameObject);
+ return DateTime.Now >= alarmTime;
}
- private void Awake()
+ public void Execute()
{
- this.gameObject.hideFlags = HideFlags.HideInHierarchy;
- }
-
- private void LateUpdate()
- {
- if (System.DateTime.Now > alarmTime)
+ if (alarmCallBack != null)
{
- try
+ alarmCallBack();
+ alarmCallBack = null;
+ }
+ }
+
+ static List<Clock> clocks = new List<Clock>();
+
+ public static void Init()
+ {
+ new LogicUpdate().Start(OnUpdate);
+ }
+
+ public static Clock AlarmAt(DateTime alarmTime, UnityAction callBack)
+ {
+ var clock = new Clock(alarmTime, callBack);
+
+ clocks.Add(clock);
+ return clock;
+ }
+
+ public static Clock AlarmAfter(int seconds, UnityAction callBack)
+ {
+ var clock = new Clock(DateTime.Now.AddSeconds(seconds), callBack);
+
+ clocks.Add(clock);
+ return clock;
+ }
+
+ public static void Stop(Clock clock)
+ {
+ if (clocks.Contains(clock))
+ {
+ clocks.Remove(clock);
+ }
+ }
+
+ static void OnUpdate()
+ {
+ for (var i = clocks.Count - 1; i >= 0; i--)
+ {
+ var clock = clocks[i];
+ if (clock.CanAlarm())
{
- if (alarmCallBack != null)
+ try
{
- alarmCallBack();
- alarmCallBack = null;
+ clock.Execute();
+ }
+ catch (Exception e)
+ {
+ Debug.LogException(e);
+ }
+ finally
+ {
+ clocks.RemoveAt(i);
}
}
- catch (System.Exception ex)
- {
- DebugEx.Log(ex);
- }
- finally
- {
- Stop();
- }
}
-
- }
-
-
- public static Clock Create(DateTime _alarmTime, UnityAction _action)
- {
- var carrier = new GameObject();
- GameObject.DontDestroyOnLoad(carrier);
-
- var clock = carrier.AddComponent<Clock>();
- clock.alarmTime = _alarmTime;
- clock.AddListener(_action);
-
- return clock;
- }
-
- public static Clock Create(int _seconds, UnityAction _action)
- {
- var carrier = new GameObject();
- GameObject.DontDestroyOnLoad(carrier);
-
- var clock = carrier.AddComponent<Clock>();
- clock.alarmTime = System.DateTime.Now + new TimeSpan(_seconds * TimeSpan.TicksPerSecond);
- clock.AddListener(_action);
-
- return clock;
}
}
--
Gitblit v1.8.0