Unity日期时间选择器

2020-12-24  本文已影响0人  玄策丶

参考 https://blog.csdn.net/sinat_39291423/article/details/91039436?locationNum=11&fps=1
一、搭建场景

下拉条.png image.png image.png image.png

1、

/***********************
* Title:        年 月 日
* FileName:         
* Date:         2020
* Author:       玄策
* Version:      1.0
* UnityVersion: 2019.2.4f1
* Description:  
* Func:           
* -   
***********************/
using System.Collections;
using System;
using UnityEngine;
using UnityEngine.UI;
public class ChoiceTime : MonoBehaviour
{
    [Tooltip("箭头图片")]
    //public Sprite[] Arrows;
    //显示时间的文字
    public Text Text_Data;
    //显示箭头的图片
    private Image ArrowsImage;
    //选择时间界面
    public GameObject ChoiceTimeObj;
    //按钮
    private Button ChoiceBtn;
    //是否选择时间
    private bool isShowChoiceTime;
    void Start()
    {
        //Text_Data = GameObject.Find("Text_Data").GetComponent<Text>();
        //ArrowsImage = GameObject.Find("ShowArrows").GetComponent<Image>();

        //ChoiceTimeObj = GameObject.Find("Time_Data");
        ChoiceTimeObj.SetActive(false);
        ChoiceBtn = GetComponent<Button>();
        ChoiceBtn.onClick.AddListener(StartChoiceTime);
    }
    private void OnEnable()
    {
        //开始默认选择系统时间

        //Text_Data.text = DateTime.Now.ToString("yyyy年MM月dd日 HH : mm : ss");
        Text_Data.text = DateTime.Now.ToString("yyyy 年 MM 月 dd 日");
    }
    // Update is called once per frame
    void Update()
    {
        if (ChoiceTimeObj.activeSelf)
        {
            isShowChoiceTime = true;
        }
        else
        {
            isShowChoiceTime = false;
        }
    }
    public void StartChoiceTime()
    {
        //作战时间的number为1,开始时间的number为2   
        if (!isShowChoiceTime)
        {
            //显示选择时间界面
            ChoiceTimeObj.SetActive(true);
            //箭头向下
            //ArrowsImage.sprite = Arrows[1];
        }
        else
        {
            //关闭选择时间界面
            ChoiceTimeObj.SetActive(false);
            //是否显示时间选择界面为false
            isShowChoiceTime = false;
            //箭头向上
            //ArrowsImage.sprite = Arrows[0];
            //判断选没选择日期,当只点开选择框没有选择时,默认的日期会变为001年。所以要判断下
            if (DatePickerGroup._selectTime.ToString("yyyy 年 MM 月 dd 日").Substring(0, 3) == "000")
            {
                Text_Data.text = DateTime.Now.ToString("yyyy 年 MM 月 dd 日");
            }
            else
            {
                Text_Data.text = DatePickerGroup._selectTime.ToString("yyyy 年 MM 月 dd 日");
            }
            //string[] data = Text_Data.text.Split(' ');
            //UI_DaPingTest.Instance.year = int.Parse(data[0]);
            //UI_DaPingTest.Instance.mouth = int.Parse(data[2]);
            //UI_DaPingTest.Instance.day = int.Parse(data[4]);
        }
    }
}

2、

using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

/// <summary>
/// 当选择日期的委托
/// </summary>
public delegate void OnDateUpdate();

public enum DateType
{
    _year, _month, _day,
    _hour, _minute, _second
}
/// <summary>
/// 日期选择器
/// </summary>
public class DatePicker : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    /// <summary>
    /// 日期类型 (年月日时分秒)
    /// </summary>
    public DateType _dateType;
    /// <summary>
    /// 子节点数量(奇数)
    /// </summary>
    public int _itemNum = 5;
    [HideInInspector]
    /// <summary>
    /// 更新选择的目标值
    /// </summary>
    public float _updateLength;
    /// <summary>
    /// 子节点预制体
    /// </summary>
    public GameObject _itemObj;
    /// <summary>
    /// 子节点容器对象
    /// </summary>
    public Transform _itemParent;
    /// <summary>
    /// 我属于的日期选择组
    /// </summary>
    [HideInInspector]
    public DatePickerGroup myGroup;
    /// <summary>
    /// 当选择日期的委托事件
    /// </summary>
    public event OnDateUpdate _onDateUpdate;
    void Awake()
    {
        _itemObj.SetActive(false);
        _updateLength = _itemObj.GetComponent<RectTransform>().sizeDelta.y;
    }

    /// <summary>
    /// 初始化时间选择器
    /// </summary>
    public void Init()
    {
        for (int i = 0; i < _itemNum; i++)
        {
            //计算真实位置
            int real_i = -(_itemNum / 2) + i;
            SpawnItem(real_i);
        }
        RefreshDateList();
    }
    /// <summary>
    /// 生成子对象
    /// </summary>
    /// <param name="dataNum">对应日期</param>
    /// <param name="real_i">真实位置</param>
    /// <returns></returns>
    GameObject SpawnItem(float real_i)
    {
        GameObject _item = Instantiate(_itemObj);
        _item.SetActive(true);
        _item.transform.SetParent(_itemParent);
        _item.transform.localScale = new Vector3(1, 1, 1);
        _item.transform.localEulerAngles = Vector3.zero;
        _item.transform.localPosition = Vector3.zero;
        if (real_i != 0)
        {
            _item.GetComponent<Text>().color = new Color(0, 0, 0, 1 - 0.2f - (Mathf.Abs(real_i) / (_itemNum / 2 + 1)));
        }
        return _item;
    }

    Vector3 oldDragPos;
    /// <summary>
    /// 当开始拖拽
    /// </summary>
    /// <param name="eventData"></param>
    public void OnBeginDrag(PointerEventData eventData)
    {
        oldDragPos = eventData.position;
    }

    public void OnDrag(PointerEventData eventData)
    {
        //Debug.Log("拖拽");
        UpdateSelectDate(eventData);
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        _itemParent.localPosition = Vector3.zero;
    }

    /// <summary>
    /// 更新选择的时间
    /// </summary>
    /// <param name="eventData"></param>
    void UpdateSelectDate(PointerEventData eventData)
    {
        //判断拖拽的长度是否大于目标值
        if (Mathf.Abs(eventData.position.y - oldDragPos.y) >= _updateLength)
        {
            int num;
            //判断加减
            if (eventData.position.y > oldDragPos.y)
            {
                //+
                num = 1;
            }
            else
            {
                //-
                num = -1;
            }
            DateTime _data = new DateTime();
            switch (_dateType)
            {
                case DateType._year:
                    _data = myGroup._selectDate.AddYears(num);
                    break;
                case DateType._month:
                    _data = myGroup._selectDate.AddMonths(num);
                    break;
                case DateType._day:
                    _data = myGroup._selectDate.AddDays(num);
                    break;
                case DateType._hour:
                    _data = myGroup._selectDate.AddHours(num);
                    break;
                case DateType._minute:
                    _data = myGroup._selectDate.AddMinutes(num);
                    break;
                case DateType._second:
                    _data = myGroup._selectDate.AddSeconds(num);
                    break;
            }
            //判断是否属于时间段
            if (IsInDate(_data, myGroup._minDate, myGroup._maxDate))
            {
                myGroup._selectDate = _data;
                oldDragPos = eventData.position;
                _onDateUpdate();
            }
            //   _itemParent.localPosition = Vector3.zero;
        }
        else
        {
            //_itemParent.localPosition = new Vector3(0, (eventData.position.y - oldDragPos.y), 0);
        }
    }

    /// <summary>
    /// 刷新时间列表
    /// </summary>
    public void RefreshDateList()
    {
        DateTime _data = new DateTime();
        for (int i = 0; i < _itemNum; i++)
        {
            //计算真实位置
            int real_i = -(_itemNum / 2) + i;
            switch (_dateType)
            {
                case DateType._year:
                    _data = myGroup._selectDate.AddYears(real_i);
                    break;
                case DateType._month:
                    _data = myGroup._selectDate.AddMonths(real_i);
                    break;
                case DateType._day:
                    _data = myGroup._selectDate.AddDays(real_i);
                    break;
                case DateType._hour:
                    _data = myGroup._selectDate.AddHours(real_i);
                    break;
                case DateType._minute:
                    _data = myGroup._selectDate.AddMinutes(real_i);
                    break;
                case DateType._second:
                    _data = myGroup._selectDate.AddSeconds(real_i);
                    break;
            }
            string str = "";
            if (IsInDate(_data, myGroup._minDate, myGroup._maxDate))
            {
                switch (_dateType)
                {
                    case DateType._year:
                        str = _data.Year.ToString();
                        break;
                    case DateType._month:
                        str = _data.Month.ToString();
                        break;
                    case DateType._day:
                        str = _data.Day.ToString();
                        break;
                    case DateType._hour:
                        str = _data.Hour.ToString();
                        break;
                    case DateType._minute:
                        str = _data.Minute.ToString();
                        break;
                    case DateType._second:
                        str = _data.Second.ToString();
                        break;
                }
            }
            _itemParent.GetChild(i).GetComponent<Text>().text = str;
        }

    }
    /// <summary> 
    /// 判断某个日期是否在某段日期范围内,返回布尔值
    /// </summary> 
    /// <param name="dt">要判断的日期</param> 
    /// <param name="dt_min">开始日期</param> 
    /// <param name="dt_max">结束日期</param> 
    /// <returns></returns>  
    public bool IsInDate(DateTime dt, DateTime dt_min, DateTime dt_max)
    {
        return dt.CompareTo(dt_min) >= 0 && dt.CompareTo(dt_max) <= 0;
    }
}


3、

using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 日期选择组
/// </summary>
public class DatePickerGroup : MonoBehaviour
{
    /// <summary>
    /// 最小日期和最大日期
    /// </summary>
    public DateTime _minDate, _maxDate;
    /// <summary>
    /// 选择的日期(年月日时分秒)
    /// </summary>
    public DateTime _selectDate;
    /// <summary>
    /// 时间选择器列表
    /// </summary>
    public List<DatePicker> _datePickerList;
    /// <summary>
    /// 当选择日期的委托事件
    /// </summary>
    public event OnDateUpdate _OnDateUpdate;
    public static DateTime _selectTime;
    void Awake()
    {
        //设置最大最小日期
        _minDate = new DateTime(1999, 1, 1, 0, 0, 0);
        _maxDate = new DateTime(2050, 1, 1, 0, 0, 0);
        Init();
    }
    public void Init(DateTime dt)
    {
        _selectDate = dt;
        for (int i = 0; i < _datePickerList.Count; i++)
        {
            _datePickerList[i].myGroup = this;
            _datePickerList[i].Init();
            _datePickerList[i]._onDateUpdate += onDateUpdate;
        }
    }
    public void Init()
    {
        _selectDate = DateTime.Now;
        for (int i = 0; i < _datePickerList.Count; i++)
        {
            _datePickerList[i].myGroup = this;
            _datePickerList[i].Init();
            _datePickerList[i]._onDateUpdate += onDateUpdate;
        }
    }
    /// <summary>
    /// 当选择的日期更新
    /// </summary>
    public void onDateUpdate()
    {
        //Debug.Log("当前选择日期:" + _selectDate.ToString("yyyy年MM月dd日 HH : mm : ss"));
        //将选中的时间给_selectTime ,供其他界面调用
        _selectTime = _selectDate;
        for (int i = 0; i < _datePickerList.Count; i++)
        {
            _datePickerList[i].RefreshDateList();
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读