Unity calendar plug-in component - date picker 2D (2)

Reminder: The source code is attached to the post~ Everyone can learn from each other

Table of contents

foreword

1. Component structure

2. Use steps

1. Script List

2. Binding instructions

1. The outer layer is used as the general control layer

2. Select date module

Summarize


foreword

刚开始使用Unity开发项目,目前工作需求以Unity2D开发为主!发现在以Unity开发的管理系统中,时常要用到日期选择的工具!所以归类提炼不同的实现方式。这款日期选择组件基本实现灵活选择、设置日期(年月日格式)!方便每次使用!


1. Component structure

2. Use steps

1. Script List

2. Binding instructions

1. The outer layer is used as the general control layer

Bind the DatePickerUI script to control the selection time reverse display, reverse display format and time clearing function.

Note: The binding position is shown in the figure below!

DatePickerUI core code


        protected override void Awake()
        {
            //info_tex = this.transform.Find("info_tex").GetComponent<Text>();
            _dateText = this.transform.Find("startDate_tex").GetComponent<Text>();
            _enddateText = this.transform.Find("endDate_tex").GetComponent<Text>();
            _calendar = this.transform.Find("calendarUIper").GetComponent<CalendarUI>();
            _endcalendar = this.transform.Find("endcalendarUIper").GetComponent<CalendarUI>();
            transform.Find("startCalendar_but").GetComponent<Button>().onClick.AddListener(() =>
            {
                //info_tex.text = "";
                _calendar.gameObject.SetActive(true);
                _endcalendar.gameObject.SetActive(false);
                _calendar.onDayClick.AddListener(dateTime => { DateTime = dateTime; });
                transform.Find("calendarUIper/title/clear_but").GetComponent<Button>().onClick.AddListener(()=> { refreshDateText("1"); });
            });
            transform.Find("endCalendar_but").GetComponent<Button>().onClick.AddListener(() =>
            {
                //info_tex.text = "";
                _endcalendar.gameObject.SetActive(true);             
                _calendar.gameObject.SetActive(false);
                _endcalendar.onDayClick.AddListener(dateTime => { EndDateTime = dateTime; });
                transform.Find("endcalendarUIper/title/clear_but").GetComponent<Button>().onClick.AddListener(() => { refreshEndDateText("1"); });
            }); 
        }

        public void refreshDateText(String i)
        {
            if ("".Equals(i))
            {
                switch (_calendar.CalendarType)
                {
                    case E_CalendarType.Day:
                        _dateText.text = DateTime.Year + "-" + DateTime.Month + "-" + DateTime.Day;
                        break;
                    case E_CalendarType.Month:
                        _dateText.text = DateTime.Year + "-" + DateTime.Month;
                        break;
                    case E_CalendarType.Year:
                        _dateText.text = DateTime.Year + "";
                        break;
                }
            }
            else {
                _dateText.text = "";
            }    
            _calendar.gameObject.SetActive(false);
        }

        public void refreshEndDateText(String i)
        {
            if ("".Equals(i))
            {
                switch (_endcalendar.CalendarType)
                {
                    case E_CalendarType.Day:
                        _enddateText.text = EndDateTime.Year + "-" + EndDateTime.Month + "-" + EndDateTime.Day;
                        break;
                    case E_CalendarType.Month:
                        _enddateText.text = EndDateTime.Year + "-" + EndDateTime.Month;
                        break;
                    case E_CalendarType.Year:
                        _enddateText.text = EndDateTime.Year + "";
                        break;
                }
            }
            else {
                _enddateText.text = "";
            }
            _endcalendar.gameObject.SetActive(false);
        }

2. Select date module

Binding the CalendarUI script is to realize the functions of loading, switching and selecting the date list.

Note: The binding position is shown in the figure below!

CalendarUI core code

 private void OnTimeButtonClick()
        {
            if (CalendarType == E_CalendarType.Month)
                CalendarType = E_CalendarType.Year;
            if (CalendarType == E_CalendarType.Day)
            {
                CalendarType = E_CalendarType.Month;
                calendarTypeChange(false);
            }
            Refresh();
        }
 private void OnNextButtonClick()
        {
            if (CalendarType == E_CalendarType.Day)
                m_selectDT = m_selectDT.AddMonths(1);
            else if (CalendarType == E_CalendarType.Month)
                m_selectDT = m_selectDT.AddYears(1);
            else
                m_selectDT = m_selectDT.AddYears(12);
            Refresh();
        }
        private void OnLastButtonClick()
        {
            if (CalendarType == E_CalendarType.Day)
                m_selectDT = m_selectDT.AddMonths(-1);
            else if (CalendarType == E_CalendarType.Month)
                m_selectDT = m_selectDT.AddYears(-1);
            else
                m_selectDT = m_selectDT.AddYears(-12);
            Refresh();
        }
  private void WeekGenerator(GameObject weekPrefab, Transform parent)
        {
            for (int i = 0; i < 7; i++)
            {
                GameObject week = prefabGenerator(weekPrefab, parent);
                week.GetComponent<Text>().text = getWeekName(i.ToString());
            }
            Destroy(weekPrefab);
        }
        private void DayGenerator(GameObject dayPrefab, Transform parent)
        {
            for (int i = 0; i < 42; i++)
            {
                GameObject day = prefabGenerator(dayPrefab, parent);
                DMY dmy = day.AddComponent<DMY>();
                day.GetComponent<Button>().onClick.AddListener(() =>
                {
                    m_selectDT = dmy.DateTime;
                    onDayClick.Invoke(dmy.DateTime);
                    Refresh();
                });
                _daysPool.Add(dmy);
            }
            Destroy(dayPrefab);
        }
        private void MonthGenerator(GameObject monthPrefab, Transform parent)
        {
            for (int i = 0; i < 12; i++)
            {
                GameObject month = prefabGenerator(monthPrefab, parent);
                DMY dmy = month.AddComponent<DMY>();
                month.GetComponent<Button>().onClick.AddListener(() =>
                {
                    m_selectDT = dmy.DateTime;
                    if (CalendarType == E_CalendarType.Month)
                    {
                        CalendarType = E_CalendarType.Day;
                        calendarTypeChange(true);
                        onMonthClick.Invoke(dmy.DateTime);
                    }
                    if (CalendarType == E_CalendarType.Year)
                    {
                        CalendarType = E_CalendarType.Month;
                        onYearClick.Invoke(dmy.DateTime);
                    }
                    Refresh();
                });
                _monthYearPool.Add(dmy);
            }
            Destroy(monthPrefab);
        }

reward

By arranging the components on the platform, you can use your spare time to classify and summarize, further sort out the simple component structure, consolidate your knowledge and deepen your understanding! And the next use is quick to use, saving time! At the same time, you can record and share!

Summarize

The component structure is simple, the hierarchy is clear, and it is easy to understand. The ui and size can be changed for different occasions. Personal summary and induction, easy to use. I hope it can be used directly, it is convenient and easy to understand, save time in the development process, and avoid reinventing the wheel~~~

CSDN component download: https://download.csdn.net/download/u014641682/87563968

Guess you like

Origin blog.csdn.net/u014641682/article/details/129473043