Unity Calendar 日历功能

这篇具有很好参考价值的文章主要介绍了Unity Calendar 日历功能。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


简介

工具已上传至SKFramework框架PackageManager

unity日历,Unity 开发日志,Unity,SKFramework,Calendar,日历,DateTime

框架开源地址:SKFramework

unity日历,Unity 开发日志,Unity,SKFramework,Calendar,日历,DateTime

参数说明

unity日历,Unity 开发日志,Unity,SKFramework,Calendar,日历,DateTime

  • OnlyCurrentMonthDays:是否只显示当前月份的日期项;

unity日历,Unity 开发日志,Unity,SKFramework,Calendar,日历,DateTime

参数为false时表示会填充当前月份1号之前的日期和当前月份最后一天之后的日期,这些日期不可交互。

  • HistoryDaysInteractable:历史日期是否可以交互;

unity日历,Unity 开发日志,Unity,SKFramework,Calendar,日历,DateTime

参数为false时表示当前日期之前的日期,即历史日期不可交互。(例如当前为1月11号,那么1月10号则不可以被选中)

事件说明

  • OnEndEditDateTime参数类型事件,当结束编辑时执行(确认选择)

unity日历,Unity 开发日志,Unity,SKFramework,Calendar,日历,DateTime文章来源地址https://www.toymoban.com/news/detail-535111.html

using System;
using UnityEngine;

public class Example : MonoBehaviour
{
    public void OnSelectedTime(DateTime dateTime)
    {
        Debug.Log(string.Format(">>>> {0}", dateTime));
    }
}

源码

using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;

namespace SK.Framework
{
    /// <summary>
    /// 日历工具
    /// </summary>
    public class Calendar : MonoBehaviour
    {
        //日期预制体
        [SerializeField] private GameObject dayPrefab;
        //小时预制体
        [SerializeField] private GameObject hourPrefab;
        //分钟预制体
        [SerializeField] private GameObject minutePrefab;

        //年份文本
        [SerializeField] private Text yearText;
        //月份文本
        [SerializeField] private Text monthText;
        //小时:分钟 文本
        [SerializeField] private Text timeText;

        [Tooltip("仅显示本月份的日期项")]
        [SerializeField] private bool onlyCurrentMonthDays = true;
        [Tooltip("历史日期是否可交互")]
        [SerializeField] private bool historyDaysInteractable = false;

        private int currentYear;
        private int currentMonth;
        private int currentDay;
        private int currentHour;
        private int currentMinute;

        /// <summary>
        /// 当前选中的时间
        /// </summary>
        public DateTime CurrentSelectedTime
        {
            get
            {
                return new DateTime(currentYear, currentMonth, currentDay, currentHour, currentMinute, 0);
            }
        }
        /// <summary>
        /// 编辑结束事件
        /// </summary>
        public UnityEvent<DateTime> onEndEdit;

        private void Start()
        {
            DateTime now = DateTime.Now;
            Init(now.Year, now.Month, now.Day, now.Hour, now.Minute);
            SelectTime(now);
        }

        //初始化日历
        private void Init(int year, int month, int day, int hour, int minute)
        {
            //首先清空
            Clear();
            //记录当前时间
            currentYear = year;
            currentMonth = month;
            currentDay = day;
            currentHour = hour;
            currentMinute = minute;

            //填充年份
            yearText.text = string.Format("{0}年", currentYear);
            //填充月份
            monthText.text = string.Format("{0}月", currentMonth);
            //获取该月份的天数
            int daysCount = DateTime.DaysInMonth(currentYear, currentMonth);
            //获取该月份一号是星期几
            int weekOfFirstDay = (int)new DateTime(currentYear, currentMonth, 1).DayOfWeek;
            //获取该月份最后一天是星期几
            int weekOfEndDay = (int)new DateTime(currentYear, currentMonth, daysCount).DayOfWeek;
            //上个月份
            DateTime prevMonth = new DateTime(currentYear, currentMonth, 1).AddMonths(-1);
            //上个月份的天数
            int prevMonthDaysCount = DateTime.DaysInMonth(prevMonth.Year, prevMonth.Month);
            //一号之前的占位
            for (int i = weekOfFirstDay - 1; i >= 0; i--)
            {
                CreateDayItem(onlyCurrentMonthDays ? 0 : (prevMonthDaysCount - i), false);
            }
            //实例化该月份的所有日期项
            for (int i = 0; i < daysCount; i++)
            {
                CreateDayItem(i + 1, historyDaysInteractable ? true :(new DateTime(currentYear,currentMonth, i + 1) - 
                    new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)).TotalDays >= 0);
            }
            //最后一天之后的占位
            for (int i = 0; i < 6 - weekOfEndDay; i++)
            {
                CreateDayItem(onlyCurrentMonthDays ? 0 : (i + 1), false);
            }
            //实例化24小时项
            for (int i = 0; i < 24; i++)
            {
                CreateHourItem(i);
            }
            //实例化60分钟项
            for (int i = 0; i < 60; i++)
            {
                CreateMinuteItem(i);
            }
        }
        //创建日期项
        private void CreateDayItem(int day, bool interactable)
        {
            //实例化
            var instance = Instantiate(dayPrefab);
            //设置父级
            instance.transform.SetParent(dayPrefab.transform.parent, false);
            //显示物体
            instance.SetActive(true);
            //名称设为日期
            instance.name = day.ToString();
            //大于0正常显示日期
            if (day > 0)
            {
                //将日期设置到文本中
                instance.GetComponentInChildren<Text>().text = day.ToString();
                //获取Toggle组件
                Toggle toggle = instance.GetComponent<Toggle>();
                //是否可交互
                toggle.interactable = interactable;
                //不可交互时文本颜色置灰
                toggle.GetComponentInChildren<Text>().color = interactable ? Color.white : Color.grey;
                //绑定事件
                toggle.onValueChanged.AddListener(isOn =>
                {
                    if (isOn)
                    {
                        currentDay = day;
                    }
                });
            }
            //占位
            else
            {
                //销毁Toggle、Image组件
                Destroy(instance.GetComponent<Toggle>());
                Destroy(instance.GetComponent<Image>());
                //销毁两个子物体
                for (int i = 0; i < 2; i++)
                {
                    Destroy(instance.transform.GetChild(i).gameObject);
                }
            }
        }
        //创建小时项
        private void CreateHourItem(int hour)
        {
            //实例化
            var instance = Instantiate(hourPrefab);
            //设置父级
            instance.transform.SetParent(hourPrefab.transform.parent, false);
            //显示物体
            instance.SetActive(true);
            //小时格式化为00格式
            string hourString = string.Format("{0:D2}", hour);
            //名称设为小时
            instance.name = hourString;
            //将小时设置到文本中
            instance.GetComponentInChildren<Text>().text = hourString;
            //获取Toggle组件
            Toggle toggle = instance.GetComponent<Toggle>();
            //绑定事件
            toggle.onValueChanged.AddListener(isOn =>
            {
                if (isOn)
                {
                    currentHour = hour;
                    timeText.text = string.Format("{0:D2}:{1:D2}", currentHour, currentMinute);
                }
            });
        }
        //创建分钟项
        private void CreateMinuteItem(int minute)
        {
            //实例化
            var instance = Instantiate(minutePrefab);
            //设置父级
            instance.transform.SetParent(minutePrefab.transform.parent, false);
            //显示物体
            instance.SetActive(true);
            //分钟格式化为00格式
            string minuteString = string.Format("{0:D2}", minute);
            //名称设为分钟
            instance.name = minuteString;
            //将分钟设置到文本中
            instance.GetComponentInChildren<Text>().text = minuteString;
            //获取Toggle组件
            Toggle toggle = instance.GetComponent<Toggle>();
            //绑定事件
            toggle.onValueChanged.AddListener(isOn =>
            {
                if (isOn)
                {
                    currentMinute = minute;
                    timeText.text = string.Format("{0:D2}:{1:D2}", currentHour, currentMinute);
                }
            });
        }
        //清除日历内容
        private void Clear()
        {
            //清空日期项
            Transform day = dayPrefab.transform.parent;
            if (day.childCount > 1)
            {
                for (int i = 1; i < day.childCount; i++)
                {
                    Destroy(day.GetChild(i).gameObject);
                }
            }
            //清空小时项
            Transform hour = hourPrefab.transform.parent;
            if (hour.childCount > 1)
            {
                for (int i = 1; i < hour.childCount; i++)
                {
                    Destroy(hour.GetChild(i).gameObject);
                }
            }
            //清空分钟项
            Transform minute = minutePrefab.transform.parent;
            if (minute.childCount > 1)
            {
                for (int i = 1; i < minute.childCount; i++)
                {
                    Destroy(minute.GetChild(i).gameObject);
                }
            }
        }

        /// <summary>
        /// 上个月份按钮点击事件
        /// </summary>
        public void OnPrevMonthButtonClick()
        {
            currentMonth--;
            if (currentMonth < 1)
            {
                currentMonth = 12;
                currentYear--;
            }
            Init(currentYear, currentMonth, currentDay, currentHour, currentMinute);
        }
        /// <summary>
        /// 下个月份按钮点击事件
        /// </summary>
        public void OnNextMonthButtonClick()
        {
            currentMonth++;
            if (currentMonth > 12)
            {
                currentMonth = 1;
                currentYear++;
            }
            Init(currentYear, currentMonth, currentDay, currentHour, currentMinute);
        }
        /// <summary>
        /// 上个年份按钮点击事件
        /// </summary>
        public void OnPrevYearButtonClick()
        {
            Init(currentYear - 1, currentMonth, currentDay, currentHour, currentMinute);
        }
        /// <summary>
        /// 下个年份按钮点击事件
        /// </summary>
        public void OnNextYearButtonClick()
        {
            Init(currentYear + 1, currentMonth, currentDay, currentHour, currentMinute);
        }
        /// <summary>
        /// 选中当前时间按钮点击事件
        /// </summary>
        public void OnSelectNowButtonClick()
        {
            SelectTime(DateTime.Now);
        }
        /// <summary>
        /// 确认按钮点击事件
        /// </summary>
        public void OnConfirmButtonClick()
        {
            onEndEdit?.Invoke(CurrentSelectedTime);
        }
        /// <summary>
        /// 选中时间
        /// </summary>
        /// <param name="dateTime">时间</param>
        public void SelectTime(DateTime dateTime)
        {
            //如果日历当前内容不是当前年份和月份 重新初始化日历
            if (dateTime.Year != currentYear || dateTime.Month != currentMonth)
            {
                Init(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute);
            }
            //当前月份的1号是周几 代表1号前面有几个占位物体
            int weekOfFirstDay = (int)new DateTime(dateTime.Year, dateTime.Month, 1).DayOfWeek;
            //选中当前日期
            dayPrefab.transform.parent.GetChild(weekOfFirstDay + dateTime.Day).GetComponent<Toggle>().isOn = true;
            //选中当前小时
            hourPrefab.transform.parent.GetChild(dateTime.Hour + 1).GetComponent<Toggle>().isOn = true;
            //选中当前分钟
            minutePrefab.transform.parent.GetChild(dateTime.Minute + 1).GetComponent<Toggle>().isOn = true;
        }
    }
}

