Unity 无限滑动列表制作
Unity 无限滚动列表功能,效果,循环滚动,并且可以点击交互。
下面展示一些 内联代码片
。
public class SlideList : MonoBehaviour,IDragHandler
{
public static SlideList Instance;
public List<RectTransform> itemDataArray = new List<RectTransform>();
private List<Vector3> standardItemPosition = new List<Vector3>();
private static float timer = 0;
public float slideSpeed = 0.5f;
private void Start()
{
Instance = this;
foreach (RectTransform item in transform.Find("Content"))
{
itemDataArray.Add(item);
standardItemPosition.Add(item.localPosition);
}
}
public void HidScrollView()
{
this.gameObject.SetActive(false);
}
public void OnDrag(PointerEventData eventData)
{
if (Input.GetAxis("Mouse Y") < 0)
{
timer -= Input.GetAxis("Mouse Y") * slideSpeed;
DownSlide(timer);
}
if (Input.GetAxis("Mouse Y") > 0)
{
timer += Input.GetAxis("Mouse Y") * slideSpeed;
UPSlide(timer);
}
}
private bool isSliderDown;
public void UPSlide(float value)
{
for (int i = 0; i < itemDataArray.Count; i++)
{
if (i > 0)
{
value = Mathf.Clamp(value, 0, 1);
itemDataArray[i].localPosition = Vector3.Lerp(standardItemPosition[i], standardItemPosition[i - 1], value);
}
else if (i == 0)
{
itemDataArray[i].localPosition = Vector3.Lerp(standardItemPosition[i], standardItemPosition[i] - new Vector3(0, -129, 0), value);
}
}
if (value >= 1)
{
for (int i = 0; i < itemDataArray.Count - 1; i++)
{
RectTransform temp = itemDataArray[i];
itemDataArray[i] = itemDataArray[i + 1];
itemDataArray[i + 1] = temp;
}
timer = 0;
itemDataArray[itemDataArray.Count - 1].localPosition = standardItemPosition[standardItemPosition.Count - 1];
}
}
public void DownSlide(float value)
{
for (int i = itemDataArray.Count - 1; i >= 0; i--)
{
value = Mathf.Clamp(value, 0, 1);
if (i < itemDataArray.Count - 1)
{
itemDataArray[i].localPosition = Vector3.Lerp(standardItemPosition[i], standardItemPosition[i + 1], value);
}
else if (i == itemDataArray.Count - 1)
{
itemDataArray[i].localPosition = Vector3.Lerp(standardItemPosition[i], standardItemPosition[i] + new Vector3(0, -129, 0), value);
}
}
if (value >= 1)
{
itemDataArray[itemDataArray.Count - 1].localPosition = standardItemPosition[0];
for (int i = itemDataArray.Count - 1; i > 0; i--)
{
RectTransform temp = itemDataArray[i];
itemDataArray[i] = itemDataArray[i - 1];
itemDataArray[i - 1] = temp;
}
timer = 0;
}
}
}
下面展示一些 内联代码片
。
public class Item :MonoBehaviour, IBeginDragHandler,IEndDragHandler,IDragHandler,IPointerDownHandler,IPointerUpHandler
{
public bool isClick;
public void OnBeginDrag(PointerEventData eventData)
{
isClick = false;
}
public void OnEndDrag(PointerEventData eventData)
{
}
public void OnPointerDown(PointerEventData eventData)
{
isClick = true;
}
public void OnPointerUp(PointerEventData eventData)
{
if (isClick) {
Debug.Log("点击:"+transform.GetComponentInChildren<Text>());
}
}
void IDragHandler.OnDrag(PointerEventData eventData)
{
SlideList.Instance.OnDrag(eventData);
}
}
工程链接,按需下载
https://download.csdn.net/download/qq_33547099/13799875