1. 程式人生 > >Unity3D UGUI製作滾動日期選擇

Unity3D UGUI製作滾動日期選擇

然後直接新建指令碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

/// <summary>
/// 指令碼掛在ScrollView上
/// </summary>
public class Clander : MonoBehaviour ,IDragHandler{
    //滾動檢視
    public ScrollRect year;
    //滑鼠點選和釋放時候的座標  通過eventData獲取
Vector3 pressPos; Vector3 releasePos; //拖拽的距離 float distance; //滾動檢視content下物體的高 float itemHeight; //年份 int middleYear = 1949; //選擇的年份 string selectYeay; //拖拽觸發 public void OnDrag(PointerEventData eventData) { // ContentItemMove(eventData.delta); // print(eventData.delta); delta是一個動態的值
pressPos = eventData.pressPosition; releasePos = eventData.position; //求距離 distance = Mathf.Abs(releasePos.y - pressPos.y); ContentItemMove(eventData.delta); YearChange(); } // Use this for initialization void Start () { itemHeight
= year.content.GetChild(0).GetComponent<RectTransform>().rect.height; year.content.GetChild(2).GetComponent<Text>().text = middleYear.ToString(); } // Update is called once per frame void Update () { } /// <summary> /// 用來移動檢視 /// </summary> /// <param name="move"></param> void ContentItemMove(Vector2 move) { //當移動距離大於一個item的時候 if (distance > itemHeight) { for (int i = 0; i < year.content.childCount; i++) { //上移 if (releasePos.y >pressPos.y) { // year.content.GetChild(i).GetComponent<RectTransform>().anchoredPosition += new Vector2(0 ,move.y ); } else //下移 { year.content.GetChild(i).GetComponent<RectTransform>().anchoredPosition += new Vector2(0,move.y); } } } } //無限滾動和改變年份(有Bug),思路就是在滾動檢視中有四個item 顯示介面中最中間的item是基準, 也就是content中索引是2的子物體 這個索引是2的物體是一直在改變的 void YearChange() { for (int i = 0;i<year.content.childCount;i++) { //處於索引2的時候,字型是白色 其餘的是黑色 用來突出選擇的年份 if (year.content.GetChild(i).GetComponent<RectTransform>().anchoredPosition.y > -15 && year.content.GetChild(i).GetComponent<RectTransform>().anchoredPosition.y < 15) { year.content.GetChild(i).GetComponent<Text>().color = Color.white; //把選擇的年份賦值到變數中,一遍獲取 selectYeay = year.content.GetChild(i).GetComponent<Text>().text; } else { year.content.GetChild(i).GetComponent<Text>().color = Color.black; } } //在拖拽的過程中,作為基準的索引2的相對位置的y值大於15的時候(整個item的高是30) 把最上面的item移動到最下面 if (year.content.GetChild(2).GetComponent<RectTransform>().anchoredPosition.y > 15) { //更改預設年份 middleYear = middleYear + 1; //改所i有item的索引 year.content.GetChild(0).SetAsLastSibling(); //移動 放到最後一個item的下面 year.content.GetChild(0).GetComponent<RectTransform>().anchoredPosition = year.content.GetChild(3).GetComponent<RectTransform>().anchoredPosition - new Vector2(0, itemHeight); //更改顯示的年份 year.content.GetChild(0).GetComponent<Text>().text = (middleYear + 2).ToString(); } else if (year.content.GetChild(2).GetComponent<RectTransform>().anchoredPosition.y < -45) { middleYear = middleYear - 1; year.content.GetChild(3).SetAsFirstSibling(); year.content.GetChild(3).GetComponent<RectTransform>().anchoredPosition = year.content.GetChild(0).GetComponent<RectTransform>().anchoredPosition + new Vector2(0, itemHeight); year.content.GetChild(3).GetComponent<Text>().text = (middleYear - 1).ToString(); } } public void ConfirmYear() { print("當前選擇年份" + selectYeay); } }