DatePickerGroupNew.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /// <summary>
  6. /// 日期选择组
  7. /// </summary>
  8. public class DatePickerGroupNew : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// 最小日期和最大日期
  12. /// </summary>
  13. public DateTime _minDate, _maxDate;
  14. /// <summary>
  15. /// 选择的日期(年月日时分秒)
  16. /// </summary>
  17. public DateTime _selectDate;
  18. /// <summary>
  19. /// 时间选择器列表
  20. /// </summary>
  21. public List<DatePickerNew> _datePickerList;
  22. /// <summary>
  23. /// 当选择日期的委托事件
  24. /// </summary>
  25. public event OnDateUpdate _OnDateUpdate;
  26. public static DateTime _selectTime;
  27. [SerializeField] Button btnClose;
  28. [SerializeField] Button btnEnter;
  29. public Action<DatePickerGroupNew> onEnter;
  30. void Awake()
  31. {
  32. //设置最大最小日期
  33. _minDate = new DateTime(1950, 1, 1, 0, 0, 0);
  34. _maxDate = new DateTime(2060, 1, 1, 0, 0, 0);
  35. Init();
  36. if (btnClose)
  37. {
  38. btnClose.onClick.AddListener(() => {
  39. Destroy(transform.GetComponentInParent<Canvas>().gameObject);
  40. });
  41. }
  42. if (btnEnter)
  43. {
  44. btnEnter.onClick.AddListener(() => {
  45. Destroy(transform.GetComponentInParent<Canvas>().gameObject);
  46. onEnter?.Invoke(this);
  47. });
  48. }
  49. }
  50. private void Update()
  51. {
  52. }
  53. public void Init(DateTime dt)
  54. {
  55. _selectDate = dt;
  56. for (int i = 0; i < _datePickerList.Count; i++)
  57. {
  58. _datePickerList[i].myGroup = this;
  59. _datePickerList[i].Init();
  60. _datePickerList[i]._onDateUpdate += onDateUpdate;
  61. }
  62. }
  63. public void Init()
  64. {
  65. _selectDate = new DateTime(1995, 1, 1, 0, 0, 0); //DateTime.Now;
  66. for (int i = 0; i < _datePickerList.Count; i++)
  67. {
  68. _datePickerList[i].myGroup = this;
  69. _datePickerList[i].Init();
  70. _datePickerList[i]._onDateUpdate += onDateUpdate;
  71. }
  72. int languageID = PlayerPrefs.GetInt("Language", 0);
  73. string title = languageID == 0 ? "日期选择" : "Date Select";
  74. string title0 = languageID == 0 ? "年" : "Year";
  75. string title1 = languageID == 0 ? "月" : "Month";
  76. string title2 = languageID == 0 ? "日" : "Day";
  77. transform.Find("TopBar/Title").GetComponent<Text>().text = title;
  78. transform.Find("ViewPort/DatePicker_Year/year").GetComponentInChildren<Text>().text = title0;
  79. transform.Find("ViewPort/DatePicker_Month/month").GetComponentInChildren<Text>().text = title1;
  80. transform.Find("ViewPort/DatePicker_Day/day").GetComponentInChildren<Text>().text = title2;
  81. }
  82. /// <summary>
  83. /// 当选择的日期更新
  84. /// </summary>
  85. public void onDateUpdate()
  86. {
  87. // Debug.Log("当前选择日期:" + _selectDate.ToString("yyyy年MM月dd日 HH : mm : ss"));
  88. //将选中的时间给_selectTime ,供其他界面调用
  89. _selectTime = _selectDate;
  90. for (int i = 0; i < _datePickerList.Count; i++)
  91. {
  92. _datePickerList[i].RefreshDateList();
  93. }
  94. }
  95. public string GetSelectDateStr()
  96. {
  97. _selectTime = _selectDate;
  98. return _selectTime.ToString("yyyy-MM-dd");
  99. }
  100. }