Unity拖动ScrollRect结束时始终让一个子物体位于中心位置

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

/// <summary>
/// 
/// 拖动ScrollRect结束时始终让一个子物体位于中心位置。
/// 
/// </summary>
public class CenterOnChild : MonoBehaviour, IEndDragHandler,  IBeginDragHandler
{
    //将子物体拉到中心位置时的速度
    public float centerSpeed = 9f;

    //注册该事件获取当拖动结束时位于中心位置的子物体
    public delegate void OnCenterHandler(GameObject centerChild);
    public event OnCenterHandler onCenter;

    private ScrollRect _scrollView;
    private Transform _container;

    private List<float> _childrenPos = new List<float>();
    private float _targetPos;
    private bool _centering = false;

    private float pastPos;     //之前记录
    private float currentPos; //当前记录
    private int count = 0;

    void Awake()
    {
        _scrollView = GetComponent<ScrollRect>();
        if (_scrollView == null)
        {
            Debug.LogError("CenterOnChild: No ScrollRect");
            return;
        }
        _container = _scrollView.content;

        GridLayoutGroup grid;
        grid = _container.GetComponent<GridLayoutGroup>();
        if (grid == null)
        {
            Debug.LogError("CenterOnChild: No GridLayoutGroup on the ScrollRect's content");
            return;
        }

        _scrollView.movementType = ScrollRect.MovementType.Unrestricted;

        //计算第一个子物体位于中心时的位置
        float childPosX = _scrollView.GetComponent<RectTransform>().rect.width * 0.5f - grid.cellSize.x * 0.5f;
        _childrenPos.Add(childPosX);
        //缓存所有子物体位于中心时的位置
        for (int i = 0; i < _container.childCount - 1; i++)
        {
            childPosX -= grid.cellSize.x + grid.spacing.x;
            _childrenPos.Add(childPosX);
        }
    }

    void Update()
    {
        if (_centering)
        {
            Vector3 v = _container.localPosition;
            v.x = Mathf.Lerp(_container.localPosition.x, _targetPos, centerSpeed * Time.deltaTime);
            _container.localPosition = v;
            if (Mathf.Abs(_container.localPosition.x - _targetPos) < 0.01f)
            {
                _centering = false;
            }
        }
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        pastPos = _container.localPosition.x;
        _centering = false;
    }
    public void OnEndDrag(PointerEventData eventData)
    {
        _centering = true;

        float cha= _container.localPosition.x - pastPos;
       if (cha<-50)
       {
           if (count< _childrenPos.Count-1)
           {
               count++;
            }
       }
       else
       {
           if (cha > 50)
           {
               if (count > 0)
               {
                   count--;
               }
            }
       }

       _targetPos = _childrenPos[count];

    }   
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值