本文提供详细教程 记录遇到的难点并帮助同行的朋友们 坚持以最简单的方法传授和把更好的阅读体验带给你们! |
一:效果图
二:添加组件
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 ———————————————————————————————— 版权声明:对于经本博主明确授权和许可使用文章及内容的 使用时请注明文章或内容出处并注明网址 转载请附上原文出处链接及本声明。 |