添加倒计时
This commit is contained in:
parent
459da470c9
commit
3040218c5a
|
|
@ -0,0 +1,101 @@
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.UI;
|
||||||
|
using UnityEngine.Events;
|
||||||
|
using TMPro;
|
||||||
|
|
||||||
|
public class Timers : MonoBehaviour
|
||||||
|
{
|
||||||
|
private bool isRun = false;
|
||||||
|
public float limitTime = 0;
|
||||||
|
private TextMeshProUGUI guiTimer;
|
||||||
|
public UnityAction<string> timeOver;
|
||||||
|
|
||||||
|
public void SetTime(int _time, TextMeshProUGUI timeText, UnityAction<string> timeOverCallback)
|
||||||
|
{
|
||||||
|
limitTime = _time * 60;
|
||||||
|
guiTimer = timeText;
|
||||||
|
guiTimer.text = "倒计时:" + StringHelper.GetCountDownStr((int)limitTime);
|
||||||
|
timeOver = timeOverCallback;
|
||||||
|
RunTimer();
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetCurrentTime()
|
||||||
|
{
|
||||||
|
EndTimer();
|
||||||
|
return limitTime.ToString("0");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 运行计时器
|
||||||
|
public void RunTimer()
|
||||||
|
{
|
||||||
|
isRun = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 暂停或恢复计时器
|
||||||
|
public void PauseTimer()
|
||||||
|
{
|
||||||
|
isRun = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 结束计时器
|
||||||
|
public void EndTimer()
|
||||||
|
{
|
||||||
|
isRun = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update 方法在每一帧运行,用于更新计时器的状态和显示
|
||||||
|
private void Update()
|
||||||
|
{
|
||||||
|
if (isRun)
|
||||||
|
Timer();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 计时
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
private void Timer()
|
||||||
|
{
|
||||||
|
guiTimer.text = StringHelper.GetCountDownStr((int)limitTime);
|
||||||
|
if (limitTime >= 0)
|
||||||
|
{
|
||||||
|
limitTime -= Time.deltaTime;
|
||||||
|
guiTimer.text = "倒计时:" + StringHelper.GetCountDownStr((int)limitTime);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//Debug.Log("计时结束");
|
||||||
|
limitTime = 0;
|
||||||
|
guiTimer.text = "倒计时:" + StringHelper.GetCountDownStr((int)limitTime);
|
||||||
|
timeOver?.Invoke(limitTime.ToString());
|
||||||
|
isRun = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StringHelper
|
||||||
|
{
|
||||||
|
public const string format = "{0}:{1}:{2}";
|
||||||
|
|
||||||
|
public const char zero = '0';
|
||||||
|
|
||||||
|
public const int hour2Second = 3600;
|
||||||
|
|
||||||
|
public const int minute2Second = 60;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 倒计时格式
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="second">秒数</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string GetCountDownStr(int second)
|
||||||
|
{
|
||||||
|
int hours = second / 3600;
|
||||||
|
int minutes = (second % 3600) / 60;
|
||||||
|
int seconds = second % 60;
|
||||||
|
|
||||||
|
return string.Format("{0:00}:{1:00}:{2:00}", hours, minutes, seconds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 18387162de4e32c4d9463506cca9a9d8
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
|
|
@ -11,6 +11,7 @@ GameObject:
|
||||||
- component: {fileID: 912765198}
|
- component: {fileID: 912765198}
|
||||||
- component: {fileID: 912765200}
|
- component: {fileID: 912765200}
|
||||||
- component: {fileID: 912765199}
|
- component: {fileID: 912765199}
|
||||||
|
- component: {fileID: 7427894153927231783}
|
||||||
m_Layer: 5
|
m_Layer: 5
|
||||||
m_Name: modelText (TMP)
|
m_Name: modelText (TMP)
|
||||||
m_TagString: Untagged
|
m_TagString: Untagged
|
||||||
|
|
@ -135,6 +136,19 @@ MonoBehaviour:
|
||||||
m_hasFontAssetChanged: 0
|
m_hasFontAssetChanged: 0
|
||||||
m_baseMaterial: {fileID: 0}
|
m_baseMaterial: {fileID: 0}
|
||||||
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
|
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
|
||||||
|
--- !u!114 &7427894153927231783
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 912765197}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: 18387162de4e32c4d9463506cca9a9d8, type: 3}
|
||||||
|
m_Name:
|
||||||
|
m_EditorClassIdentifier:
|
||||||
|
limitTime: 0
|
||||||
--- !u!1 &1209595290
|
--- !u!1 &1209595290
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|
@ -1073,6 +1087,8 @@ MonoBehaviour:
|
||||||
m_Script: {fileID: 11500000, guid: f086b9c0b118c40418934216517cd57c, type: 3}
|
m_Script: {fileID: 11500000, guid: f086b9c0b118c40418934216517cd57c, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
|
modelText: {fileID: 912765199}
|
||||||
|
timer: {fileID: 7427894153927231783}
|
||||||
--- !u!1 &4884683979387215420
|
--- !u!1 &4884683979387215420
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,8 @@ public class UI_MainTitlePanel : BasePanel
|
||||||
//public GameObject timeInfo;
|
//public GameObject timeInfo;
|
||||||
//public GameObject modelInfo;
|
//public GameObject modelInfo;
|
||||||
//public GameObject useNameInfo;
|
//public GameObject useNameInfo;
|
||||||
|
public TextMeshProUGUI modelText;
|
||||||
|
public Timers timer;
|
||||||
internal void Init()
|
internal void Init()
|
||||||
{
|
{
|
||||||
SwitchTitleImg();
|
SwitchTitleImg();
|
||||||
|
|
@ -66,7 +68,8 @@ public class UI_MainTitlePanel : BasePanel
|
||||||
GetControl<Image>("modelText").gameObject.SetActive(mode != "");
|
GetControl<Image>("modelText").gameObject.SetActive(mode != "");
|
||||||
//GetControl<Image>("userName").gameObject.SetActive(mode != string.Empty && mode != "学习模式");
|
//GetControl<Image>("userName").gameObject.SetActive(mode != string.Empty && mode != "学习模式");
|
||||||
///模式显示text
|
///模式显示text
|
||||||
GetControl<TextMeshProUGUI>("modelText (TMP)").text = mode;
|
|
||||||
|
modelText.text = mode;
|
||||||
|
|
||||||
}
|
}
|
||||||
private void InitializationUI()
|
private void InitializationUI()
|
||||||
|
|
@ -74,6 +77,13 @@ public class UI_MainTitlePanel : BasePanel
|
||||||
GetControl<Button>("upExamBtn").gameObject.SetActive(RunModelMgr.Instance.ModeType == E_ModeType.Exam);
|
GetControl<Button>("upExamBtn").gameObject.SetActive(RunModelMgr.Instance.ModeType == E_ModeType.Exam);
|
||||||
GetControl<Button>("submitBtn").gameObject.SetActive(RunModelMgr.Instance.ModeType == E_ModeType.Practice);
|
GetControl<Button>("submitBtn").gameObject.SetActive(RunModelMgr.Instance.ModeType == E_ModeType.Practice);
|
||||||
//GetControl<Image>("timeText").gameObject.SetActive(RunModelMgr.Instance.ModeType == E_ModeType.Exam);
|
//GetControl<Image>("timeText").gameObject.SetActive(RunModelMgr.Instance.ModeType == E_ModeType.Exam);
|
||||||
|
if (GameManager.RunModelMgr.ModeType == E_ModeType.Exam)
|
||||||
|
{
|
||||||
|
timer.SetTime(GameManager.NetMgr.totalTime, modelText, (s) =>
|
||||||
|
{
|
||||||
|
GameManager.EventMgr.EventTrigger(Enum_EventType.Submit);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ResetByModeType()
|
public void ResetByModeType()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue