目录
Unity 工具之 获取当前系统时间,并格式化显示的封装(并有简单的月星期am/pm转换)
一、简单介绍
二、实现原理
三、注意事项
四、效果预览
五、实现步骤
六、关键代码
七、System.DateTime.Now.ToString() 格式化字符相关说明
Unity 工具类,自己整理的一些游戏开发可能用到的模块,单独独立使用,方便游戏开发。
本节介绍,简单的获取系统时间,并且格式化显示,并处理一些时间显示,来符合使用要求。
1、调用 System.DateTime.Now ,获取系统时间
2、类似 System.DateTime.Now.ToString("hh:mm:ss tt") 形式格式化显示
1、格式上午 /下午 (AM/PM)不同设备,显示可能不一样(Win 可能显示上午/下午,Android 可能显示AM/PM,等)
1、打开Unity,新建一个空工程
2、在场景中布局UI,简单的显示一下获取到的 DateTime 数据
3、新建脚本,编辑脚本 GetDateTimeWrapper 获取系统时间,并且必要的格式话调整,便于接口调用,TestScript 测试 GetDateTimeWrapper 的接口,Singleton 单例类
4、把 TestScript 挂载到场景中,并对应赋值 UI
5、运行场景,效果如下
1、GetDateTimeWrapper
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class GetDateTimeWrapper : Singleton<GetDateTimeWrapper> { // 星期几的英文缩写 string[] Day = new string[]{ "Sun","Mon", "Tue", "Wed", "Thu","Fri","Sat" }; // 月份的英文缩写 string[] Month = new string[]{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" }; /// <summary> /// 获取当前时间 时 /// </summary> /// <returns></returns> public int GetCurrentHour() { return DateTime.Now.Hour; } /// <summary> /// 获取当前时间 分 /// </summary> /// <returns></returns> public int GetCurrentMinute() { return DateTime.Now.Minute; } /// <summary> /// 获取当前时间 时:分 am/pm /// </summary> /// <returns></returns> public string GetCurrentTime_hmt() { #if UNITY_EDITOR return DateTime.Now.ToString("tt").Equals("上午") ? DateTime.Now.ToString("hh:mm") + " am" : DateTime.Now.ToString("hh:mm") + " pm"; #else // 移动端 AM/PM 为大写,如需要转换小写的话,(也可以直接最后最后一次转为小写) return DateTime.Now.ToString("tt").ToLower().Equals("am")? DateTime.Now.ToString("hh:mm") + " am" : DateTime.Now.ToString("hh:mm") + " pm"; #endif } /// <summary> /// 获取当前时间 日 /// </summary> /// <returns></returns> public int GetCurrentDay() { return DateTime.Now.Day; } /// <summary> /// 获取当前时间 月 /// </summary> /// <returns></returns> public string GetCurrentMonth() { return Month[DateTime.Now.Month-1]; } /// <summary> /// 获取当前时间 星期 /// </summary> /// <returns></returns> public string GetCurrentDayOfWeek() { return Day[(int)(DateTime.Now.DayOfWeek)]; } /// <summary> /// 获取当前时间 星期 /// </summary> /// <returns></returns> public string GetCurrentDate_dMw() { return string.Format("{0} {1},{2}", GetCurrentDay(), GetCurrentMonth(),GetCurrentDayOfWeek()); } }
2、GetDateTimeWrapper
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class TestScript : MonoBehaviour { public Text Text0; public Text Text1; public Text Text2; // Start is called before the first frame update void Start() { Text0.text = GetDateTimeWrapper.Instance.GetCurrentTime_hmt(); Text1.text = GetDateTimeWrapper.Instance.GetCurrentDate_dMw(); Text2.text = System.DateTime.Now.ToString("hh:mm:ss tt"); Debug.Log(GetDateTimeWrapper.Instance.GetCurrentTime_hmt()); Debug.Log(GetDateTimeWrapper.Instance.GetCurrentDate_dMw()); Debug.Log(System.DateTime.Now.ToString("hh:mm:ss tt")); } }
3、Singleton
public abstract class Singleton<T> where T : class, new() { private static T instance = null; // 多线程安全机制 private static readonly object locker = new object(); public static T Instance { get { lock (locker) { if (instance == null) instance = new T(); return instance; } } } }
d
月中的某一天。一位数的日期没有前导零。
dd
月中的某一天。一位数的日期有一个前导零。
ddd
周中某天的缩写名称,在 AbbreviatedDayNames 中定义。
dddd
周中某天的完整名称,在 DayNames 中定义。
M
月份数字。一位数的月份没有前导零。
MM
月份数字。一位数的月份有一个前导零。
MMM
月份的缩写名称,在 AbbreviatedMonthNames 中定义。
MMMM
月份的完整名称,在 MonthNames 中定义。
y
不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。
yy
不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
yyyy
包括纪元的四位数的年份。
gg
时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
h
12小时制的小时。一位数的小时数没有前导零。
hh
12小时制的小时。一位数的小时数有前导零。
H
24小时制的小时。一位数的小时数没有前导零。
HH
24小时制的小时。一位数的小时数有前导零。
m
分钟,一位数的分钟数没有前导零。
mm
分钟,一位数的分钟数有一个前导零。
s
秒,一位数的秒数没有前导零。
ss
秒,一位数的秒数有一个前导零。
f
秒的小数精度为一位。其余数字被截断。
ff
秒的小数精度为两位。其余数字被截断。
fff
秒的小数精度为三位。其余数字被截断。
ffff
秒的小数精度为四位。其余数字被截断。
fffff
秒的小数精度为五位。其余数字被截断。
ffffff
秒的小数精度为六位。其余数字被截断。
fffffff
秒的小数精度为七位。其余数字被截断。
t
在AMDesignator或PMDesignator中定义的AM/PM指示项的第一个字符(如果存在)。
tt
在AMDesignator或PMDesignator中定义的AM/PM指示项(如果存在)。
z
时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。
zz
时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。
zzz
完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。
:
在TimeSeparator中定义的默认时间分隔符。
/
在DateSeparator中定义的默认日期分隔符。
%c
其中c是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。
c
其中c是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“\”。