UGUI循环滚动(竖)

Posted joke-crazy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UGUI循环滚动(竖)相关的知识,希望对你有一定的参考价值。

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Collections.Generic;

public class Scroll_Vertical : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public Transform itemParent;
public GameObject itemPrefab;

public Vector3 normalScale;
public Vector3 selectedScale;

public Vector2 cellSpace;
public int itemCount;
public int showCount = 8;

private int circleNumber;
private Vector2 halfCellSpace;

private float circleSpace;
private float startPosition;
private float endPosition;

private GameObject currentGo;
private List<GameObject> items;
private List<float> centerPositions;

private void Awake()
{
circleNumber = (showCount / itemCount) + 1;
halfCellSpace = cellSpace * 0.5f;

RectTransform rt = (RectTransform)itemParent;
startPosition = 0;
circleSpace = cellSpace.y * itemCount;
endPosition = circleNumber * circleSpace - rt.rect.height;

items = new List<GameObject>();
centerPositions = new List<float>();
}

private void Start()
{
for (int i = 0; i < circleNumber; i++)
{
for (int j = 0; j < itemCount; j++)
{
GameObject go = Instantiate(itemPrefab, itemParent) as GameObject;
go.GetComponentInChildren<Text>().text = "item_" + j;
go.name = "item_" + i.ToString() + j.ToString();
float pos = -(i * circleSpace + cellSpace.y * j);
go.transform.localPosition = new Vector3(halfCellSpace.x, pos - halfCellSpace.y, 0);
items.Add(go);
centerPositions.Add(-pos);
}
}
CheckSelect();
}

public void OnBeginDrag(PointerEventData eventData)
{

}

public void OnDrag(PointerEventData eventData)
{
if (eventData.delta.y > 0)
{
if (itemParent.transform.localPosition.y >= endPosition)
{
float pos = endPosition - circleSpace;
itemParent.localPosition = new Vector3(0, pos, 0);

}
else
{
itemParent.localPosition += new Vector3(0, eventData.delta.y, 0);
}
}
else
{
if (itemParent.transform.localPosition.y <= startPosition )
{
float pos = startPosition + circleSpace;
itemParent.localPosition = new Vector3(0, pos, 0);
}
else
{
itemParent.localPosition += new Vector3(0, eventData.delta.y, 0);
}
}
CheckSelect();
}

public void OnEndDrag(PointerEventData eventData)
{
int lastPos = CheckSelect();
itemParent.localPosition = new Vector3(0, centerPositions[lastPos], 0);
}

public int CheckSelect()
{
float pos_y = itemParent.transform.localPosition.y;
int index = 0;

for (int i = 0; i < centerPositions.Count; i++)
{
if (centerPositions[i] + halfCellSpace.y >= pos_y &&
centerPositions[i] - halfCellSpace.y <= pos_y)
{
index = i;
break;
}
}

if (currentGo != null)
{
currentGo.transform.localScale = normalScale;
}
currentGo = items[index];
currentGo.transform.localScale = selectedScale;

return index;
}
}

以上是关于UGUI循环滚动(竖)的主要内容,如果未能解决你的问题,请参考以下文章

3秒滚动一次,且无缝循环滚动,用css3怎么实现

循环滚动

UIScorlView 循环滚动

连续循环页面(非无限滚动)

循环滚动图片

C# 实现richtextbox 文字自动滚动 首尾相接循环