提示:源码附在文后~大家互相学习
目录
前言
一、组件结构
二、使用步骤
1.脚本列表
2.绑定说明
1.外层作为总控制层
2.选择日期模块
总结
前言
刚开始使用Unity开发项目,目前工作需求以Unity2D开发为主!发现在以Unity开发的管理系统中,时常要用到日期选择的工具!所以归类提炼不同的实现方式。这款日期选择组件基本实现灵活选择、设置日期(年月日格式)!方便每次使用!
一、组件结构
二、使用步骤
1.脚本列表
2.绑定说明
1.外层作为总控制层
绑定DatePickerUI脚本,控制选择时间反显、反显格式以及时间清空功能。
注释:绑定位置为下图显示!
DatePickerUI核心代码
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.选择日期模块
绑定CalendarUI脚本,是实现日期列表加载、切换、选择功能。
注释:绑定位置为下图显示!
CalendarUI核心代码
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);
}
收获
通过在平台上整理组件,可以利用空余时间归类总结,更进一步梳理出简单的组件结构,巩固认识、加深理解!并在下次使用是快速使用,节约时间!同时可以记录分享!
总结
组件结构简单,层级明朗,便于理解。可更改ui和尺寸用于不同场合。个人总结归纳,便于使用。希望可以拿来直接使用,方便易懂,在开发过程中节约时间、避免重复造轮子~~~文章来源:https://www.toymoban.com/news/detail-724948.html
CSDN组件下载:https://download.csdn.net/download/u014641682/87563968文章来源地址https://www.toymoban.com/news/detail-724948.html
到了这里,关于Unity 日历插件组件-日期选择器2D(二)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!