UGUI ScrollRect 無限循環(huán)

在做UI scrollrect的時(shí)候,有的時(shí)候因?yàn)閿?shù)據(jù)很多,要是每個(gè)單位的數(shù)據(jù)都做成一個(gè)scrollrect element的話,會造成無畏的內(nèi)存浪費(fèi),那些不顯示的element 其實(shí)沒有必要提前創(chuàng)建。
那么怎么解決這個(gè)問題呢?

我們可以做一個(gè)只能的scrollrect 控制器,用一個(gè)有限的scrollrect element組來顯示無限的scrollrect 數(shù)據(jù),只要這些scrollrect element RectTransform可以重新定位,及時(shí)更新顯示的數(shù)據(jù),那就可以用少量的scrollrect element RectTransform來模擬無限多的效果。

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


namespace LDFW.UI {

    [RequireComponent(typeof(ScrollRect))]
    public class InfiniteScrollRectController : MonoBehaviour {
        
        public List<int> _data;

        public ScrollRect _scrollRect;
        public RectTransform _scrollRectContent;
        public RectTransform _scrollRectElementParentContent;
        public int _scrollRectElementPreferredHeight = 100;
        
        private int _contentStartDataIndex;
        private int _contentEndDataIndex;
        private int _lastActiveIndex;
        private int _dataLength;
        private int _contentElementCount;

        private float _contentTopActionPosition;
        private float _contentBottomActionPosition;

        

        void Awake () {
            _scrollRect.onValueChanged.AddListener (OnScrollRectContentValueChanged);
        }

        void Start () {
            Test ();
        }

        void Test () {
            _data = new List<int> ();
            for (int i=0; i<100; i++) {
                _data.Add (i);
            }

            LoadData ();
        }

        public virtual void Reset () {        
            if (_scrollRect == null || _scrollRectContent == null || _scrollRectElementParentContent == null) {
                Debug.LogError ("Missing scroll rect info!");
                return;
            }

            _scrollRectContent.anchorMin = new Vector2 (0, 1);
            _scrollRectContent.anchorMax = new Vector2 (1, 1);
            _scrollRectContent.pivot = new Vector2 (0.5f, 1f);
            _scrollRectContent.sizeDelta = new Vector2 (_scrollRectContent.sizeDelta.x, _data.Count * _scrollRectElementPreferredHeight);

            _scrollRectElementParentContent.anchorMin = new Vector2 (0, 1);
            _scrollRectElementParentContent.anchorMax = new Vector2 (1, 1);
            _scrollRectElementParentContent.pivot = new Vector2 (0.5f, 1f);
            for (int i = 0; i < _scrollRectElementParentContent.childCount; i++) {
                if (_scrollRectElementParentContent.GetChild (i).GetComponent<LayoutElement> () == null) {
                    _scrollRectElementParentContent.GetChild (i).gameObject.AddComponent<LayoutElement> ();
                }
                _scrollRectElementParentContent.GetChild (i).GetComponent<LayoutElement> ().preferredHeight = _scrollRectElementPreferredHeight;
            }

            _contentTopActionPosition = _scrollRectElementPreferredHeight * 1.5f;
            _contentBottomActionPosition = _scrollRectElementPreferredHeight * (_scrollRectElementParentContent.childCount - 1.5f) - _scrollRect.viewport.rect.height;

            Debug.Log ("top and bottom = " + _contentTopActionPosition + ", " + _contentBottomActionPosition);

            _contentStartDataIndex = 0;
            _contentEndDataIndex = 0;
            _lastActiveIndex = 0;
            _dataLength = _data.Count;
            _contentElementCount = _scrollRectElementParentContent.childCount;
        }

        public virtual void LoadData () {
            Reset ();

            for (int i = 0; i < _contentElementCount; i++) {
                if (i >= _dataLength) {
                    _scrollRectElementParentContent.GetChild (i).gameObject.SetActive (false);
                } else {
                    _scrollRectElementParentContent.GetChild (i).gameObject.SetActive (true);
                    _contentStartDataIndex = 0;
                    _contentEndDataIndex = i;
                    _lastActiveIndex = i;
                    _scrollRectElementParentContent.GetChild (i).GetComponent<Text> ().text = "Index " + i;
                }
            }
        }

        public virtual void LoadDataIntoScrollRectElement (int dataIndex, int scrollRectElementIndex) {
            if (dataIndex < _dataLength && scrollRectElementIndex < _contentElementCount) {
                _scrollRectElementParentContent.GetChild (scrollRectElementIndex).GetComponent<Text> ().text = "Index = " + dataIndex;
            }
        }
        
        public virtual void OnScrollRectContentValueChanged (Vector2 scrollVector) {
            if (_scrollRectContent.anchoredPosition.y < _contentTopActionPosition) {
                ContentTopAction ();

            } else if (_scrollRectContent.anchoredPosition.y > _contentBottomActionPosition) {
                ContentBottomAction ();

            }
        }

        public virtual void ContentTopAction () {
            if (_lastActiveIndex >= _contentElementCount - 1) {
                if (_contentStartDataIndex > 0) {

                    _scrollRectElementParentContent.GetChild (_contentElementCount - 1).SetAsFirstSibling ();
                    _scrollRectElementParentContent.anchoredPosition += new Vector2 (0f, _scrollRectElementPreferredHeight);
                    _contentTopActionPosition -= _scrollRectElementPreferredHeight;
                    _contentBottomActionPosition -= _scrollRectElementPreferredHeight;

                    _contentStartDataIndex--;
                    _contentEndDataIndex--;
                    LoadDataIntoScrollRectElement (_contentStartDataIndex, 0);
                }
            }
        }

        public virtual void ContentBottomAction () {
            if (_lastActiveIndex >= _contentElementCount - 1) {
                if (_contentEndDataIndex < _dataLength - 1) {
                    
                    _scrollRectElementParentContent.GetChild (0).SetAsLastSibling ();
                    _scrollRectElementParentContent.anchoredPosition -= new Vector2 (0f, _scrollRectElementPreferredHeight);
                    _contentTopActionPosition += _scrollRectElementPreferredHeight;
                    _contentBottomActionPosition += _scrollRectElementPreferredHeight;


                    _contentStartDataIndex++;
                    _contentEndDataIndex++;
                    LoadDataIntoScrollRectElement (_contentEndDataIndex, _contentElementCount - 1);

                }
            }
        }
    }
}

在每一次更新位置的時(shí)候,我們通過程序來判斷當(dāng)前顯示的數(shù)據(jù)index,然后來判斷是否需要更新當(dāng)前的顯示數(shù)據(jù)以及是否需要更新當(dāng)前的element位置

下面是一些設(shè)置截圖


image.png

image.png

在上面的例子里,我們可以只有9個(gè)scrollrect element來顯示超過100個(gè)單位的數(shù)據(jù),做成有100個(gè)scrollrect element在滾動的效果

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容