[csharp] Calendar
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- using System;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- public class Calendar : MonoBehaviour
- {
- [Header("Start elements")]
- [SerializeField] private TMP_Text startMonthText;
- [SerializeField] private TMP_Text startDayText;
- [SerializeField] private TMP_Text startYearText;
- [SerializeField] private Button startMonthUp;
- [SerializeField] private Button startMonthDown;
- [SerializeField] private Button startDayUp;
- [SerializeField] private Button startDayDown;
- [Header("End elements")]
- [SerializeField] private TMP_Text endMonthText;
- [SerializeField] private TMP_Text endDayText;
- [SerializeField] private TMP_Text endYearText;
- [SerializeField] private Button endMonthUp;
- [SerializeField] private Button endMonthDown;
- [SerializeField] private Button endDayUp;
- [SerializeField] private Button endDayDown;
- private DateTime startDate;
- private DateTime endDate;
- private DateTime currentDate;
- private void Start()
- {
- currentDate = DateTime.Now;
- startDate = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day);
- endDate = startDate.AddDays(1);
- InitializeUI();
- }
- private void InitializeUI()
- {
- startMonthUp.onClick.AddListener(() => AdjustStartDate(month: 1));
- startMonthDown.onClick.AddListener(() => AdjustStartDate(month: -1));
- startDayUp.onClick.AddListener(() => AdjustStartDate(day: 1));
- startDayDown.onClick.AddListener(() => AdjustStartDate(day: -1));
- endMonthUp.onClick.AddListener(() => AdjustEndDate(month: 1));
- endMonthDown.onClick.AddListener(() => AdjustEndDate(month: -1));
- endDayUp.onClick.AddListener(() => AdjustEndDate(day: 1));
- endDayDown.onClick.AddListener(() => AdjustEndDate(day: -1));
- UpdateStartUI();
- UpdateEndUI();
- }
- private void AdjustStartDate(int month = 0, int day = 0)
- {
- startDate = startDate.AddMonths(month).AddDays(day);
- ValidateDate(ref startDate);
- UpdateStartUI();
- ValidateEndDate();
- }
- private void AdjustEndDate(int month = 0, int day = 0)
- {
- endDate = endDate.AddMonths(month).AddDays(day);
- ValidateDate(ref endDate);
- UpdateEndUI();
- }
- private void ValidateDate(ref DateTime date)
- {
- if (date < currentDate)
- {
- date = currentDate;
- }
- // Ensure the date is not beyond a reasonable future date(I put 5 but you can change it to whatever you like)
- else if (date > currentDate.AddYears(5))
- {
- date = currentDate.AddYears(5);
- }
- }
- private void UpdateStartUI()
- {
- startMonthText.text = startDate.Month.ToString();
- startDayText.text = startDate.Day.ToString();
- startYearText.text = startDate.Year.ToString();
- }
- private void UpdateEndUI()
- {
- endMonthText.text = endDate.Month.ToString();
- endDayText.text = endDate.Day.ToString();
- endYearText.text = endDate.Year.ToString();
- }
- private void ValidateEndDate()
- {
- if (endDate < startDate)
- {
- endDate = startDate.AddDays(1);
- UpdateEndUI();
- }
- }
- }
Editor
You can edit this paste and save as new: