unity无限滑动日历

链接: https://pan.baidu.com/s/1gPe-fOjs4UBR-YT43Jcsrg 提取码: vx4v 复制这段内容后打开百度网盘手机App,操作更方便哦

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class TestCalendar : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        back.onClick.AddListener(BackClick);
        forward.onClick.AddListener(ForwardClick);

        //获取系统时间格式   19/01/02
        //初始化显示日期
        InitDate();
    }


    //初始化显示日期
    void InitDate()
    {
        dates[1].GetComponentInChildren<Text>().text = GetAddDayDate(-2);
        dates[2].GetComponentInChildren<Text>().text = GetAddDayDate(-1);
        dates[3].GetComponentInChildren<Text>().text = GetAddDayDate(0);
        dates[4].GetComponentInChildren<Text>().text = GetAddDayDate(1);
        dates[5].GetComponentInChildren<Text>().text = GetAddDayDate(2);
    }

    

    //获取显示指定日期
    string GetAddDayDate(int i)
    {
        string date = null;
        if (i == 0)
        {
            date = DateTime.Now.Year.ToString()[2].ToString() + DateTime.Now.Year.ToString()[3].ToString() +
          "/" + (DateTime.Now.Month.ToString().Length > 1 ? DateTime.Now.Month.ToString() : ("0" + DateTime.Now.Month.ToString()))
          + "/" + (DateTime.Now.Day.ToString().Length > 1 ? DateTime.Now.Day.ToString() : ("0" + DateTime.Now.Day.ToString()));
            return date;
        }
        else
        {
            date = DateTime.Now.AddDays(i).Year.ToString()[2].ToString() + DateTime.Now.AddDays(i).Year.ToString()[3].ToString() +
          "/" + (DateTime.Now.AddDays(i).Month.ToString().Length > 1 ? DateTime.Now.AddDays(i).Month.ToString() : ("0" + DateTime.Now.AddDays(i).Month.ToString()))
          + "/" + (DateTime.Now.AddDays(i).Day.ToString().Length > 1 ? DateTime.Now.AddDays(i).Day.ToString() : ("0" + DateTime.Now.AddDays(i).Day.ToString()));
            return date;
        }
    }

    public GameObject calendar;//日历部分 
    RectTransform rectTransform;
    Vector2 target;


    float distance = 50;//间距需要自己根据实际间距设置
    bool isback;
     bool isMove;
    bool isForward;
    public Button back;
    public Button forward;
    void BackClick()
    {
        if (isMove == false)
        {
            //获取系统时间日期    189   待做
            target = new Vector2(calendar.GetComponent<RectTransform>().anchoredPosition.x - distance, calendar.GetComponent<RectTransform>().anchoredPosition.y);
            rectTransform = calendar.GetComponent<RectTransform>();
            isMove = true;
            isback = true;//向后
        }

    }
    void ForwardClick()
    {
        if (isMove == false)
        {
            //获取系统时间日期
            target = new Vector2(calendar.GetComponent<RectTransform>().anchoredPosition.x + distance, calendar.GetComponent<RectTransform>().anchoredPosition.y);
            rectTransform = calendar.GetComponent<RectTransform>();
            isMove = true;
            isForward = true;//向前
        }

    }

    public RectTransform[] dates;
    //0是中心点    1   2   3   4   5   间距是189
    int center = 3;//初始3是在中间
    int adddays = 3;
    int reducedays = -3;
    /**  
    注意 1-5 代表初始x坐标从左到右依次增加等距排列,刚开始3在最中间
    向左
    1   2   3   4   5        
    2   3   4   5   1
    3   4   5   1   2
    4   5   1   2    3
    5   1   2   3    4
    1   2   3   4    5

    中间时移动的
    1   2  3  4    5
    -1  0  1   2   3
    4   5  1   2   3
            
     
    向右
    1     2     3     4      5
    5     1     2     3      4
    4     5     1     2      3
    3     4     5     1      2
    2     3    4      5      1
    1     2    3      4      5

     
     */
    //获取需要向左移动的Item索引
    int GetBackMoveIndex(int i)
    {
        if (i == 1)
        {
            return 4;
        }
        else if (i == 2)
        {
            return 5;
        }
        else if (i == 3)
        {
            return 1;
        }
        else if (i == 4)
        {
            return 2;
        }
        else if (i == 5)
        {
            return 3;
        }
        else
        {
            return 0;
        }
    }

    //获取需要向右移动的Item索引
    int GetForwardMoveIndex(int i)
    {
        if (i == 1)
        {
            return 3;
        }
        else if (i == 2)
        {
            return 4;
        }
        else if (i == 3)
        {
            return 5;
        }
        else if (i == 4)
        {
            return 1;
        }
        else if (i == 5)
        {
            return 2;
        }
        else
        {
            return -1;
        }
    }

    private void Update()
    {
        if (isMove)
        {
            if (rectTransform != null)
            {
                rectTransform.anchoredPosition = Vector2.MoveTowards(rectTransform.anchoredPosition, target, 20);//20是移动速度可以调节
                //移动到目标点
                if (rectTransform.anchoredPosition == target)
                {
                    //修改显示数据以及位置
                    //初始是3在最中间

                    //分为两种情况一个是向前一个是向后


                    //向后
                    //第一次需要把1移动到最后面  5*189
                    if (isback)
                    {
                        //移动中心点归位
                        dates[0].anchoredPosition = new Vector2(dates[0].anchoredPosition.x + distance, dates[0].anchoredPosition.y);

                        //移动最左面的到最右面
                        dates[GetBackMoveIndex(center)].anchoredPosition = new Vector2(
                            dates[GetBackMoveIndex(center)].anchoredPosition.x + 5 * distance,
                            dates[GetBackMoveIndex(center)].anchoredPosition.y);

                        //修改最左面和最右面的日期
                        //特别注意同时需要考虑加的天数以及减的天数------特别重要
                        dates[GetBackMoveIndex(center)].GetComponentInChildren<Text>().text = GetAddDayDate(adddays + reducedays + 3);
                        //dates[(GetBackMoveIndex(center)-1)==0?5: (GetBackMoveIndex(center)-1)].GetComponentInChildren<Text>().text = GetAddDayDate(adddays-4);
                        adddays += 1;
                        // reducedays += 1;

                        center += 1;
                        if (center == 6)
                            center = 1;

                    }

                    //向右
                    //第一次需要把5移动到最前面
                    if (isForward)
                    {
                        //移动中心点归位
                        dates[0].anchoredPosition = new Vector2(dates[0].anchoredPosition.x - distance, dates[0].anchoredPosition.y);
                        //移动最右面的到最左面
                        dates[GetForwardMoveIndex(center)].anchoredPosition = new Vector2(
                            dates[GetForwardMoveIndex(center)].anchoredPosition.x - 5 * distance,
                            dates[GetForwardMoveIndex(center)].anchoredPosition.y);

                        //修改最右面和最左面的日期
                        //特别注意同时需要考虑加的天数以及减的天数------特别重要
                        dates[GetForwardMoveIndex(center)].GetComponentInChildren<Text>().text = GetAddDayDate(reducedays + adddays - 3);
                        //dates[(GetForwardMoveIndex(center)+1) == 6 ? 1 : (GetBackMoveIndex(center)+1)].GetComponentInChildren<Text>().text = GetAddDayDate(reducedays + 4);
                        reducedays -= 1;
                        //adddays -= 1;

                        center -= 1;
                        if (center == 0)
                            center = 5;
                    }


                    isback = isForward = isMove = false;

                    //是谁要高亮
                   // SetActiveAndSize();
                }
            }

        }

    }

    //判断谁应该高亮    101  129  190  255  字体大小125/94
    void SetActiveAndSize()
    {
        int temp = -1;
        for (int i = 1; i < dates.Length; i++)
        {
            if (Vector2.Distance(dates[0].anchoredPosition, dates[i].anchoredPosition) < 10)
            {
                temp = i;

            }
            dates[i].GetComponentInChildren<Text>().color = new Color(0, 0, 0, 1);
            dates[i].GetComponentInChildren<Text>().fontSize = 94;
            Debug.Log(i.ToString() + "与中心点的距离:" + Vector2.Distance(dates[0].anchoredPosition, dates[i].anchoredPosition));
        }
        if (temp != -1)
        {
            dates[temp].GetComponentInChildren<Text>().color = new Color(101f / 255, 129f / 255, 190f / 255, 255f / 255);
            dates[temp].GetComponentInChildren<Text>().fontSize = 125;
        }

    }
}


猜你喜欢

转载自blog.csdn.net/weixin_41995872/article/details/85621417