android scrollview 循环滚动,UGUI ScrollView循环滚动

using UnityEngine;

using UnityEngine.UI;

using System.Collections;

using System.Collections.Generic;

public class ScrollMove : MonoBehaviour {

public ScrollRect scrollRect;

public RectTransform gridRect;

public RectOffset padding;

public Vector2 spacing;

public GameObject prefab;

public int itemCount = 10;

private List prefabsList = new List ();

private int instantiatCount;

float itemHeight;

int upIndex = 0;

int botomIndex = 0;

int oldMoveIndex = 0;

float postionY = 0;

float scrollRectHeight;

// Use this for initialization

void Start ()

initScrollview ();

scrollRectHeight = scrollRect.GetComponent ().sizeDelta.y;

scrollRect.onValueChanged = new ScrollRect.ScrollRectEvent ();

scrollRect.onValueChanged.AddListener (OnScrollValueChanged);

void initScrollview ()

itemHeight = prefab.GetComponent ().sizeDelta.y + spacing.y;

gridRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, itemCount * itemHeight);

instantiatCount = Mathf.CeilToInt (scrollRect.GetComponent ().sizeDelta.y / itemHeight) + 1;//获取需要实例化的对象个数

for (int i = 0; i < instantiatCount; i++) {//实例化循环的item

GameObject gob = Instantiate (prefab, gridRect.transform) as GameObject;

prefabsList.Add (gob);

gob.GetComponent ().anchoredPosition = new Vector2 (0, -i * itemHeight);

gob.transform.FindChild ("Text").GetComponent ().text = "" + i;

gob.name = "item" + i;

botomIndex = instantiatCount - 1;

postionY = gridRect.anchoredPosition.y;

oldMoveIndex = Mathf.FloorToInt (Mathf.Abs(gridRect.anchoredPosition.y / itemHeight));//当前移动到了第几格

void OnScrollValueChanged (Vector2 vec)

float curPosY = gridRect.anchoredPosition.y;//获取grid对象y方向的位置

int curMoveIndex = Mathf.FloorToInt (Mathf.Abs(gridRect.anchoredPosition.y / itemHeight));

int offsetCount = Mathf.Abs (curMoveIndex - oldMoveIndex);

for (int i = 0; i < offsetCount; i++) {

if (curPosY > itemHeight && curPosY > postionY && botomIndex < itemCount-1) {//向上移动,判断控制顶部刷新位置,判断移动方向,判断控制底部刷新位置

upIndex++;//只有当上面一个对象完全看不到的时候才开始刷新

botomIndex++;

updateListviewPos (true);//更新item位置

} else if (curPosY > 0 && curPosY < postionY && (curPosY+scrollRectHeight) < (itemCount-1)*itemHeight) {// 向下移动

//只有当下面一个对象完全看不到的时候才开始刷新

upIndex--;

botomIndex--;

updateListviewPos (false);

oldMoveIndex = curMoveIndex;

postionY = curPosY;

void updateListviewPos (bool isMoveUp)

int tempIndex = 0;

if (isMoveUp) {//向上移动,把集合第一个对象移动的最后位置,调整该对象在父对象中的相对位置

GameObject gob = prefabsList [tempIndex];

prefabsList.RemoveAt (tempIndex);

prefabsList.Add (gob);

gob.GetComponent ().anchoredPosition = new Vector2 (0, -botomIndex * itemHeight);

gob.transform.FindChild ("Text").GetComponent ().text = "" + botomIndex;

} else {//向下移动,把集合最后一个对象移动的第一个位置,调整该对象在父对象中的相对位置

tempIndex = prefabsList.Count - 1;

GameObject gob = prefabsList [tempIndex];

prefabsList.RemoveAt (tempIndex);

prefabsList.Insert (0,gob);

gob.GetComponent ().anchoredPosition = new Vector2 (0, -upIndex * itemHeight);

gob.transform.FindChild ("Text").GetComponent ().text = "" + upIndex;

/*

void init(){

RectTransform prefabRect = prefab.GetComponent ();

prefabRect.pivot = new Vector2 (0.5f,1);

RectTransform gridRect = prefab.GetComponent ();

gridRect.pivot = new Vector2 (0.5f,1);

gridRec.anchoredPosition = new Vector2 (gridRec.anchoredPosition.x,0);

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值