SelectionTutorial.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Bitsplash.DatePicker;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. namespace Bitsplash.DatePicker.Tutorials
  8. {
  9. public class SelectionTutorial : MonoBehaviour
  10. {
  11. public DatePickerSettings DatePicker;
  12. public Text InfoText;
  13. // Start is called before the first frame update
  14. void Start()
  15. {
  16. if(DatePicker != null)
  17. {
  18. // handle selection change using a unity event
  19. DatePicker.Content.OnSelectionChanged.AddListener(OnSelectionChanged);
  20. DatePicker.Content.OnDisplayChanged.AddListener(OnDisplayChanged);
  21. ShowAllSelectedDates();// show all the selected days in the begining
  22. DatePicker.Content.SetMarkerColor(DateTime.Now, Color.red);
  23. }
  24. }
  25. public void OnDisplayChanged()
  26. {
  27. var cell = DatePicker.Content.GetCellObjectByDate(DateTime.Now);
  28. if (cell != null)
  29. {
  30. cell.CellEnabled = false;
  31. }
  32. }
  33. public void SelectSingleDate()
  34. {
  35. if(DatePicker != null)
  36. {
  37. // this method clears the selection and selects the specified date
  38. DatePicker.Content.Selection.SelectOne(DateTime.Today);
  39. }
  40. }
  41. public void SelectDateRange()
  42. {
  43. if (DatePicker != null)
  44. {
  45. // this method clears the selection ans selects a spcified range
  46. DatePicker.Content.Selection.SelectRange(DateTime.Today, DateTime.Today + TimeSpan.FromDays(5));
  47. }
  48. }
  49. void ShowAllSelectedDates()
  50. {
  51. if(InfoText != null)
  52. {
  53. string text = "";
  54. var selection = DatePicker.Content.Selection;
  55. for (int i=0; i< selection.Count; i++)
  56. {
  57. var date = selection.GetItem(i);
  58. text += "\r\n" + date.ToShortDateString();
  59. }
  60. InfoText.text = text;
  61. }
  62. }
  63. void OnSelectionChanged()
  64. {
  65. ShowAllSelectedDates();
  66. }
  67. // Update is called once per frame
  68. void Update()
  69. {
  70. }
  71. }
  72. }