Unity简单的一次一页滑动效果

脚本挂上即可

 public class ScrollPage : MonoBehaviour, IBeginDragHandler, IEndDragHandler
    {
        private ScrollRect rect;
        //页面:0,1,2,3  索引从0开始
        private List<float> pages = new List<float>();
        private int currentPageIndex = -1;

        //滑动速度
        public float smooting = 4;

        //单页显示个数
        public int perPageCount;

        //滑动的起始坐标
        private float targethorizontal = 0;

        //是否拖拽结束
        bool isDrag = false;

        /// <summary>
        /// 用于返回一个页码,-1说明page的数据为0
        /// </summary>
        public System.Action<int, int> OnPageChanged;

        float startime = 0f;
        float delay = 0.1f;

        void Start()
        {
            rect = transform.GetComponent<ScrollRect>();
            startime = Time.time;
        }

        void Update()
        {
            if (Time.time < startime + delay)
            {
                return;
            }
            UpdatePages();
            if (!isDrag && pages.Count > 0)
            {
                rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, Time.deltaTime * smooting);
            }
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            isDrag = true;
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            isDrag = false;

            float posX = rect.horizontalNormalizedPosition;
            int index = 0;
            //假设离第一位最近
            float offset = Mathf.Abs(pages[index] - posX);
            for (int i = 1; i < pages.Count; i++)
            {
                float temp = Mathf.Abs(pages[i] - posX);
                if (temp < offset)
                {
                    index = i;
                    offset = temp;
                }
            }

            if (index != currentPageIndex)
            {
                currentPageIndex = index;
                if(OnPageChanged != null)
                {
                    OnPageChanged(pages.Count, currentPageIndex);
                }                
            }
            targethorizontal = pages[index];
        }

        private void UpdatePages()
        {
            // 获取子对象的数量
            int count = this.rect.content.childCount;
            int temp = 0;
            for (int i = 0; i < count; i++)
            {
                if (this.rect.content.GetChild(i).gameObject.activeSelf)
                {
                    temp++;
                }
            }
            count = temp;

            if (pages.Count != count)
            {
                if (count != 0)
                {
                    pages.Clear();
                    for (int i = 0; i < count; i++)
                    {
                        float page = 0;
                        if (count != 1)
                        {
                            page = i / ((float)(count - 1));
                        }
                        pages.Add(page);
                    }
                }
                OnEndDrag(null);
            }
        }

        public void SetTargethorizontal()
        {
            targethorizontal = 0;
        }
    }

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值