Guest User

UI_InfiniteScroll

a guest
May 8th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.18 KB | None | 0 0
  1. /// Credit Tomasz Schelenz
  2. /// Sourced from - https://bitbucket.org/ddreaper/unity-ui-extensions/issues/81/infinite-scrollrect
  3. /// Demo - https://www.youtube.com/watch?v=uVTV7Udx78k  - configures automatically.  - works in both vertical and horizontal (but not both at the same time)  - drag and drop  - can be initialized by code (in case you populate your scrollview content from code)
  4.  
  5. using System.Collections.Generic;
  6.  
  7. namespace UnityEngine.UI.Extensions
  8. {
  9.     /// <summary>
  10.     /// Infinite scroll view with automatic configuration
  11.     ///
  12.     /// Fields
  13.     /// - InitByUSer - in case your scrollrect is populated from code, you can explicitly Initialize the infinite scroll after your scroll is ready
  14.     /// by callin Init() method
  15.     ///
  16.     /// Notes
  17.     /// - doesn't work in both vertical and horizontal orientation at the same time.
  18.     /// - in order to work it disables layout components and size fitter if present(automatically)
  19.     ///
  20.     /// </summary>
  21.     [AddComponentMenu("UI/Extensions/UI Infinite Scroll")]
  22.     public class UI_InfiniteScroll : MonoBehaviour
  23.     {
  24.         //if true user will need to call Init() method manually (in case the contend of the scrollview is generated from code or requires special initialization)
  25.         [Tooltip("If false, will Init automatically, otherwise you need to call Init() method")]
  26.         public bool InitByUser = false;
  27.         private ScrollRect _scrollRect;
  28.         private ContentSizeFitter _contentSizeFitter;
  29.         private VerticalLayoutGroup _verticalLayoutGroup;
  30.         private HorizontalLayoutGroup _horizontalLayoutGroup;
  31.         private GridLayoutGroup _gridLayoutGroup;
  32.         private bool _isVertical = false;
  33.         private bool _isHorizontal = false;
  34.         private float _disableMarginX = 0;
  35.         private float _disableMarginY = 0;
  36.         private bool _hasDisabledGridComponents = false;
  37.         private List<RectTransform> items = new List<RectTransform>();
  38.         private Vector2 _newAnchoredPosition = Vector2.zero;
  39.         //TO DISABLE FLICKERING OBJECT WHEN SCROLL VIEW IS IDLE IN BETWEEN OBJECTS
  40.         private float _treshold = 100f;
  41.         private int _itemCount = 0;
  42.         private float _recordOffsetX = 0;
  43.         private float _recordOffsetY = 0;
  44.  
  45.         void Awake()
  46.         {
  47.             if (!InitByUser)
  48.                 Init();
  49.         }
  50.  
  51.  
  52.         public void DeleteParameters(){
  53.             items.Clear ();
  54.             _scrollRect.content.offsetMin = new Vector2 (0,0);
  55.             //_itemCount = _scrollRect.content.childCount;
  56.         }
  57.  
  58.         public void Init()
  59.         {
  60.             if (GetComponent<ScrollRect>() != null)
  61.             {
  62.                 _scrollRect = GetComponent<ScrollRect>();
  63.                 _scrollRect.onValueChanged.AddListener(OnScroll);
  64.                 //_scrollRect.movementType = ScrollRect.MovementType.Unrestricted;
  65.  
  66.                 for (int i = 0; i < _scrollRect.content.childCount; i++)
  67.                 {
  68.                     items.Add(_scrollRect.content.GetChild(i).GetComponent<RectTransform>());
  69.                 }
  70.                 if (_scrollRect.content.GetComponent<VerticalLayoutGroup>() != null)
  71.                 {
  72.                     _verticalLayoutGroup = _scrollRect.content.GetComponent<VerticalLayoutGroup>();
  73.                 }
  74.                 if (_scrollRect.content.GetComponent<HorizontalLayoutGroup>() != null)
  75.                 {
  76.                     _horizontalLayoutGroup = _scrollRect.content.GetComponent<HorizontalLayoutGroup>();
  77.                 }
  78.                 if (_scrollRect.content.GetComponent<GridLayoutGroup>() != null)
  79.                 {
  80.                     _gridLayoutGroup = _scrollRect.content.GetComponent<GridLayoutGroup>();
  81.                 }
  82.                 if (_scrollRect.content.GetComponent<ContentSizeFitter>() != null)
  83.                 {
  84.                     _contentSizeFitter = _scrollRect.content.GetComponent<ContentSizeFitter>();
  85.                 }
  86.  
  87.                 _isHorizontal = _scrollRect.horizontal;
  88.                 _isVertical = _scrollRect.vertical;
  89.  
  90.                 if (_isHorizontal && _isVertical)
  91.                 {
  92.                     Debug.LogError("UI_InfiniteScroll doesn't support scrolling in both directions, plase choose one direction (horizontal or vertical)");
  93.                 }
  94.  
  95.                 _itemCount = _scrollRect.content.childCount;
  96.             }
  97.             else
  98.             {
  99.                 Debug.LogError("UI_InfiniteScroll => No ScrollRect component found");
  100.             }
  101.         }
  102.  
  103.         void DisableGridComponents()
  104.         {
  105.             if (_isVertical)
  106.             {
  107.                 _recordOffsetY = items[0].GetComponent<RectTransform>().anchoredPosition.y - items[1].GetComponent<RectTransform>().anchoredPosition.y;
  108.                 _disableMarginY = _recordOffsetY * _itemCount / 2;// _scrollRect.GetComponent<RectTransform>().rect.height/2 + items[0].sizeDelta.y;
  109.             }
  110.             if (_isHorizontal)
  111.             {
  112.                 _recordOffsetX = items[1].GetComponent<RectTransform>().anchoredPosition.x - items[0].GetComponent<RectTransform>().anchoredPosition.x;
  113.                 _disableMarginX = _recordOffsetX * _itemCount / 2;//_scrollRect.GetComponent<RectTransform>().rect.width/2 + items[0].sizeDelta.x;
  114.             }
  115.  
  116.             if (_verticalLayoutGroup)
  117.             {
  118.                 _verticalLayoutGroup.enabled = false;
  119.             }
  120.             if (_horizontalLayoutGroup)
  121.             {
  122.                 _horizontalLayoutGroup.enabled = false;
  123.             }
  124.             if (_contentSizeFitter)
  125.             {
  126.                 _contentSizeFitter.enabled = false;
  127.             }
  128.             if (_gridLayoutGroup)
  129.             {
  130.                 _gridLayoutGroup.enabled = false;
  131.             }
  132.             _hasDisabledGridComponents = true;
  133.         }
  134.        
  135.         public void OnScroll(Vector2 pos)
  136.         {
  137.             if (!_hasDisabledGridComponents)
  138.                 DisableGridComponents();
  139.  
  140.             if (items.Count == 0)
  141.                 return;
  142.  
  143.             for (int i = 0; i < items.Count; i++)
  144.             {
  145.                 if (_isHorizontal)
  146.                 {
  147.                     if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).x > _disableMarginX + _treshold)
  148.                     {
  149.                         _newAnchoredPosition = items[i].anchoredPosition;
  150.                         _newAnchoredPosition.x -= _itemCount * _recordOffsetX;
  151.                         items[i].anchoredPosition = _newAnchoredPosition;
  152.                         _scrollRect.content.GetChild(_itemCount - 1).transform.SetAsFirstSibling();
  153.                     }
  154.                     else if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).x < -_disableMarginX)
  155.                     {
  156.                         _newAnchoredPosition = items[i].anchoredPosition;
  157.                         _newAnchoredPosition.x += _itemCount * _recordOffsetX;
  158.                         items[i].anchoredPosition = _newAnchoredPosition;
  159.                         _scrollRect.content.GetChild(0).transform.SetAsLastSibling();
  160.                     }
  161.                 }
  162.  
  163.                 if (_isVertical)
  164.                 {
  165.                     if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).y > _disableMarginY + _treshold)
  166.                     {
  167.                         _newAnchoredPosition = items[i].anchoredPosition;
  168.                         _newAnchoredPosition.y -= _itemCount * _recordOffsetY;
  169.                         items[i].anchoredPosition = _newAnchoredPosition;
  170.                         _scrollRect.content.GetChild(_itemCount - 1).transform.SetAsFirstSibling();
  171.                     }
  172.                     else if (_scrollRect.transform.InverseTransformPoint(items[i].gameObject.transform.position).y < -_disableMarginY)
  173.                     {
  174.                         _newAnchoredPosition = items[i].anchoredPosition;
  175.                         _newAnchoredPosition.y += _itemCount * _recordOffsetY;
  176.                         items[i].anchoredPosition = _newAnchoredPosition;
  177.                         _scrollRect.content.GetChild(0).transform.SetAsLastSibling();
  178.                     }
  179.                 }
  180.             }
  181.         }
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment