unity3D显示系统时间UI

一、实现目标:在游戏UI上显示系统时间

二、实现方法01:Canvas-TMP(TextMeshPro)方法:

The First Step:创建Tmp -- SystemTimerText01

        

The Second Step:创建C#脚本 -- SystemTimer

      

TheThird Step:编辑脚本

        

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using System;
using TMPro;

public class SystemTimer : MonoBehaviour
{
    public TextMeshProUGUI SystemTimerText01;
    //保存当前时间
    public DateTime CurrentTime;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //获取当前时间
        CurrentTime = DateTime.Now.ToLocalTime();
        //将所需时间格式赋给 SystemTimerText01.text
        SystemTimerText01.text = CurrentTime.Year+"/"+CurrentTime.Month+"/"
            +CurrentTime.Day+"("+CurrentTime.Hour+":"+
            CurrentTime.Minute+":"+CurrentTime.Second+")";
    }
}

三、结果测试

四、结果分析

        目前无法显示中文:

  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Unity3D中获取当前时间,可以使用C#的DateTime类。首先,您需要在脚本中导入System命名空间。然后,您可以使用DateTime.Now属性来获取当前日期和时间。下面是一个示例代码片段: using System; public class TimeManager : MonoBehaviour { private void Start() { string currentTime = GetCurrentTime(); Debug.Log("当前时间:" + currentTime); } private string GetCurrentTime() { DateTime dateTime = DateTime.Now; string strNowTime = string.Format("{0:D}{1:D}{2:D}{3:D}{4:D}{5:D}", dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second); return strNowTime; } } 在上述代码中,GetCurrentTime()方法返回一个格式化的字符串,包含年、月、日、小时、分钟和秒。您可以根据您的需求修改格式化字符串。然后,您可以在Start()方法中调用GetCurrentTime()方法***<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Unity3D获取系统当前时间](https://blog.csdn.net/qq_40544338/article/details/114806180)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Unity3D获取当前系统时间并在UI界面中显示](https://blog.csdn.net/weixin_46992165/article/details/126358648)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值