Unity ScrollView左右拖拽翻頁

ScrollView來實現左右拖拽的翻頁。類似於微信,左右拖拽時候上下無法拖拽,上下拖拽的時候左右無法拖拽。並且左右拖拽的是時候 會有彈力進行對對齊


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ScrollPagaUtil : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{
    bool isPressing;
    public ScrollRect sr;
    Vector2 deltaPos;
    public void OnBeginDrag(PointerEventData eventData)
    {
        //throw new System.NotImplementedException();

        if (moving != null) StopCoroutine(moving);
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        //throw new System.NotImplementedException();
        isPressing = false;
        if (moving != null) StopCoroutine(moving);
        Move();
    }

    private void OnApplicationFocus(bool focus)
    {
        if (!focus)
        {
            isPressing = false;
            MoveNow();
        }
    }


    public void OnDrag(PointerEventData eventData)
    {
       Vector2 drag =  eventData.delta;
        if(Mathf.Abs(drag.y) > 1f && !isPressing)
        {
            sr.vertical = true;
            sr.horizontal = false;
            sr.movementType = ScrollRect.MovementType.Elastic;
            sr.inertia = true;
            isPressing = true;
        }
        else if (Mathf.Abs(drag.x) > 1f && !isPressing)
        {
            sr.horizontal = true;
            sr.vertical = false;
            sr.movementType = ScrollRect.MovementType.Clamped;
            sr.inertia = false;
            isPressing = true;
        }

    }



    //-------------------------------------------------------------
    public GameObject[] activeObject;
    public GameObject[] disActiveObject;
    public Transform[]  contants;
    public Transform center;
    public Transform target;
    Coroutine moving;

    //獲取最近的一個
    Transform GetNearContant()
    {
        float min = 9999;
        Transform near = null;
        foreach (Transform trans in contants)
        {
            float x = trans.position.x - center.transform.position.x;
            if (Mathf.Abs(x) < min)
            {
                min = Mathf.Abs(x);
                near = trans;
            }
        }
        return near;
    }

    //設置按鈕顯示與否
    void SetButtonActive(int index)
    {
        for(int i =0;i< activeObject.Length; i++)
        {
            activeObject[i].SetActive(i == index);
            disActiveObject[i].SetActive(i != index);
        }
    }

    //設置按鈕顯示與否
    void SetButtonActive(Transform obj)
    {
       for(int i = 0;i< contants.Length; i++)
        {
            if(contants[i] == obj)
            {
                SetButtonActive(i);
                return;
            }
        }
    }

    
    //移動到第一個
    public void MoveToFirst()
    {
        if (moving != null) StopCoroutine(moving);
        Transform near = contants[0];
        SetButtonActive(0);
        Vector3 pos = target.transform.localPosition;
        pos.x = -near.localPosition.x;
        target.transform.localPosition = pos;
        
    }

    //移動到
    public void MoveTo(int index)
    {
        if (moving != null) StopCoroutine(moving);
        Transform near = contants[index];
        SetButtonActive(index);
        Vector3 pos = target.transform.localPosition;
        pos.x = -near.localPosition.x;
        target.transform.localPosition = pos;
    }

    //移動
    void Move()
    {
        if (moving != null) StopCoroutine(moving);
        Transform near = GetNearContant();
        float xPos = -near.localPosition.x;
        SetButtonActive(near);
        moving = StartCoroutine(_MoveTo(xPos));
    }
    
    //立即移動到
    void MoveNow()
    {
        if (moving != null) StopCoroutine(moving);
        Transform near = GetNearContant();
        Vector3 pos = target.transform.localPosition;
        pos.x = -near.localPosition.x;
        SetButtonActive(near);
        target.transform.localPosition = pos;
    }

    //移動到
    IEnumerator _MoveTo(float x)
    {

        float from = target.localPosition.x;
        float to = x;

        float timer = 0;
        float time = 0.25f; 

        while (timer < time)
        {
            timer += Time.deltaTime;
            float t = timer / time;
            float curX = Mathf.Lerp(from, to, t);
            Vector3 pos = target.localPosition;
            pos.x = curX;
            target.localPosition = pos;

            yield return null;
        }

        moving = null;
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章