1. 程式人生 > >unity無限滑動日曆

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;
        }

    }
}