From 97d4ff045024a029121c9d9fbcc89765928074ac Mon Sep 17 00:00:00 2001 From: stary <834207172@qq.com> Date: Tue, 12 May 2026 13:15:50 +0800 Subject: [PATCH] =?UTF-8?q?time=20=E5=A2=9E=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/00.StaryEvo/Runtime/Tool/Timer.meta | 8 +++ .../Runtime/Tool/Timer/TimerController.cs | 56 ++++++++++++++++ .../Tool/Timer/TimerController.cs.meta | 11 +++ .../Runtime/Tool/Timer/TimerSystem.cs | 67 +++++++++++++++++++ .../Runtime/Tool/Timer/TimerSystem.cs.meta | 11 +++ Assets/00.StaryEvo/package.json | 2 +- 6 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 Assets/00.StaryEvo/Runtime/Tool/Timer.meta create mode 100644 Assets/00.StaryEvo/Runtime/Tool/Timer/TimerController.cs create mode 100644 Assets/00.StaryEvo/Runtime/Tool/Timer/TimerController.cs.meta create mode 100644 Assets/00.StaryEvo/Runtime/Tool/Timer/TimerSystem.cs create mode 100644 Assets/00.StaryEvo/Runtime/Tool/Timer/TimerSystem.cs.meta diff --git a/Assets/00.StaryEvo/Runtime/Tool/Timer.meta b/Assets/00.StaryEvo/Runtime/Tool/Timer.meta new file mode 100644 index 0000000..8bc57e2 --- /dev/null +++ b/Assets/00.StaryEvo/Runtime/Tool/Timer.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: cdd5f630d231efb48b075b3b7c318b34 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/00.StaryEvo/Runtime/Tool/Timer/TimerController.cs b/Assets/00.StaryEvo/Runtime/Tool/Timer/TimerController.cs new file mode 100644 index 0000000..4221709 --- /dev/null +++ b/Assets/00.StaryEvo/Runtime/Tool/Timer/TimerController.cs @@ -0,0 +1,56 @@ +using System; +using Stary.Evo; +using UnityEngine; +using UnityEngine.Serialization; + +namespace Stary.Evo +{ + public class TimerController : MonoBehaviour, IController + { + public string TimerName => gameObject.name; + private bool isStart = false; + + /// + /// 持续时间 + /// + private float timeDuration; + + + /// + /// 时间到达的事件 + /// + private Action onArrivalTimeComplete; + + /// + /// 持续时间最大阈值 + /// + public float timeDurationMax; + + public void StartTimer(float timeDurationMax, Action onArrivalTimeComplete) + { + this.timeDurationMax = timeDurationMax; + this.onArrivalTimeComplete = onArrivalTimeComplete; + timeDuration = 0; + isStart = true; + } + + private void Update() + { + if (isStart) + { + timeDuration += Time.deltaTime; + if (timeDuration >= timeDurationMax) + { + onArrivalTimeComplete?.Invoke(); + this.GetSystem().RemoveTimer(TimerName); + } + } + } + + public IArchitecture GetArchitecture() + { + ITimerSystem system = this.GetSystem(); + return system.GetArchitecture(); + } + } +} \ No newline at end of file diff --git a/Assets/00.StaryEvo/Runtime/Tool/Timer/TimerController.cs.meta b/Assets/00.StaryEvo/Runtime/Tool/Timer/TimerController.cs.meta new file mode 100644 index 0000000..58dff3e --- /dev/null +++ b/Assets/00.StaryEvo/Runtime/Tool/Timer/TimerController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a0837a1376a9b5f4db88fe9e9601b981 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/00.StaryEvo/Runtime/Tool/Timer/TimerSystem.cs b/Assets/00.StaryEvo/Runtime/Tool/Timer/TimerSystem.cs new file mode 100644 index 0000000..60d979f --- /dev/null +++ b/Assets/00.StaryEvo/Runtime/Tool/Timer/TimerSystem.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using Stary.Evo; +using UnityEngine; + +namespace Stary.Evo +{ + public interface ITimerSystem : ISystem + { + void CreateTimer(string timerName, float timeDuration, Action onArrivalTimeComplete); + void RemoveTimer(string timerName); + } + + public class TimerSystem : AbstractSystem, ITimerSystem + { + private static Dictionary + _timerControllers = new Dictionary(); + + private static GameObject timerSystem; + + protected override void OnInit() + { + _timerControllers = new Dictionary(); + timerSystem = new GameObject("Timer"); + } + + public void CreateTimer(string timerName, float timeDuration, Action onArrivalTimeComplete) + { + if (_timerControllers.ContainsKey(timerName)) + { + Debug.LogError($"TimerSystem CreateTimer 重复创建定时器 {timerName}"); + return; + } + + var _timer = new GameObject(timerName); + _timer.transform.SetParent(timerSystem.transform); + var timerController = _timer.AddComponent(); + _timerControllers.Add(timerName, timerController); + timerController.StartTimer(timeDuration, onArrivalTimeComplete); + } + + public void RemoveTimer(string timerName) + { + if (_timerControllers.ContainsKey(timerName)) + { + var timerController = _timerControllers[timerName]; + GameObject.Destroy(timerController.gameObject); + _timerControllers.Remove(timerName); + } + } + + + + public override void Dispose() + { + foreach (var controller in _timerControllers) + { + if (controller.Value != null) + { + GameObject.Destroy(controller.Value.gameObject); + } + } + + GameObject.Destroy(timerSystem.gameObject); + } + } +} \ No newline at end of file diff --git a/Assets/00.StaryEvo/Runtime/Tool/Timer/TimerSystem.cs.meta b/Assets/00.StaryEvo/Runtime/Tool/Timer/TimerSystem.cs.meta new file mode 100644 index 0000000..44a6523 --- /dev/null +++ b/Assets/00.StaryEvo/Runtime/Tool/Timer/TimerSystem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8b5962b06d3756b41b29b0496e9e996f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/00.StaryEvo/package.json b/Assets/00.StaryEvo/package.json index 5a7490d..cc63eb3 100644 --- a/Assets/00.StaryEvo/package.json +++ b/Assets/00.StaryEvo/package.json @@ -1,6 +1,6 @@ { "name": "com.staryevo.main", - "version": "2.1.14", + "version": "2.1.15", "displayName": "00.StaryEvo", "description": "This is an Framework package(后台服务器版本,端口9527)", "unity": "2021.3",