Unity 获取系统当前时间,并格式化显示。通过“System.DateTime”获取系统当前的时间,然后通过格式化把获得的时间格式化显示出来,具体如下:

1、打开Unity,新建一个空工程,Unity界面如下图

2、在工程中新建一个脚本,可以命名为“CurrrentTimeTest”,选中“CurrrentTimeTest”,双击打开脚本。

3、在打开 的脚本上进行编辑,首先设置几个变量存取当前时间的时分秒,年月日,然后把取得到的时间进行格式化输出,具体如下图

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; public class CurrrentTimeTest : MonoBehaviour { private Text CurrrentTimeText;
private int hour;
private int minute;
private int second;
private int year;
private int month;
private int day; // Use this for initialization
void Start () {
CurrrentTimeText = GetComponent<Text>(); } // Update is called once per frame
void Update () {
//获取当前时间
hour = DateTime.Now.Hour;
minute = DateTime.Now.Minute;
second = DateTime.Now.Second;
year = DateTime.Now.Year;
month = DateTime.Now.Month;
day = DateTime.Now.Day; //格式化显示当前时间
CurrrentTimeText.text = string.Format("{0:D2}:{1:D2}:{2:D2} " + "{3:D4}/{4:D2}/{5:D2}", hour, minute, second, year, month, day); #if UNITY_EDITOR
Debug.Log("W now " + System.DateTime.Now); //当前时间(年月日时分秒)
Debug.Log("W utc " + System.DateTime.UtcNow); //当前时间(年月日时分秒)
#endif
}
}

4、脚本编译正确后,回到Unity界面,在场景中新建一个“Text”,适当调整好位置与大小,然后把“CurrentTimeTest”赋给“Text”,具体如下图

5、运行场景,即可以看到当前时间的显示,具体如下图

Unity3D获取系统当前时间,并格式化显示的更多相关文章

  1. java 获取系统当前时间并格式化

      java 获取系统当前时间并格式化 CreateTime--2018年5月9日11:41:00 Author:Marydon 实现方式有三种 updateTime--2018年7月23日09点32 ...

  2. 使用date类和format类对系统当前时间进行格式化显示

    一:Date------------String 代码1:(代码二对显示出来的时间格式进行优化) package DateDemo; import java.text.SimpleDateFormat ...

  3. Java基础 - Date的相关使用(获取系统当前时间)

    前言: 在日常Java开发中,常常会使用到Date的相关操作,如:获取当前系统时间.获取当前时间戳.时间戳按指定格式转换成时间等.以前用到的时候,大部分是去网上找,但事后又很快忘记.现为方便自己今后查 ...

  4. Oracle,MySQL,sqlserver三大数据库如何获取系统当前时间

    Oracle中如何获取系统当前时间:用SYSDATE() MySQL中获取系统当前时间主要有以下几点: (1)now()函数以('YYYY-MM-dd HH:mm:SS')返回当前的日期时间,可以直接 ...

  5. android service 样例(电话录音和获取系统当前时间)

    关于android service 的具体解释请參考: android四大组件--android service具体解释.以下将用两个实例具体呈现Android Service的两种实现. 一个是st ...

  6. java获取系统指定时间年月日

    java获取系统指定时间年月日 private String setDateTime(String falg) { Calendar c = Calendar.getInstance(); c.set ...

  7. 使用js时,如何获取系统当前时间并且得到格式为"yyyy年MM月"的日期

    1.使用js时,如何获取系统当前时间并且得到格式为"yyyy年MM月"的日期: 1 var newdate = new Date(); 2 var nowyear = newdat ...

  8. C++ 获取系统当前时间(日历时)

    获取系统当前时间(日历时) //Linux & C++11 #include <chrono> #include <ctime> using namespace std ...

  9. C# 获取系统开机时间

    原文:C# 获取系统开机时间 ///         ///  获取系统开机时间          ///         ///         private DateTime GetComput ...

随机推荐

  1. div左右居中css

    l_btn{ font-size: 1.2rem; width: 190px; height: 50px; border: 1px solid #fff; border-radius: 25px; c ...

  2. 给table加边框的样式

    <style> .tb { width: 1600px; text-align: center; border-collapse: collapse; } .tb tr td { bord ...

  3. 振兴中华(dfs or dp )

    小明参加了学校的趣味运动会,其中的一个项目是:跳格子. 地上画着一些格子,每个格子里写一个字,如下所示:(也可参见p1.jpg) 从我做起振 我做起振兴 做起振兴中 起振兴中华 比赛时,先站在左上角的 ...

  4. web开发中xml的内容

    文档声明(注:文档声明前不能有注释) XML中的元素/标签 注:xmlx中解析程序会将其中的空格与换行当做内容来解析,区分大小写 CDATA区域中的内容不解析

  5. nginx获取uri里面的参数

    add_header Content-Disposition "attachment;fileName=$arg_filename"; 请求连接为:10.26.1.165/abc? ...

  6. Changing Ethernet Media Speed for AIX

    ITS UNIX Systems Changing Ethernet Media Speed for AIX First you need to find out the device name of ...

  7. Codeforces 706C - Hard problem - [DP]

    题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...

  8. (在命名空间 public 中)存在冲突

    解决方案: http://www.wastedpotential.com/flash-as3-stop-using-automatically-declare-stage-instances/ 修复错 ...

  9. 17.0-uC/OS-III消息管理

    消息传递 有些情况下任务或ISR与另一个任务间进行通信,这种信息交换叫做作业间的通信. 可以有两种方法实现这种通信: 全局变量. 发送消息. 1.果使用全局变量,任务或ISR就须确保它独占该变量.如果 ...

  10. Python3学习之路~5.11 configparser模块

    用于生成和修改常见配置文档,当前模块的名称在 python 2.x 版本中为 ConfigParser, python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式 ...