创建scrollview,设置滑动方向为垂直方向
在该对象上添加脚本scrollMove
在scrollView下面创建一个空的UI对象grid,不需要挂在gridLayout组件。
创建一个预质体,下面添加一个Text文本的子对象
cood:
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<GameObject> prefabsList = new List<GameObject> ();
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<RectTransform> ().sizeDelta.y;
scrollRect.onValueChanged = new ScrollRect.ScrollRectEvent ();
scrollRect.onValueChanged.AddListener (OnScrollValueChanged);
}
void initScrollview ()
{
itemHeight = prefab.GetComponent<RectTransform> ().sizeDelta.y + spacing.y;
gridRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, itemCount * itemHeight);
instantiatCount = Mathf.CeilToInt (scrollRect.GetComponent<RectTransform> ().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<RectTransform> ().anchoredPosition = new Vector2 (0, -i * itemHeight);
gob.transform.FindChild ("Text").GetComponent<Text> ().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<RectTransform> ().anchoredPosition = new Vector2 (0, -botomIndex * itemHeight);
gob.transform.FindChild ("Text").GetComponent<Text> ().text = "" + botomIndex;
} else {//向下移动,把集合最后一个对象移动的第一个位置,调整该对象在父对象中的相对位置
tempIndex = prefabsList.Count - 1;
GameObject gob = prefabsList [tempIndex];
prefabsList.RemoveAt (tempIndex);
prefabsList.Insert (0,gob);
gob.GetComponent<RectTransform> ().anchoredPosition = new Vector2 (0, -upIndex * itemHeight);
gob.transform.FindChild ("Text").GetComponent<Text> ().text = "" + upIndex;
}
}
/*
void init(){
RectTransform prefabRect = prefab.GetComponent<RectTransform> ();
prefabRect.pivot = new Vector2 (0.5f,1);
RectTransform gridRect = prefab.GetComponent<RectTransform> ();
gridRect.pivot = new Vector2 (0.5f,1);
gridRec.anchoredPosition = new Vector2 (gridRec.anchoredPosition.x,0);
}
*/
}
运行效果