138 lines
3.4 KiB
C#
138 lines
3.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
using UnityEngine.UI;
|
|
using System;
|
|
|
|
public class Calendar : MonoBehaviour
|
|
{
|
|
[SerializeField] private TextMeshProUGUI monthText;
|
|
[SerializeField] private Transform daysParent;
|
|
[SerializeField] private GameObject dayPrefab;
|
|
|
|
private DateTime _currentDate;
|
|
|
|
private List<GameObject> _currentMonthButtons =
|
|
new List<GameObject>();
|
|
|
|
private int _lastMonth = -1;
|
|
private int _lastYear = -1;
|
|
|
|
public void SetDate(DateTime newDate)
|
|
{
|
|
_currentDate = newDate;
|
|
|
|
if (_currentDate.Month != _lastMonth ||
|
|
_currentDate.Year != _lastYear)
|
|
{
|
|
_lastMonth = _currentDate.Month;
|
|
_lastYear = _currentDate.Year;
|
|
|
|
GenerateCalendar();
|
|
}
|
|
else
|
|
{
|
|
UpdateVisual();
|
|
}
|
|
}
|
|
|
|
private void GenerateCalendar()
|
|
{
|
|
for (int i = daysParent.childCount - 1; i >= 0; i--)
|
|
{
|
|
Destroy(daysParent.GetChild(i).gameObject);
|
|
}
|
|
|
|
_currentMonthButtons.Clear();
|
|
|
|
monthText.text = _currentDate.ToString("MMMM yyyy");
|
|
|
|
DateTime firstDay =
|
|
new DateTime(_currentDate.Year,
|
|
_currentDate.Month,
|
|
1);
|
|
|
|
int daysInMonth =
|
|
DateTime.DaysInMonth(_currentDate.Year,
|
|
_currentDate.Month);
|
|
|
|
int startDayOfWeek =
|
|
((int)firstDay.DayOfWeek + 6) % 7;
|
|
|
|
DateTime prevMonth =
|
|
_currentDate.AddMonths(-1);
|
|
|
|
int daysInPrevMonth =
|
|
DateTime.DaysInMonth(prevMonth.Year,
|
|
prevMonth.Month);
|
|
|
|
for (int i = startDayOfWeek; i > 0; i--)
|
|
{
|
|
int dayNumber =
|
|
daysInPrevMonth - i + 1;
|
|
|
|
GameObject dayObj =
|
|
Instantiate(dayPrefab, daysParent);
|
|
|
|
dayObj.GetComponentInChildren<TextMeshProUGUI>()
|
|
.text = dayNumber.ToString();
|
|
|
|
Image img =
|
|
dayObj.GetComponent<Image>();
|
|
|
|
img.color = Color.gray;
|
|
}
|
|
|
|
for (int day = 1; day <= daysInMonth; day++)
|
|
{
|
|
GameObject dayObj =
|
|
Instantiate(dayPrefab, daysParent);
|
|
|
|
dayObj.GetComponentInChildren<TextMeshProUGUI>()
|
|
.text = day.ToString();
|
|
|
|
_currentMonthButtons.Add(dayObj);
|
|
}
|
|
|
|
UpdateVisual();
|
|
}
|
|
|
|
private void UpdateVisual()
|
|
{
|
|
int today = _currentDate.Day;
|
|
|
|
for (int i = 0; i < _currentMonthButtons.Count; i++)
|
|
{
|
|
Image img =
|
|
_currentMonthButtons[i]
|
|
.GetComponent<Image>();
|
|
|
|
Transform marker =
|
|
_currentMonthButtons[i]
|
|
.transform.Find("CurrentData");
|
|
|
|
int dayNumber = i + 1;
|
|
|
|
if (dayNumber < today)
|
|
{
|
|
img.color = Color.gray;
|
|
if (marker != null)
|
|
marker.gameObject.SetActive(false);
|
|
}
|
|
else if (dayNumber == today)
|
|
{
|
|
img.color = Color.white;
|
|
if (marker != null)
|
|
marker.gameObject.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
img.color = Color.white;
|
|
if (marker != null)
|
|
marker.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
}
|