Unity时钟定时器插件——Vision Timer源码分析之二
前面的已经介绍了vp_Timer(点击前往查看),vp_TimeUtility相对简单很多,vp_TimeUtility定义了个表示时间的结构Units:
- /// <summary>
- /// represents a time measured in standard units
- /// </summary>
- public struct Units
- {
- public int hours;
- public int minutes;
- public int seconds;
- public int deciSeconds; // a.k.a. 'tenths of a second'
- public int centiSeconds; // a.k.a. 'hundredths of a second'
- public int milliSeconds;
- }
下面就直接贴出时间与Units的转换,Units转换为string,时间转换为时钟转过的度数的实现
1.时间与Units的相互转换:
- public static Units TimeToUnits(float timeInSeconds)
- {
- Units iTime = new Units();
- iTime.hours = ((int)timeInSeconds) / 3600;
- iTime.minutes = (((int)timeInSeconds) - (iTime.hours * 3600)) / 60;
- iTime.seconds = ((int)timeInSeconds) % 60;
- iTime.deciSeconds = (int)((timeInSeconds - iTime.seconds) * 10) % 60;
- iTime.centiSeconds = (int)((timeInSeconds - iTime.seconds) * 100 % 600);
- iTime.milliSeconds = (int)((timeInSeconds - iTime.seconds) * 1000 % 6000);
- return iTime;
- }
- public static float UnitsToSeconds(Units units)
- {
- float seconds = 0.0f;
- seconds += units.hours * 3600;
- seconds += units.minutes * 60;
- seconds += units.seconds;
- seconds += (float)units.deciSeconds * 0.1f;
- seconds += (float)(units.centiSeconds / 100);
- seconds += (float)(units.milliSeconds / 1000);
- return seconds;
- }
2.时间转换为字符串string:
- public static string TimeToString(float timeInSeconds, bool showHours, bool showMinutes, bool showSeconds,
- bool showTenths, bool showHundredths, bool showMilliSeconds,
- char delimiter = ':')
- {
- Units iTime = TimeToUnits(timeInSeconds);
- string hours = (iTime.hours < 10) ? "0" + iTime.hours.ToString() : iTime.hours.ToString();
- string minutes = (iTime.minutes < 10) ? "0" + iTime.minutes.ToString() : iTime.minutes.ToString();
- string seconds = (iTime.seconds < 10) ? "0" + iTime.seconds.ToString() : iTime.seconds.ToString();
- string deciSeconds = iTime.deciSeconds.ToString();
- string centiSeconds = (iTime.centiSeconds < 10) ? "0" + iTime.centiSeconds.ToString() : iTime.centiSeconds.ToString();
- string milliSeconds = (iTime.milliSeconds < 100) ? "0" + iTime.milliSeconds.ToString() : iTime.milliSeconds.ToString();
- milliSeconds = (iTime.milliSeconds < 10) ? "0" + milliSeconds : milliSeconds;
- return ((showHours ? hours : "") +
- (showMinutes ? delimiter + minutes : "") +
- (showSeconds ? delimiter + seconds : "") +
- (showTenths ? delimiter + deciSeconds : "") +
- (showHundredths ? delimiter + centiSeconds : "") +
- (showMilliSeconds ? delimiter + milliSeconds : "")).TrimStart(delimiter);
- }
3.时间对应时钟的转过的度数:
- private static float HoursToDegreesInternal(float hours, float minutes = 0.0f, float seconds = 0.0f)
- {
- return (hours * 30.0f) + (minutes * 0.5f) + (seconds * 0.0083333333f);
- }
- private static float MinutesToDegreesInternal(float minutes, float seconds = 0.0f, float milliSeconds = 0.0f)
- {
- return (minutes * 6.0f) + (seconds * 0.1f) + (milliSeconds * 0.0001f);
- }
- private static float SecondsToDegreesInternal(float seconds, float milliSeconds = 0.0f)
- {
- return (seconds * 6.0f) + (milliSeconds * 0.006f);
- }
- private static float MilliSecondsToDegreesInternal(float milliSeconds)
- {
- return milliSeconds * 0.36f;
- }
- 转载自:http.dsqiu.iteye.com