Unity时钟定时器插件——Vision Timer源码分析之二
Unity时钟定时器插件——Vision Timer源码分析之二
By D.S.Qiu
尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com
前面的已经介绍了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; }
小结:
vp_TimeUtility确实简单,之前还以为计算角度会比较复杂,贴完代码发现我想多了。具体的代码可以下载Unity时钟定时器插件——Vision Timer源码分析之一的附件查看。
刚好2:00,晚上还有做点erlang的事情,只能等明天在写了,明天打算介绍Unity3D Managed Coroutines with Start, Stop, Resume——TaskManager。
如果您对D.S.Qiu有任何建议或意见可以在文章后面评论,或者发邮件(gd.s.qiu@gmail.com)交流,您的鼓励和支持是我前进的动力,希望能有更多更好的分享。
转载请在文首注明出处:http://dsqiu.iteye.com/blog/2020604
更多精彩请关注D.S.Qiu的博客和微博(ID:静水逐风)
上一篇: Unity3D单例类模板类的实现
下一篇: ai怎么设计扁平化的电脑键盘图标?