_xiaofang/xiaofang/Assets/Script/UI/PanelUI/CalendarPanel.cs

82 lines
2.3 KiB
C#
Raw Normal View History

2024-11-29 09:47:47 +08:00
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CalendarPanel : MonoBehaviour
{
[Header("<22><><EFBFBD><EFBFBD>")]
public Text monthText; // <20><>ǰ<EFBFBD>·<EFBFBD><C2B7><EFBFBD>ʾ<EFBFBD><CABE>Text
public Transform dateGrid; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ڵ<EFBFBD>GridLayoutGroup
public GameObject dayButtonPrefab; // <20><><EFBFBD>ڰ<EFBFBD>ť<EFBFBD><C5A5>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD>
public Button prevMonthBtn; // <20><>һ<EFBFBD><D2BB><EFBFBD>°<EFBFBD>ť
public Button nextMonthBtn; // <20><>һ<EFBFBD><D2BB><EFBFBD>°<EFBFBD>ť
public Text selectedDateText; // <20><>ʾѡ<CABE>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
public DateTime currentDate; // <20><>ǰ<EFBFBD><C7B0>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// Start is called before the first frame update
void Start()
{
currentDate = DateTime.Now; // <20><>ʼ<EFBFBD><CABC>ʾ<EFBFBD><CABE>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
UpdateCalendar();
//prevMonthBtn.onClick.AddListener(OnPrevMonth);
//nextMonthBtn.onClick.AddListener(OnNextMonth);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void UpdateCalendar()
{
// <20><><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD>ʾ
monthText.text = currentDate.ToString("yyyy<79><79>MM<4D><4D>");
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڰ<EFBFBD>ť֮ǰ<D6AE><C7B0><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD><EFBFBD>ڰ<EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
foreach (Transform child in dateGrid)
{
Destroy(child.gameObject); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5A3A8><EFBFBD>ڰ<EFBFBD>ť<EFBFBD><C5A5>
}
// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD>·ݵĵ<DDB5>һ<EFBFBD><D2BB>
DateTime firstDayOfMonth = new DateTime(currentDate.Year, currentDate.Month, 1);
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>·ݵ<C2B7><DDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int daysInMonth = DateTime.DaysInMonth(currentDate.Year, currentDate.Month);
// <20><>ȡ<EFBFBD><C8A1>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD>
int firstDayOfWeek = (int)firstDayOfMonth.DayOfWeek; // 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, 6<><36><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD>ɿյ<C9BF>ռλ<D5BC><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><E6BCB8>
for (int i = 0; i < firstDayOfWeek; i++)
{
GameObject emptyItem = Instantiate(dayButtonPrefab, dateGrid);
emptyItem.SetActive(false); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Щռλ<D5BC><CEBB>
}
// <20><><EFBFBD>ɵ<EFBFBD>ǰ<EFBFBD>·ݵ<C2B7>ÿһ<C3BF><D2BB>
for (int day = 1; day <= daysInMonth; day++)
{
GameObject dayItem = Instantiate(dayButtonPrefab, dateGrid);
Button dayButton = dayItem.GetComponent<Button>();
Text dayText = dayItem.GetComponentInChildren<Text>();
dayText.text = day.ToString();
}
}
// <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
public void OnPrevMonth()
{
currentDate = currentDate.AddMonths(-1);
UpdateCalendar();
}
// <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
public void OnNextMonth()
{
currentDate = currentDate.AddMonths(1);
UpdateCalendar();
}
}