close

Unity 讀取年月日,並顯並不是難事,只要使用 System.DateTime 函式庫就可以了。

顯示設定當下月份天數,比較少用到這個功能,除非班表功能(讓使用者)才會使用的上。

那我們趕快進入程式碼部分吧

這段程式:  resultDisPlayer(int year, int month, int day) 為了方便使用者開發,

可以透過UI介面或者Unity GUI (過去式介面) 來進行溝通。

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

public class TestScript : MonoBehaviour
{
    DateTime startdate;
  
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(resultDisPlayer(2021, 5, 1));        
    }
  
    string resultDisPlayer(int year, int month, int day){
  
        string result = "";
  
        startdate = new DateTime(year, month, day);
  
        for (int x = day; x <= DateTime.DaysInMonth(startdate.Year, startdate.Month); x++)
        {
 
            startdate = new DateTime(startdate.Year, startdate.Month, x);
  
            string str = "民國: " + vidToRepublic(startdate.Year) + "年 " 
                + startdate.Month + "月 " + x + "日 " + "星期: " + weekEngToChin(startdate.DayOfWeek.ToString());
  
            result += str + "\n";
        }
  
        return result;
  
    }
  
    // 西元轉換民國 (公式)
    string vidToRepublic(int dayofYear)
    {
        int vidNumber = 1911; // 公式
  
        int sum = dayofYear - vidNumber;
  
        return sum.ToString();           
    }
  
    // 英文轉中文(星期)
    string weekEngToChin(string dayOfWeek)
    {
        string chinWeek = "";
  
        if (dayOfWeek.ToString().Equals("Monday"))
            chinWeek = "星期一";
        if (dayOfWeek.ToString().Equals("Tuesday"))
            chinWeek = "星期二";
        if (dayOfWeek.ToString().Equals("Wednesday"))
            chinWeek = "星期三";
        if (dayOfWeek.ToString().Equals("Thursday"))
            chinWeek = "星期四";
        if (dayOfWeek.ToString().Equals("Friday"))
            chinWeek = "星期五";
        if (dayOfWeek.ToString().Equals("Saturday"))
            chinWeek = "星期六";
        if (dayOfWeek.ToString().Equals("Sunday"))
            chinWeek = "星期日";
  
        return chinWeek;
    }
}

顯示結果如下:

image

 

arrow
arrow
    文章標籤
    unity教學
    全站熱搜

    Xauxas 發表在 痞客邦 留言(0) 人氣()