Unity再滚动视图下实现水平翻页
作者:互联网
滚动视图的MovementType属性设置为Clamped,然后去除Inertia的勾选
其中用到UnitManager代码在:Unity:利用UnitManager类实现一些复杂的功能(长期更新)_努力长头发的程序猿的博客-CSDN博客
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ScrollPage : MonoBehaviour , IPointerDownHandler , IPointerUpHandler
{
private RectTransform content;
private float lastClickPosX;
private Vector2 lastContentPos;
private float clickTime;
float selectCarSize;
void Start()
{
content = transform.Find("Viewport/Content") as RectTransform;
selectCarSize = (transform as RectTransform).sizeDelta.x + transform.Find("Viewport/Content").GetComponent<HorizontalLayoutGroup>().spacing;
}
public void OnPointerDown(PointerEventData eventData)
{
lastClickPosX = FanGameManager.Main.GetNumPos().x;
lastContentPos = content.anchoredPosition;
clickTime = Time.time + 0.5f;
}
public void OnPointerUp(PointerEventData eventData)
{
if (Time.time <= clickTime)
{
if (FanGameManager.Main.GetNumPos().x > lastClickPosX + 150.0f)
{
UnitManager.Main.Domove(content, new Vector2(-((int)(Math.Abs(content.anchoredPosition.x) / selectCarSize)) * selectCarSize, content.anchoredPosition.y), 0.1f);
}
else if (FanGameManager.Main.GetNumPos().x < lastClickPosX - 150.0f)
{
UnitManager.Main.Domove(content, new Vector2(-((int)(Math.Abs(content.anchoredPosition.x) / selectCarSize) + 1) * selectCarSize, content.anchoredPosition.y), 0.1f);
}
else
{
UnitManager.Main.Domove(content, lastContentPos, 0.1f);
}
}
else
{
if (Math.Abs(content.anchoredPosition.x) % selectCarSize > selectCarSize * 0.5f)
{
UnitManager.Main.Domove(content, new Vector2(-((int)(Math.Abs(content.anchoredPosition.x) / selectCarSize) + 1) * selectCarSize, content.anchoredPosition.y), 0.1f);
}
else
{
UnitManager.Main.Domove(content, new Vector2(-((int)(Math.Abs(content.anchoredPosition.x) / selectCarSize)) * selectCarSize, content.anchoredPosition.y), 0.1f);
}
}
}
}
标签:selectCarSize,翻页,anchoredPosition,Vector2,视图,content,Unity,UnitManager,Main 来源: https://blog.csdn.net/qq_39332021/article/details/122586680