天津23维预案
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

216 lines
5.2 KiB

using UnityEngine;
using System;
using UnityEngine.UI;
public class DateTimeControl : MonoBehaviour
{
// Use this for initialization
public int year;
public int month;
public int day;
public InputField hours;
public InputField minutes;
public InputField seconds;
public Dropdown years;
public Dropdown months;
public GameObject dayPanel;
public GameObject timePanel;
private Text dataTime;
private bool time;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void setDateTime(bool time, Text dataTime)
{
setDateTime(DateTime.Now,time, dataTime);
}
//将日期时间映射到UI
public void setDateTime(DateTime dt,bool time, Text dataTime)
{
this.dataTime = dataTime;
year = dt.Year;
var yearOptionIndex = years.options.FindIndex((option) => option.text == year + "年");
years.value = yearOptionIndex;
month = dt.Month;
var monthOptionIndex = months.options.FindIndex((option) => option.text == month + "月");
months.value = monthOptionIndex;
day = dt.Day;
setDayUI(year, month, day);
var hour = dt.Hour;
hours.text = timeSetFormat(hour);
var minute = dt.Minute;
minutes.text = timeSetFormat(minute);
var secord = dt.Second;
seconds.text = timeSetFormat(secord);
this.time = time;
if (time)
{
timePanel.SetActive(true);
}
else
{
timePanel.SetActive(false);
}
}
private void setDayUI(int year, int month, int day)
{
var daysCount = DateTime.DaysInMonth(year, month);
if (daysCount< day)
{
day = daysCount;
this.day = day;
}
var dt = new DateTime(year, month, 1);
int weekDay = dt.DayOfWeek.GetHashCode();
foreach (Transform item in dayPanel.transform)
{
//Destroy(child.gameObject);
var nameIndex = int.Parse(item.name);
if (nameIndex > weekDay && nameIndex <= weekDay + daysCount)
{
item.GetComponent<DayMessage>().setActive(true);
item.GetComponent<DayMessage>().setDay(nameIndex - weekDay);
if (day == nameIndex - weekDay)
{
item.GetComponent<Toggle>().isOn = true;
}
else
{
item.GetComponent<Toggle>().isOn = false;
}
}
else
{
item.GetComponent<DayMessage>().setActive(false);
}
}
}
public void dayChanged(int day)
{
this.day = day;
}
public void monthChanged(int month)
{
this.month = month;
setDayUI(year, month, day);
}
public void yearChanged(int year)
{
this.year = year;
setDayUI(year, month, day);
}
public void toLastYear()
{
if (years.value == 0)
return;
years.value--;
}
public void toNextYear()
{
if (years.value == years.options.Count-1)
return;
years.value++;
}
public void toLastMonth()
{
if (months.value == 0)
return;
months.value--;
}
public void toNextMonth()
{
if (months.value == months.options.Count - 1)
return;
months.value++;
}
private string timeSetFormat(int time)
{
if (time < 10)
{
return "0" + time;
}
else
{
return time.ToString();
}
}
public void hourFormat()
{
var hour=int.Parse(hours.text);
if (hour < 0)
{
hours.text = "00";
}
else if (hour >= 0 && hour < 10)
{
hours.text = "0" + hour;
}
else if (hour > 23)
{
hours.text = "23";
}
}
public void minuteFormat()
{
var minute = int.Parse(minutes.text);
if (minute < 0)
{
minutes.text = "00";
}
else if (minute >= 0 && minute < 10)
{
minutes.text = "0" + minute;
}
else if (minute > 59)
{
minutes.text = "59";
}
}
public void secondFormat()
{
var second = int.Parse(seconds.text);
if (second < 0)
{
seconds.text = "00";
}
else if (second >= 0 && second < 10)
{
seconds.text = "0" + second;
}
else if (second > 59)
{
seconds.text = "59";
}
}
public void submit()
{
var hour = int.Parse(hours.text);
var minute = int.Parse(minutes.text);
var second = int.Parse(seconds.text);
DateTime dt = new DateTime(year, month, day, hour, minute, second);
if (time)
{
dataTime.text = dt.ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
dataTime.text = dt.ToString("yyyy-MM-dd");
}
gameObject.SetActive(false);
}
}