查看: 2397|回复: 0

UNITY在界面上显示当前时间的方法

[复制链接]

839

主题

887

帖子

3663

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3663

最佳新人

发表于 2018-11-15 09:25:55 | 显示全部楼层 |阅读模式
创建一个TEXT  UI

80_3_0f69b1c5f528994.jpg


将下面这段此代码拖放在相机上。

  1. using UnityEngine;
  2. using System;
  3. using System.Collectio自行去掉汉字ns;
  4. using UnityEngine.UI;

  5. public class Testdata : MonoBehaviour
  6. {
  7.     private Text CurrentGX;
  8.     private int hour;
  9.     private int minute;
  10.     private int second;
  11.     private int year;
  12.     private int month;
  13.     private int day;

  14.     void Start()
  15.     {
  16.         //CurrentGX = GetComponent<Text>();
  17.         CurrentGX = GameObject.Find("Canvas/Text").GetComponent<Text>();
  18.         //text.text = "WWW.GALAXIX.COM";
  19.     }
  20.     void Update()
  21.     {
  22.         hour = DateTime.Now.Hour;
  23.         minute = DateTime.Now.Minute;
  24.         second = DateTime.Now.Second;
  25.         year = DateTime.Now.Year;
  26.         month = DateTime.Now.Month;
  27.         day = DateTime.Now.Day;

  28.         CurrentGX.text = string.Format("{0:D2}:{1:D2}:{2:D2} " + "{3:D2}:{4:D2}:{5:D2}", hour, minute, second, year, month, day);

  29.     #if UNITY_EDITOR
  30.         Debug.Log("W now " + System.DateTime.Now);      //当前时间(年月日时分秒)
  31.         Debug.Log("W utc " + System.DateTime.UtcNow);   //当前时间(年月日时分秒)
  32.     #endif
  33.         /*
  34.         Debug.Log("W now  " + System.DateTime.Now);        //当前时间(年月日时分秒)  
  35.         Debug.Log("W utc  " + System.DateTime.UtcNow);     // 当前时间(年月日时分秒)  
  36.         Debug.Log("W year  " + System.DateTime.Now.Year);  //当前时间(年)  
  37.         Debug.Log("W month   " + System.DateTime.Now.Month); //当前时间(月)  
  38.         Debug.Log("W day   " + System.DateTime.Now.Day);    // 当前时间(日)  
  39.         Debug.Log("W h    " + System.DateTime.Now.Hour);  // 当前时间(时)  
  40.         Debug.Log("W min   " + System.DateTime.Now.Minute);  // 当前时间(分)  
  41.         Debug.Log("W second   " + System.DateTime.Now.Second); // 当前时间(秒)  
  42.         */
  43.     }
  44. }
复制代码


播放测试,就可以看到这里有动态的当前时间在刷新了。

80_3_036771775c68892.jpg

关注galaxixv,官方微信公众号。
GALAXIX UE4 讨论学习群 140439020
UE4精英学习群  12762592
UNITY学习群   194092348
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


快速回复 返回顶部 返回列表