系列文章目录
unity小功能——钟表展示
主要功能,包含
1,电子时钟
2,钟表表盘
3,进度表盘
注:支持各种功能扩展
使用的时候需要拷贝基础脚本,扩展脚本可以按照需求下载。
扩展脚本就写这三个了,其他效果自己扩展就行,也比较简单呢,给“onTimeVariation”里面添加参数就行;
基础脚本
using System;
using UnityEngine;
using UnityEngine.Events;
[Serializable]
public struct ClockParam
{
public int totalSecond, hour, minute, second;
}
public class Clock : MonoBehaviour
{
public ClockParam clockParam;
public bool isChange = true;
public bool isTwelveHours = false;
public bool isHour = true;
public bool isMinute = true;
public bool isSecond = true;
private float startTime;
private float delayTime = 1f; // 延时时间为1秒
public UnityEvent<ClockParam> onTimeVariation;
public void Update()
{
if (isChange)
{
float elapsedTime = Time.time - startTime;
if (elapsedTime >= delayTime)
{
startTime = Time.time;
clockParam.totalSecond++;
clockParam = SetClockParam(clockParam.totalSecond++);
}
}
}
public ClockParam SetClockParam(int hour, int minute, int second)
{
ClockParam clockParam;
hour = hour % 24;
int totalSecond = hour * 3600 + minute * 60 + second;
clockParam.totalSecond = totalSecond;
clockParam.hour = hour;
clockParam.minute = minute;
clockParam.second = second;
onTimeVariation?.Invoke(clockParam);
return clockParam;
}
public ClockParam SetClockParam(int totalSecond)
{
ClockParam clockParam;
totalSecond = totalSecond % (24 * 60 * 60);
int hour = totalSecond / 3600;
int minute = totalSecond % 3600 / 60;
int second = totalSecond % 3600 % 60;
clockParam.totalSecond = totalSecond;
clockParam.hour = hour;
clockParam.minute = minute;
clockParam.second = second;
onTimeVariation?.Invoke(clockParam);
return clockParam;
}
/// <summary>
/// 计算指针角度,返回0-360度
/// </summary>
public (float,float,float) PointerDialAngle(ClockParam clockParam)
{
float hourAngle = ((float)clockParam.hour%12* 360 + ((float)clockParam.minute * 360 + (float)clockParam.second * 360 / 60) / 60) /12;
float minuteAnge = ((float) clockParam.minute*360 + (float)clockParam.second * 360 / 60 )/ 60;
float secondAnge = (float)clockParam.second*360 / 60;
return (hourAngle, minuteAnge, secondAnge);
}
/// <summary>
/// 计算指针进度,返回0-1之间
/// </summary>
public (float, float, float)PointerDialProgress(ClockParam clockParam){
float hourAngle = ((float)clockParam.hour%12* 360 + ((float)clockParam.minute * 360 + (float)clockParam.second * 360 / 60) / 60) /12;
float minuteAnge = ((float) clockParam.minute*360 + (float)clockParam.second * 360 / 60 )/ 60;
float secondAnge = (float)clockParam.second*360 / 60;
return (hourAngle / 360, minuteAnge / 360, secondAnge / 360);
}
}
扩展脚本
扩展脚本1,电子时钟
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class ElectronicWatchPanel : Clock
{
public Text text;
private void Awake()
{
clockParam = SetClockParam(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
PointerDialString();
onTimeVariation.AddListener(call=> {
PointerDialString();
});
}
/// <summary>
/// 返回打印时间
/// </summary>
public void PointerDialString()
{
string hourStr = clockParam.hour.ToString().PadLeft(2,'0');
if (isTwelveHours)
{
hourStr = (clockParam.hour % 12).ToString().PadLeft(2, '0');
}
string minuteStr = clockParam.minute.ToString().PadLeft(2, '0');
string secondStr = clockParam.second.ToString().PadLeft(2, '0');
string textStr="";
if (isHour)
{
if (!string.IsNullOrEmpty(textStr))
{
textStr += ":";
}
textStr += hourStr;
}
if (isMinute)
{
if (!string.IsNullOrEmpty(textStr))
{
textStr += ":";
}
textStr += minuteStr;
}
if (isSecond)
{
if (!string.IsNullOrEmpty(textStr))
{
textStr += ":";
}
textStr += secondStr;
}
text.text = textStr;
}
}
扩展脚本2,钟表表盘
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class ClockDialPanel : Clock
{
public Transform hourHand;
public Transform minuteHand;
public Transform secondHand;
private void Awake()
{
clockParam = SetClockParam(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
PointerDial(hourHand, minuteHand, secondHand);
onTimeVariation.AddListener(call=> {
PointerDial(hourHand, minuteHand, secondHand);
});
}
public void PointerDial(Transform hourHand, Transform minuteHand, Transform secondHand)
{
(float, float, float) Angle = PointerDialAngle(clockParam);
hourHand.localEulerAngles = -Vector3.forward * Angle.Item1;
minuteHand.localEulerAngles = -Vector3.forward * Angle.Item2;
secondHand.localEulerAngles = -Vector3.forward * Angle.Item3;
hourHand.gameObject.SetActive(isHour ? true : false);
minuteHand.gameObject.SetActive(isMinute ? true : false);
secondHand.gameObject.SetActive(isSecond ? true : false);
}
}
扩展脚本3,进度表盘
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class ProgressDialPanel : Clock
{
public Image hourHand;
public Image minuteHand;
public Image secondHand;
private void Awake()
{
clockParam = SetClockParam(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
PointerDial(hourHand, minuteHand, secondHand);
onTimeVariation.AddListener(call=> {
PointerDial(hourHand, minuteHand, secondHand);
});
}
public void PointerDial(Image hourHand, Image minuteHand, Image secondHand)
{
(float, float, float) progress = PointerDialProgress(clockParam);
hourHand.fillAmount = progress.Item1;
minuteHand.fillAmount = progress.Item2;
secondHand.fillAmount = progress.Item3;
hourHand.gameObject.SetActive(isHour ? true : false);
minuteHand.gameObject.SetActive(isMinute ? true : false);
secondHand.gameObject.SetActive(isSecond ? true : false);
}
}