高性能的Unity3DScrollView组件,仅适用部分特定的滑块需求

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

/// <summary>
/// 高性能ScrollView
/// </summary>

[RequireComponent(typeof(ScrollRect))]
public class GridScroller : MonoBehaviour
{
    public enum Movement
    {
        Horizontal,
        Vertical
    }
    public delegate void MoveToEndCallback();
    public delegate void OnScrollViewItemChanged(Transform trans, int itemIdx);

    private List<Transform> _cacheList = new List<Transform>();
    private Vector2 _cellSize = Vector3.zero;
    private int _col;
    private HashSet<int> _currentItemIdxSet = new HashSet<int>();
    private Dictionary<int, RectTransform> _currentVisibleItemIdxRTMap = new Dictionary<int, RectTransform>();
    private RectTransform _grid;
    private bool _hasChanged;
    private Stack<RectTransform> _itemPool = new Stack<RectTransform>();
    private GameObject _itemPrefab;
    private Movement _moveType;
    private HashSet<int> _nextVisibleItemSet = new HashSet<int>();
    private OnScrollViewItemChanged _onNewItemBecomeVisibleCallback;
    private int _row;
    private ScrollRect _scroller;
    private Rect _scrollerRect;
    private Vector2 _spacing = Vector3.zero;
    private int _totalItemCount;
    private int _visibleItemCount;
    private float cachedPos;
    public MoveToEndCallback moveToEndCallBack;
    public bool moveToEndLock;

    public Vector2 ItemSize
    {
        get
        {
            return _spacing + _cellSize;
        }
    }
    public Movement MoveType
    {
        get
        {
            return _moveType;
        }
        set
        {
            _moveType = value;
        }
    }

    public RectTransform Grid
    {
        set
        {
            _grid = value;
        }
    }

    public GameObject ItemPrefab
    {
        set
        {
            _itemPrefab = value;
        }
    }

    private Vector2 CalcPosByIndex(int index)
    {
        if (_moveType == Movement.Horizontal)
        {
            return new Vector2(ItemSize.x * (index / _row), -ItemSize.y * (index % _row));
        }
        return new Vector2(ItemSize.x * (index % _col), -ItemSize.y * (index / _col));
    }

    private void CheckMoveToEnd()
    {
        if ((moveToEndCallBack != null) && !moveToEndLock)
        {
            bool flag = false;
            if (_moveType == Movement.Horizontal)
            {
                if (_grid.rect.width - _scrollerRect.width > 0 && Math.Abs(_grid.anchoredPosition.x) + _scrollerRect.width > _grid.rect.width)
                {
                    flag = true;
                }
            }
            else if (_grid.rect.height - _scrollerRect.height > 0 && Math.Abs(_grid.anchoredPosition.y) + _scrollerRect.height > _grid.rect.height)
            {
                flag = true;
            }
            if (flag)
            {
                moveToEndCallBack();
            }
        }
    }

    public void Clear()
    {
        var enumerator = _currentVisibleItemIdxRTMap.GetEnumerator();
        while (enumerator.MoveNext())
        {
            KeyValuePair<int, RectTransform> current = enumerator.Current;
            current.Value.gameObject.Set

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值