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

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值