Unity实现倒计时和获取系统时间

本文介绍了在Unity中实现倒计时和获取系统时间的步骤。首先,通过UGUI创建计时器和系统时间显示文本。接着,编写TimeManager脚本管理倒计时。为了在游戏暂停时停止倒计时,使用MenuManager和GameManager进行配合。最后展示了运行效果,倒计时在暂停游戏时停止,而系统时间始终跟随电脑时间更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

5a1b3a2c6acd4a4aaab213f5bc51c7dd.gif

 

一:创建UGUI

1.创建Canvas画布组件,调节Canvas画布的分辨率等其他设置。我们可以把视图设置为2D模式下。

32975ae81839477898d71396b641f17f.png

 

2.创建Text文本组件,取名为Timer计时器,我们调整Text文本组件的大小,用锚点设置Text文本组件的位置,并且设置好Text文本组件的颜色。

b140fb7f925a4d44ba64448ed84042e5.png

 

3.我们再创建一个Text文本组件,取名为SystemTime系统时间,我们和上面第2步的操作一模一样&#x

### 实现 Unity 中的计时系统Unity实现一个完整的计时系统可以通过多种方式完成,其中最常见的是利用协程(Coroutine),并将其封装到一个管理器类中以便于扩展维护。以下是基于提供的参考资料以及专业知识构建的一个完整解决方案。 #### 1. 使用 `TimerManager` 管理计时器 为了使计时系统更加模块化易于管理,可以创建一个名为 `TimerManager` 的管理器类来统一处理所有类型的计时器实例。该管理器能够支持单次执行、周期性执行以及倒计时等功能[^3]。 ```csharp using System.Collections; using UnityEngine; public class TimerManager : MonoBehaviour { private static TimerManager _instance; public static TimerManager Instance => _instance ??= FindObjectOfType<TimerManager>(); private void Awake() { if (_instance == null) _instance = this; else if (_instance != this) Destroy(gameObject); } /// <summary> /// 创建一个单次定时器 /// </summary> public Coroutine CreateOneShotTimer(float delay, System.Action callback) { return StartCoroutine(OneShotTimer(delay, callback)); } private IEnumerator OneShotTimer(float delay, System.Action callback) { yield return new WaitForSeconds(delay); callback?.Invoke(); } /// <summary> /// 创建一个重复定时器 /// </summary> public Coroutine CreateRepeatingTimer(float interval, System.Action callback) { return StartCoroutine(RepeatingTimer(interval, callback)); } private IEnumerator RepeatingTimer(float interval, System.Action callback) { while (true) { callback?.Invoke(); yield return new WaitForSeconds(interval); } } /// <summary> /// 倒计时功能 /// </summary> public Coroutine CreateCountdownTimer(float duration, System.Action<float> onTick, System.Action onComplete) { return StartCoroutine(CountdownTimer(duration, onTick, onComplete)); } private IEnumerator CountdownTimer(float duration, System.Action<float> onTick, System.Action onComplete) { float timeLeft = duration; while (timeLeft > 0) { onTick?.Invoke(timeLeft); yield return new WaitForSeconds(0.1f); // 更新频率可以根据需求调整 timeLeft -= 0.1f; } onComplete?.Invoke(); } } ``` 上述代码实现了三种基本的功能:单次延迟执行、周期性执行以及倒计时功能。通过这些基础组件,开发者可以轻松地满足大多数场景下的计时需求。 #### 2. UI 显示倒计时 如果需要将倒计时结果显示给玩家,则可以结合 Unity 的 UI 系统,在屏幕上动态更新剩余时间[^2]。下面展示了一个简单的例子: ```csharp using UnityEngine; using UnityEngine.UI; public class CountdownDisplay : MonoBehaviour { public Text countdownText; // 需要绑定到 Canvas 上的文字对象 private Coroutine _countdownRoutine; public void StartCountdown(float duration) { StopCurrentCountdown(); _countdownRoutine = TimerManager.Instance.CreateCountdownTimer( duration, remainingTime => { int seconds = Mathf.CeilToInt(remainingTime); countdownText.text = $"Remaining Time: {seconds}"; }, () => { countdownText.text = "Time's up!"; }); } public void StopCurrentCountdown() { if (_countdownRoutine != null) { TimerManager.Instance.StopCoroutine(_countdownRoutine); _countdownRoutine = null; } } } ``` 此脚本允许用户指定持续时间目标文字框,并自动刷新其内容直到结束为止。 #### 3. 获取系统当前时间 除了内部逻辑控制外,有时还需要同步现实世界的时间戳来进行某些特殊效果或者记录事件发生时刻。这通常借助 C# 提供的标准库函数完成[^1]: ```csharp System.DateTime now = System.DateTime.Now; Debug.Log($"The current system date and time is {now}"); ``` 这样就可以方便地读取设备上的实际日期与钟表数值了。 --- ###
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星辰大海6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值