UICalculagraph.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 计时器
/// </summary>
public class UICalculagraph : MonoBehaviour
{
[SerializeField]
private Text m_Text;
[SerializeField]
private bool m_PlayOnAwake = true;
private int m_TotalSecond = 0;
private void Awake()
{
if (m_PlayOnAwake)
Play();
}
public void Play()
{
m_TotalSecond = 0;
InvokeRepeating("Increase", 1, 1);
}
public void Stop()
{
CancelInvoke();
}
public void Show()
{
gameObject.SetActive(true);
}
public void Hide()
{
gameObject.SetActive(false);
}
private void Increase()
{
m_TotalSecond++;
if (m_Text)
m_Text.text = TimeFormat();
}
private string TimeFormat()
{
int SECOND_PER_HOUR = 3600; //1小时多少秒
int SECOND_PER_MINUTE = 60; //1分钟多少秒
int hour=0, minute=0, second=0;
if (m_TotalSecond < 60)
{
second = m_TotalSecond % SECOND_PER_MINUTE;
}
else if (m_TotalSecond < SECOND_PER_HOUR)
{
minute = m_TotalSecond / SECOND_PER_MINUTE;
second = m_TotalSecond % SECOND_PER_MINUTE;
}
else
{
hour = m_TotalSecond / SECOND_PER_HOUR;
minute = (m_TotalSecond - hour * SECOND_PER_HOUR) / SECOND_PER_MINUTE;
second = m_TotalSecond % SECOND_PER_MINUTE;
}
return string.Format("{0}:{1}:{2}", hour.ToString("D2"), minute.ToString("D2"), second.ToString("D2"));
}
}
UIFlickerEffect.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 闪烁效果
/// </summary>
public class UIFlickerEffect : MonoBehaviour
{
[SerializeField]
private Image m_Image;
[SerializeField]
private float m_Frequency = 3;
[SerializeField]
private bool m_PlayOnAwake = true;
private float m_Interval = 0;
private bool m_SwitchBool = true;
private bool m_Playing = false;
private float t = 0;
private void Awake()
{
if (m_PlayOnAwake)
Play();
}
public void Play()
{
m_Interval = 1.0f / m_Frequency;
t = 0;
m_Playing = true;
}
public void Stop()
{
m_Playing = false;
}
public void Show()
{
gameObject.SetActive(true);
}
public void Hide()
{
gameObject.SetActive(false);
}
private void Update()
{
if (!m_Playing)
return;
t += Time.deltaTime;
if (t < m_Interval)
return;
t = 0;
m_Image.enabled = m_SwitchBool;
m_SwitchBool = !m_SwitchBool;
}
}
运行效果