Unity 拖拽翻页效果之通用脚本!

Unity — 拖拽翻页效果之通用脚本!


本文提供详细教程

记录遇到的难点并帮助同行的朋友们

坚持以最简单的方法传授和把更好的阅读体验带给你们!


一:效果图

在这里插入图片描述



二:添加组件

1:添加 Scroll View 组件,脚本挂载这里
在这里插入图片描述
2
在这里插入图片描述
3
在这里插入图片描述
4
在这里插入图片描述
5
在这里插入图片描述



三:脚本!


脚本挂载游戏对象上,自己调下都可以直接用

using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using System;
using UnityEngine;
using System.Linq;
using DG.Tweening;

public class HorizontalScrollPage : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
    //通常滑动速度
    private float NormalSmooting = 15;
    //滑动最快速度
    private float MaxSmooting = 1000;
    //当前速度
    public float Smooting;
    //每页显示的项目
    public int PageCount = 1;
    //灵敏度
    private float Sensitivity = 0.1f;
    //触摸的开始点
    private float BeginDragPos;
    //获取ScrollRect
    private ScrollRect Srect;
    //获取Content
    private Transform Content;
    //获取Content里面的项目列表
    private Image[] ItemList;
    //总页数
    private float PageIndex;
    //用来判断拖拽是否结束
    private bool IsDrag = false;
    //总页数索引比列 0-1
    private List<float> PageValueList = new List<float>();
    //滑动的目标位置
    private float TargetPos = 0;
    //当前位置索引
    public float NowIndex = 0;
    //是否使用过度动画
    public bool IsUseAni;

    public event Action StartEvent;

    public event Action EndEvent;

    public bool EventFinish;
    void Awake()
    {
        IsUseAni = true;
        Smooting = NormalSmooting;
        Srect = GetComponent<ScrollRect>();
        Content = Srect.content;
        HorizontalLayoutGroup contentHGroup;
        if (Content.gameObject.GetComponent<HorizontalLayoutGroup>() == null)
        {
            contentHGroup = Content.gameObject.AddComponent<HorizontalLayoutGroup>();
        }
        else
        {
            contentHGroup = Content.gameObject.GetComponent<HorizontalLayoutGroup>();
        }
        contentHGroup.childAlignment = TextAnchor.MiddleCenter;
        contentHGroup.childControlHeight = false;
        contentHGroup.childControlWidth = false;
        if (Content.childCount != 0)
        {
            ListAddItem();
            ListPageValueInit();
            TurningToNoAnima(0);
        }
    }
    void Start(){ }
    void ListAddItem()
    {
        ItemList = new Image[Content.childCount];
        for (int i = 0; i < Content.childCount; i++)
        {
            ItemList[i] = Content.GetChild(i).GetComponent<Image>();
        }
    }
    //每页比例  
    void ListPageValueInit()
    {
        PageIndex = (ItemList.Length / PageCount) - 1;
        if (ItemList != null && ItemList.Length != 0)
        {
            for (float i = 0; i <= PageIndex; i++)
            {
                PageValueList.Add((i / PageIndex));
            }
        }
    }

    void Update()
    {
        if (IsUseAni)
        {
            if (!IsDrag)
            {
                Srect.horizontalNormalizedPosition = Mathf.Lerp(Srect.horizontalNormalizedPosition, TargetPos, Time.deltaTime * Smooting);
                if (EventFinish)
                {
                    EndEvent?.Invoke();
                    EventFinish = false;
                }
            }
        }
        else
        {
            Srect.horizontalNormalizedPosition = TargetPos;
            IsUseAni = true;
            EndEvent?.Invoke();
        }
    }

    /// <summary>  
    /// 拖动开始  
    /// </summary>  
    /// <param name="eventData"></param>  
    public void OnBeginDrag(PointerEventData eventData)
    {
        StartEvent?.Invoke();
        IsDrag = true;
        BeginDragPos = Srect.horizontalNormalizedPosition;
    }

    /// <summary>  
    /// 拖拽结束  
    /// </summary>  
    /// <param name="eventData"></param>  
    public void OnEndDrag(PointerEventData eventData)
    {
        EventFinish = true;
        IsDrag = false;
        //获取拖动的值 
        var tempPos = Srect.horizontalNormalizedPosition;
        tempPos = tempPos > BeginDragPos ? tempPos + Sensitivity : tempPos - Sensitivity;
        var index = 0;
        //拖动的绝对值
        float offset = Mathf.Abs(PageValueList[index] - tempPos);
        for (int i = 1; i < PageValueList.Count; i++)
        {
            float temp = Mathf.Abs(tempPos - PageValueList[i]);
            if (temp < offset)
            {
                index = i;
                offset = temp;
            }
        }
        TargetPos = PageValueList[index];
        NowIndex = index;
    }

    public void BtnLeftGo()
    {
        StartEvent?.Invoke();
        NowIndex = Mathf.Clamp(NowIndex - 1, 0, PageIndex);
        TargetPos = PageValueList[Convert.ToInt32(NowIndex)];
    }

    public void BtnRightGo()
    {
        StartEvent?.Invoke();
        NowIndex = Mathf.Clamp(NowIndex + 1, 0, PageIndex);
        TargetPos = PageValueList[Convert.ToInt32(NowIndex)];
    }

    /// <summary>
    /// 通常速度翻页去指定页面
    /// </summary>
    /// <param name="num">页码</param>
    public void NormalSmootingTurningTo(int num)
    {
        StartEvent?.Invoke();
        Smooting = 15;
        NowIndex = Mathf.Clamp(num, 0, PageIndex);
        TargetPos = PageValueList[Convert.ToInt32(NowIndex)];
    }

    /// <summary>
    /// 指定速度去指定页面
    /// </summary>
    /// <param name="num">页码</param>
    /// <param name="speed">速度</param>
    public void SetSmootingTurningTo(int num, float speed)
    {
        StartEvent?.Invoke();
        Smooting = speed;
        NowIndex = Mathf.Clamp(num, 0, PageIndex);
        TargetPos = PageValueList[Convert.ToInt32(NowIndex)];
        Smooting = NormalSmooting;
    }

    /// <summary>
    /// 最快速度到指定页面
    /// </summary>
    /// <param name="num">页码</param>
    public void MaxSmootingTurningTo(int num)
    {
        StartEvent?.Invoke();
        Smooting = MaxSmooting;
        NowIndex = Mathf.Clamp(num, 0, PageIndex);
        TargetPos = PageValueList[Convert.ToInt32(NowIndex)];
        Smooting = NormalSmooting;
    }
    public void TurningToNoAnima(int num)
    {
        StartEvent?.Invoke();
        IsUseAni = false;
        NowIndex = num;
        TargetPos = PageValueList[Convert.ToInt32(NowIndex)];
    }
  }

拥有自己的服务器

让开发工作不再难

MyBe

阿里云 —ESC服务器部署和搭建购买方式(图文并排,一目了然)

一键领取阿里全产品2000元优惠券大礼包 (新手必得享超值优惠)


本博客为非营利性个人原创
所刊登的所有作品的著作权均为本人所拥有
本人保留所有法定权利,违者必究!
对于需要复制、转载、链接和传播博客文章或内容的
请及时和本博主进行联系,留言,Email: ChinazJacob@163.com
————————————————————————————————
版权声明:对于经本博主明确授权和许可使用文章及内容的
使用时请注明文章或内容出处并注明网址
转载请附上原文出处链接及本声明。
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alxes_七局

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值