到了这里,关于Unity Calendar 日历功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Calendar日历类型常见方法(必看!!)

    Hi i,m JinXiang ⭐ 前言 ⭐ 本篇文章主要介绍Calendar日历类型的几种常见方法以及部分理论知识 🍉欢迎点赞 👍 收藏 ⭐留言评论 📝私信必回哟😁 🍉博主收将持续更新学习记录获,友友们有任何问题可以在评论区留言   Calendar类是一个抽象类,它为特定瞬间与一组诸如 Y

    2024年02月05日
    浏览(24)
  • java中Calendar日历类型常见方法

    Calendar是Java中常用的时间处理工具之一,它提供了很多日历类型常见方法,下面是一些常用的方法及对应的代码和运行结果。 目录 1. 如何创建 Calendar 日历对象 2. 获取时间 3. 设置时间 Calendar 是一个抽象类, 无法通过直接实例化得到对象. 因此, Calendar 提供了一个方法 getInsta

    2024年02月05日
    浏览(35)
  • 用 ElementPlus 的日历组件 Calendar 自定义渲染

    使用 ElementPlus中的 Calendar 组件完成自定义渲染 1. 英文改为中文 转为中文的方式:用 ElementPlus的日历组件如何改为中文 2. 修改样式 附源码 3. 自定义头部 4. 增删改功能接入

    2024年04月17日
    浏览(20)
  • Vue中的日历组件 Calendar 实现 考勤打卡记录

    实现效果图 1.由于Calendar没有右上角月份切换的API事件,可以给组件源码添加自定义添加一个事件 2.也可以通过自带的input事件来获取日历 3.vue页面完整代码 注释:this.$m(this.beginTime).format(‘YYYY-MM-DD HH:mm’),是分装的标准时间转化年月日,使用者可通过多种方法自定义处理。

    2024年01月22日
    浏览(35)
  • 【Unity】【VR开发】Unity云同步功能使用心得

    有时出差,旅行等等也带着电脑,晚上想要继续编辑项目,就需要用到云同步功能。目前实践下来,发现有些内容可以同步,有些内容则是不可以同步的,总结如下。 UnityHub的项目面板中有两个选项卡:项目和云端项目。 鼠标挪动到想要云同步的本地项目上,项目名右上角会

    2024年02月21日
    浏览(38)
  • element-plus日历(Calendar)动态渲染+避坑指南

    #dateCell 驼峰书写

    2024年02月11日
    浏览(29)
  • Unity 日历插件组件-日期选择器2D(二)

    提示:源码附在文后~大家互相学习 目录 前言 一、组件结构 二、使用步骤 1.脚本列表 2.绑定说明 1.外层作为总控制层 2.选择日期模块 总结 刚开始使用Unity开发项目,目前工作需求以Unity2D开发为主!发现在以Unity开发的管理系统中,时常要用到日期选择的工具!所以归类提炼

    2024年02月07日
    浏览(29)
  • Vant 的 Calendar 日历组件 自定义日期区间minDate、maxDate

          1.默认日历时间范围是当前时间到往后的6个月,可通过 min-date 和 max-date 自定义日历的范围。       2.设置 type 为 range 后可以选择日期区间,confirm 事件返回的 date 为数组结构,数组第一项为开始时间,第二项为结束时间。       3.可设置allow-same-day 允许选择同一天。

    2024年02月15日
    浏览(27)
  • 【Flutter】Flutter 使用 table_calendar 实现自定义日历

    【Flutter】Flutter 使用 table_calendar 实现自定义日历 你好!今天我要为你介绍一个非常实用的 Flutter 日历组件—— table_calendar 。这个组件不仅功能强大、高度可定制,而且使用起来非常简单。在本文中,我会手把手教你如何使用这个组件,并分享一些实际业务中的应用示例。希

    2024年02月08日
    浏览(32)
  • 关于uniapp中的日历组件uni-calendar中的小红点

    如果你使用过uni-calendar组件,可能你觉得这个小红点有点碍眼,但是官方给定的日历组件uni-calendar中如果你想要在某一天上添加一些信息例如:价格,签到,打卡之类,只要标记上就必定会带上小红点,那么我如何有想保留这些信息又把小红点去掉呢? 可以修改一下日历组件

    2024年02月15日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包