unity 无限滚动排行榜

看博客无意间看到这个无限滚动条 排行榜系统 记录下来 如有侵权 请联系我
资源链接无限滚动排行榜

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

public class LoopScrollView : MonoBehaviour
{
    public GameObject prefab;


    /// <summary>
    /// 预设体item的y轴信息
    /// </summary>
    private float m_ItemSizeY;
    /// <summary>
    /// ScrollView 
    /// </summary>
    private ScrollRect m_scrollRect;

    /// <summary>
    /// 当前scorllview 能够显示的容量
    /// </summary>
    private int m_visibleCellsRowCount;

    private int m_visiblePreFirstIndex;
    private int m_visibleFirstIndex;
    /// <summary>
    /// 存贮多少个信息
    /// </summary>
    private IList allData;
    private LinkedList<GameObject> m_Cells;
    private int[] testData1 = new int[100];

    protected void Awake()
    {
        m_ItemSizeY = prefab.GetComponent<RectTransform>().sizeDelta.y;

        m_scrollRect = GetComponent<ScrollRect>();
        for (int i = 0; i < testData1.Length; i++)
        {
            testData1[i] = i;
        }
        InitWithData(testData1);
    }

    public void InitWithData(IList cellDataList)
    {
        allData = cellDataList;
        Init();
    }
    /// <summary>
    /// 计算可见的行数和Item数量;实例化并设置好初始位置;更新Content大小。
    /// </summary>
    public void Init()
    {

        m_visibleCellsRowCount = (int)(m_scrollRect.viewport.rect.height / m_ItemSizeY) + 1;
        m_Cells = new LinkedList<GameObject>();

        for (int i = 0; i < m_visibleCellsRowCount + 1; i++)
        {
            GameObject go = Instantiate(prefab, m_scrollRect.content.transform) as GameObject;
            go.SetActive(true);
            m_Cells.AddLast(go);
            SetCellPosition(go, i);
        }

        UpdateContentSize();

    }
    /// <summary>
    /// 按照序号设置Item的位置,并更新Data内容。
    /// </summary>
    /// <param name="go"></param>
    /// <param name="index"></param>
    void SetCellPosition(GameObject go, int index)
    {
        go.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -index * m_ItemSizeY);
        go.GetComponent<RankCell>().UpdateCellData(index + 1, allData.IndexOf(index).ToString(), allData.Count - index);
    }
    /// <summary>
    /// scorllView下 Content大小
    /// </summary>
    void UpdateContentSize()
    {
        int cellOneWayCount = (int)allData.Count;
        m_scrollRect.content.sizeDelta = new Vector2(m_scrollRect.content.sizeDelta.x, cellOneWayCount * m_ItemSizeY);
    }
    /// <summary>
    /// 当开始滑动时不断计算当前序号并更新Item
    /// </summary>
    public void OnvalueChange()
    {

        CalculateIndex();
        UpdateCells();

    }
    /// <summary>
    /// 计算当前可见的合理的最小序号
    /// </summary>
    void CalculateIndex()
    {
        m_visibleFirstIndex = (int)(m_scrollRect.content.anchoredPosition.y / m_ItemSizeY);
        m_visibleFirstIndex = Mathf.Clamp(m_visibleFirstIndex, 0, m_visibleFirstIndex - 1);
    }
    /// <summary>
    /// 根据序号更新Item数量
    /// </summary>
    void UpdateCells()
    {
        if (m_visiblePreFirstIndex != m_visibleFirstIndex)
        {
            bool scrollingPositive = m_visiblePreFirstIndex < m_visibleFirstIndex;
            int indexDelta = Mathf.Abs(m_visiblePreFirstIndex - m_visibleFirstIndex);

            int deltaSign = scrollingPositive ? +1 : -1;

            for (int i = 1; i <= indexDelta; i++)
                UpdateContent(m_visiblePreFirstIndex + i * deltaSign, scrollingPositive);

            m_visiblePreFirstIndex = m_visibleFirstIndex;
        }
    }
    /// <summary>
    /// 3滚动更新Item
    /// </summary>
    /// <param name="index"></param>
    /// <param name="scrollingPositive"></param>
    void UpdateContent(int index, bool scrollingPositive)
    {
        if (scrollingPositive)
        {
            LinkedListNode<GameObject> cell = m_Cells.First;
            cell.Value.gameObject.SetActive(false);
            m_Cells.RemoveFirst();
            SetCellPosition(cell.Value, index + m_visibleCellsRowCount);
            m_Cells.AddLast(cell);
            if (index + m_visibleCellsRowCount <= allData.Count - 1)
                cell.Value.gameObject.SetActive(true);
        }
        else
        {
            LinkedListNode<GameObject> cell = m_Cells.Last;
            cell.Value.gameObject.SetActive(false);
            m_Cells.RemoveLast();
            SetCellPosition(cell.Value, index);
            m_Cells.AddFirst(cell);
            if (index >= 0)
                cell.Value.gameObject.SetActive(true);
        }
    }
}

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

public class RankCellData
{
    public string name;
    public int score;

    public RankCellData(){}
}

public class RankCell : MonoBehaviour {

    public Text m_indexText;
    public Text m_nameText;
    public Text m_scoreText;
    private int m_dataIndex;

    public RankCellData Data = new RankCellData();

    public void UpdateCellData(int index, string name, int score)
    {
        m_dataIndex = index;
        Data.name = name;
        Data.score = score;

        m_indexText.text = m_dataIndex.ToString();
        m_nameText.text = Data.name;
        m_scoreText.text = Data.score.ToString();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值