UGUI scrollView循环滚动

创建scrollview,设置滑动方向为垂直方向
在该对象上添加脚本scrollMove
UGUI scrollView循环滚动_第1张图片

在scrollView下面创建一个空的UI对象grid,不需要挂在gridLayout组件。
UGUI scrollView循环滚动_第2张图片

创建一个预质体,下面添加一个Text文本的子对象
UGUI scrollView循环滚动_第3张图片
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 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);
    }
*/
}

运行效果
UGUI scrollView循环滚动_第4张图片

demo下载:http://download.csdn.net/detail/u011484013/9842959

你可能感兴趣的:(unity-开发)