unity Scroll View 翻页效果
编写一个 unity Scroll View 翻页效果 记录一下
将脚本挂在到 Scroll View 上
我这里使用了doTween 插件移动(这里不提供) 插件自己找 没有自己写 一个数值渐变的 或者update自己更新
cell count 一共多少页
movetime 翻页需要时间
spacing 每页间距
setpage index 跳转到对应页
纵向
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using DG.Tweening;
public class VerPageView : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
enum MoveDir
{
none,
up,
down
}
public delegate void UpdatepreDataDel(GameObject preObj, int index);
public UpdatepreDataDel updatepreDatadel;
public delegate void BeginDragDel(GameObject Obj);
public BeginDragDel beginDragdel;
public delegate void MoveOverDel(GameObject CurrentObj);
public MoveOverDel moveOverDel;
public int CellCount;
[Tooltip("移动时间")]
public float moveTime = 0.5f;
[Tooltip("间距")]
public float Spacing = 10;
private ScrollRect rect; //滑动组件
private List<float> verticalNormalizedPositionList = new List<float>();//求出每页的临界角,页索引从0开始
private List<Vector2> CellPositionList = new List<Vector2>();//求出每页的临界角,页索引从0开始
private List<GameObject> CellList = new List<GameObject>();
private GameObject Cell;
private float startDragVertical;
private int currentPageIndex = 0;
private MoveDir moveDir;
public int SetPageIndex = 0;
Color[] colors = new Color[] { Color.red, Color.yellow, Color.green };
private void Awake()
{
}
public void InitData()
{
rect = GetComponent<ScrollRect>();
rect.horizontal = false;
rect.vertical = true;
var _rectTrans = (RectTransform)transform;
float _rectHeight = _rectTrans.rect.height;
float _rectWidth = _rectTrans.rect.width;
VerticalLayoutGroup verticalLayoutGroup = rect.content.gameObject.AddComponent<VerticalLayoutGroup>();
Destroy(verticalLayoutGroup);
RectTransform cellrect = (RectTransform)rect.content.GetChild(0);
cellrect.sizeDelta = new Vector2(_rectWidth, _rectHeight);
Cell = rect.content.GetChild(0).gameObject;
Cell.SetActive(false);
var contentHeight = CellCount * (_rectHeight + Spacing) - Spacing;
rect.content.sizeDelta = new Vector2(0, contentHeight);
for (int i = 0; i < CellCount; i++)
{
verticalNormalizedPositionList.Add(1.0f / (CellCount - 1) * i);
CellPositionList.Add(new Vector2(_rectWidth / 2, 0-_rectHeight / 2 - (_rectHeight+Spacing) * i));
}
int maxCount = CellCount > 3 ? 3 : CellCount;
for (int i = 0; i < maxCount; i++)
{
GameObject cell = GameObject.Instantiate(Cell, rect.content);
CellList.Add(cell);
cell.transform.GetChild(0).GetComponent<Image>().color = colors[i];
cell.GetComponent<RectTransform>().anchoredPosition = CellPositionList[i];
cell.SetActive(true);
cell.name = i.ToString();
}
currentPageIndex = 0;
if (updatepreDatadel != null)
{
updatepreDatadel(CellList[0], currentPageIndex);
updatepreDatadel(CellList[1], currentPageIndex + 1);
updatepreDatadel(CellList[2], currentPageIndex + 2);
}
}
public void OnBeginDrag(PointerEventData eventData)
{
//开始拖动
startDragVertical = rect.verticalNormalizedPosition;
if (beginDragdel != null) beginDragdel(CellList[(currentPageIndex) % CellList.Count]);
}
public void OnEndDrag(PointerEventData eventData)
{
float posX = rect.verticalNormalizedPosition;
float to = posX - startDragVertical;
if (to == 0)
{
if(moveOverDel!=null) moveOverDel(CellList[(currentPageIndex) % CellList.Count]);
return;
}
if (currentPageIndex == 0 && to > 0)
{
if (moveOverDel != null) moveOverDel(CellList[(currentPageIndex) % CellList.Count]);
return;
}
if (currentPageIndex == verticalNormalizedPositionList.Count - 1 && to < 0)
{
if (moveOverDel != null) moveOverDel(CellList[(currentPageIndex) % CellList.Count]);
return;
}
moveDir = to < 0 ? MoveDir.up : MoveDir.down;
currentPageIndex = to < 0 ? ++currentPageIndex : --currentPageIndex;
UpdateCell();
mOnEndDrag();
}
public void ScrollToPage(int index)
{
currentPageIndex = index;
if (currentPageIndex < 0 || currentPageIndex >= CellCount) return;
if (currentPageIndex == 0) // 最上面
{
CellList[0].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex];
CellList[1].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex + 1];
CellList[2].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex + 2];
if (updatepreDatadel != null) updatepreDatadel(CellList[1], currentPageIndex + 1);
}
else if (currentPageIndex >= CellCount - 1) // 最下面
{
CellList[(currentPageIndex - 2) % CellList.Count].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex - 2];
CellList[(currentPageIndex - 1) % CellList.Count].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex - 1];
CellList[currentPageIndex % CellList.Count].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex];
if (updatepreDatadel != null) updatepreDatadel(CellList[(currentPageIndex - 1) % CellList.Count], currentPageIndex - 1);
}
else // 其他情况
{
CellList[(currentPageIndex - 1) % CellList.Count].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex - 1];
CellList[currentPageIndex % CellList.Count].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex];
CellList[(currentPageIndex + 1) % CellList.Count].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex + 1];
if (updatepreDatadel != null)
{
updatepreDatadel(CellList[(currentPageIndex - 1) % CellList.Count], currentPageIndex - 1);
updatepreDatadel(CellList[(currentPageIndex + 1) % CellList.Count], currentPageIndex + 1);
}
}
mOnEndDrag();
}
void mOnEndDrag()
{
DOTween.To(() => rect.verticalNormalizedPosition, x => rect.verticalNormalizedPosition = x, verticalNormalizedPositionList[CellCount - 1 - currentPageIndex], moveTime).SetEase(Ease.OutQuad).OnComplete(() => {
if (moveOverDel != null) moveOverDel(CellList[(currentPageIndex) % CellList.Count]);
});
}
void UpdateCell()
{
if (moveDir == MoveDir.up && currentPageIndex > 1 && currentPageIndex < CellCount - 1)
{
int panelIndex = (currentPageIndex - 2) % CellList.Count;
int dataindex = currentPageIndex + 1;
CellList[panelIndex].GetComponent<RectTransform>().anchoredPosition = CellPositionList[dataindex];
int preindex = currentPageIndex + 1;
if (preindex < CellCount)
{
//Debug.Log("preObj " + CellList[panelIndex].name + " preindex " + preindex);
if (updatepreDatadel != null) updatepreDatadel(CellList[panelIndex],preindex);
}
}
else if (moveDir == MoveDir.down && currentPageIndex >= 1 && currentPageIndex < CellCount - 2)
{
int panelIndex = (currentPageIndex + 2) % CellList.Count;
int dataindex = currentPageIndex - 1;
CellList[panelIndex].GetComponent<RectTransform>().anchoredPosition = CellPositionList[dataindex];
int preindex = currentPageIndex - 1;
if (preindex >= 0)
{
//Debug.Log("preObj " + CellList[panelIndex].name + " preindex " + preindex);
if (updatepreDatadel != null) updatepreDatadel(CellList[panelIndex],preindex);
}
}
}
}
横向
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using DG.Tweening;
public class HorPageView : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
enum MoveDir
{
none,
Left,
Right
}
public delegate void UpdatepreDataDel(GameObject preObj, int index);
public UpdatepreDataDel updatepreDatadel;
public delegate void BeginDragDel(GameObject Obj);
public BeginDragDel beginDragdel;
public delegate void MoveOverDel(GameObject CurrentObj);
public MoveOverDel moveOverDel;
public int CellCount;
[Tooltip("移动时间")]
public float moveTime = 0.5f;
[Tooltip("间距")]
public float Spacing = 10;
private ScrollRect rect; //滑动组件
private List<float> horizontalNormalizedPositionList = new List<float>();//求出每页的临界角,页索引从0开始
private List<Vector2> CellPositionList = new List<Vector2>();//求出每页的临界角,页索引从0开始
private List<GameObject> CellList = new List<GameObject>();
private GameObject Cell;
private float startDragHorizontal;
private int currentPageIndex = 0;
private MoveDir moveDir;
public int SetPageIndex = 0;
Color[] colors = new Color[] { Color.red, Color.yellow, Color.green };
private void Awake()
{
}
public void InitData()
{
rect = GetComponent<ScrollRect>();
rect.horizontal = true;
rect.vertical = false;
var _rectTrans = (RectTransform)transform;
float _rectwidth = _rectTrans.rect.width;
float _rectheight = _rectTrans.rect.height;
HorizontalLayoutGroup horizontalLayoutGroup = rect.content.gameObject.AddComponent<HorizontalLayoutGroup>();
Destroy(horizontalLayoutGroup);
RectTransform cellrect = (RectTransform)rect.content.GetChild(0);
cellrect.sizeDelta = new Vector2(_rectwidth, _rectheight);
Cell = rect.content.GetChild(0).gameObject;
Cell.SetActive(false);
var contentWideth = (CellCount-1) * (_rectwidth + Spacing) - Spacing;
rect.content.sizeDelta = new Vector2(contentWideth, _rectheight);
for (int i = 0; i < CellCount; i++)
{
horizontalNormalizedPositionList.Add(1.0f / (CellCount - 1) * i);
CellPositionList.Add(new Vector2(_rectwidth / 2 + (_rectwidth+Spacing) * i , -_rectheight / 2));
}
int maxCount = CellCount > 3 ? 3 : CellCount;
for (int i = 0; i < maxCount; i++)
{
GameObject cell = GameObject.Instantiate(Cell, rect.content);
CellList.Add(cell);
cell.transform.GetChild(0).GetComponent<Image>().color = colors[i];
cell.GetComponent<RectTransform>().anchoredPosition = CellPositionList[i];
cell.SetActive(true);
cell.name = i.ToString();
}
currentPageIndex = 0;
if (updatepreDatadel != null)
{
updatepreDatadel(CellList[0], currentPageIndex);
updatepreDatadel(CellList[1], currentPageIndex + 1);
updatepreDatadel(CellList[2], currentPageIndex + 2);
}
}
public void OnBeginDrag(PointerEventData eventData)
{
//开始拖动
startDragHorizontal = rect.horizontalNormalizedPosition;
if(beginDragdel!=null) beginDragdel(CellList[(currentPageIndex) % CellList.Count]);
}
public void OnEndDrag(PointerEventData eventData)
{
float posX = rect.horizontalNormalizedPosition;
float to = startDragHorizontal - posX;
if (to == 0)
{
if (moveOverDel != null) moveOverDel(CellList[(currentPageIndex) % CellList.Count]);
return;
}
if (currentPageIndex == 0 && to > 0)
{
if (moveOverDel != null) moveOverDel(CellList[(currentPageIndex) % CellList.Count]);
return;
}
if (currentPageIndex == horizontalNormalizedPositionList.Count - 1 && to < 0)
{
if (moveOverDel != null) moveOverDel(CellList[(currentPageIndex) % CellList.Count]);
return;
}
moveDir = to < 0 ? MoveDir.Left : MoveDir.Right;
currentPageIndex = to < 0 ? ++currentPageIndex: --currentPageIndex ;
UpdateCell();
mOnEndDrag();
}
public void ScrollToPage(int index)
{
currentPageIndex = index;
if (currentPageIndex < 0 || currentPageIndex >= CellCount) return;
if (currentPageIndex == 0) // 最左面
{
CellList[0].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex];
CellList[1].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex + 1];
CellList[2].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex + 2];
if (updatepreDatadel != null) updatepreDatadel(CellList[1], currentPageIndex + 1);
}
else if (currentPageIndex >= CellCount - 1) // 最右面
{
CellList[(currentPageIndex - 2) % CellList.Count].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex - 2];
CellList[(currentPageIndex - 1) % CellList.Count].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex - 1];
CellList[currentPageIndex % CellList.Count].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex];
if (updatepreDatadel != null) updatepreDatadel(CellList[(currentPageIndex - 1) % CellList.Count], currentPageIndex - 1);
}
else // 其他情况
{
CellList[(currentPageIndex - 1) % CellList.Count].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex - 1];
CellList[currentPageIndex % CellList.Count].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex];
CellList[(currentPageIndex + 1) % CellList.Count].GetComponent<RectTransform>().anchoredPosition = CellPositionList[currentPageIndex + 1];
if (updatepreDatadel != null)
{
updatepreDatadel(CellList[(currentPageIndex - 1) % CellList.Count], currentPageIndex - 1);
updatepreDatadel(CellList[(currentPageIndex + 1) % CellList.Count], currentPageIndex + 1);
}
}
mOnEndDrag();
}
void mOnEndDrag()
{
DOTween.To(() => rect.horizontalNormalizedPosition, x => rect.horizontalNormalizedPosition = x, horizontalNormalizedPositionList[currentPageIndex], moveTime).SetEase(Ease.OutQuad).OnComplete(() => {
if (moveOverDel != null) moveOverDel(CellList[(currentPageIndex) % CellList.Count]);
});
}
void UpdateCell()
{
if (moveDir == MoveDir.Left && currentPageIndex > 1 && currentPageIndex < CellCount - 1)
{
int panelIndex = (currentPageIndex - 2) % CellList.Count;
int dataindex = currentPageIndex + 1;
CellList[panelIndex].GetComponent<RectTransform>().anchoredPosition = CellPositionList[dataindex];
int preindex = currentPageIndex + 1;
if (preindex < CellCount)
{
//Debug.Log("preObj " + CellList[panelIndex].name + " preindex " + preindex);
if (updatepreDatadel != null) updatepreDatadel(CellList[panelIndex],preindex);
}
}
else if (moveDir == MoveDir.Right && currentPageIndex >= 1 && currentPageIndex < CellCount - 2)
{
int panelIndex = (currentPageIndex + 2) % CellList.Count;
int dataindex = currentPageIndex - 1;
CellList[panelIndex].GetComponent<RectTransform>().anchoredPosition = CellPositionList[dataindex];
int preindex = currentPageIndex - 1;
if (preindex >= 0)
{
//Debug.Log("preObj " + CellList[panelIndex].name + " preindex " + preindex);
if (updatepreDatadel != null) updatepreDatadel(CellList[panelIndex],preindex);
}
}
}
}
测试代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public VerPageView vpageView;
public HorPageView hpageView;
void Start()
{
vpageView.updatepreDatadel += UpdatepreData;
vpageView.beginDragdel += BeginDrag;
vpageView.moveOverDel += MoveOver;
vpageView.InitData();
hpageView.updatepreDatadel += UpdatepreData;
hpageView.beginDragdel += BeginDrag;
hpageView.moveOverDel += MoveOver;
hpageView.InitData();
}
private void MoveOver(GameObject CurrentObj)
{
Debug.LogWarning("显示 " + CurrentObj.name);
}
private void BeginDrag(GameObject Obj)
{
Debug.LogWarning("隐藏 " + Obj.name);
}
private void UpdatepreData(GameObject preObj, int index)
{
Debug.Log(" 更新 " + preObj.name + " " + index);
}
}
原文地址:https://blog.csdn.net/qq_40143976/article/details/144322099
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!