码迷,mamicode.com
首页 > 其他好文 > 详细

UGUI循环滚动(横)

时间:2018-04-02 20:20:36      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:else   system   scale   ber   check   循环滚动   cto   normal   ring   

思路:

创建两份列表,通过改变位置实现循环

 

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

public class Scroll_Horizontal : 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;
circleNumber = circleNumber >= 2 ? circleNumber : 2;
halfCellSpace = cellSpace * 0.5f;
circleSpace = cellSpace.x * itemCount;

RectTransform rt = (RectTransform)itemParent;
startPosition = 0;
endPosition = -circleNumber * circleSpace + rt.rect.width;

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.x * j;
go.transform.localPosition = new Vector3(pos + halfCellSpace.x, -halfCellSpace.y, 0);
items.Add(go);
centerPositions.Add(-pos);
}
}
CheckSelect();
}

public void OnBeginDrag(PointerEventData eventData)
{

}

public void OnDrag(PointerEventData eventData)
{
if (eventData.delta.x > 0)
{
if (itemParent.transform.localPosition.x >= startPosition)
{
float pos = startPosition - circleSpace;
itemParent.localPosition = new Vector3(pos, 0, 0);
}
else
{
itemParent.localPosition += new Vector3(eventData.delta.x, 0, 0);
}
}
else
{
if (itemParent.transform.localPosition.x <= endPosition)
{
float pos = endPosition + circleSpace;
itemParent.localPosition = new Vector3(pos, 0, 0);
}
else
{
itemParent.localPosition += new Vector3(eventData.delta.x, 0, 0);
}
}
CheckSelect();
}

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

public int CheckSelect()
{
float pos_x = itemParent.transform.localPosition.x;
int index = 0;

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

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

return index;
}
}

UGUI循环滚动(横)

标签:else   system   scale   ber   check   循环滚动   cto   normal   ring   

原文地址:https://www.cnblogs.com/Joke-crazy/p/8695781.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!