alexsosnovskiy

UnityUI Fast infinity scroll

May 11th, 2016
1,565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. //
  2. // Coded by afrokick 2016
  3. //
  4. #pragma warning disable 0649
  5. using System;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using System.Collections;
  9.  
  10. public class FriendsPopup : MonoBehaviour
  11. {
  12.     private int ItemHeight = 100;
  13.     private const int Spacing = 8;
  14.     private const int Top = 16;
  15.     private const int Bottom = 50;
  16.  
  17.     [SerializeField]
  18.     private FriendItemView[] _views;
  19.  
  20.     [SerializeField]
  21.     private RectTransform _content;
  22.  
  23.     public event Action<int, FriendItemView> ItemShowed = delegate { };
  24.  
  25.     private int Count { get; set; }
  26.  
  27.     private float _y;
  28.     private int _oldInd = -1;
  29.     private RectTransform _item;
  30.  
  31.     void Update ()
  32.     {
  33.         _y = _content.anchoredPosition.y - Spacing;
  34.  
  35.         if (_y < 0)
  36.             return;
  37.        
  38.         var inx = Mathf.FloorToInt (_y / (ItemHeight + Spacing));
  39.  
  40.         if (_oldInd == inx)
  41.             return;
  42.        
  43.         //added to end
  44.         if (inx > _oldInd) {
  45.             var newInd = inx % _views.Length;
  46.  
  47.             newInd--;
  48.  
  49.             if (newInd < 0)
  50.                 newInd = _views.Length - 1;
  51.  
  52.             var id = inx + _views.Length - 1;
  53.  
  54.             if (id < Count) {
  55.                 _item = _views [newInd].GetComponent<RectTransform> ();
  56.  
  57.                 var pos = _item.anchoredPosition;
  58.  
  59.                 pos.y = -(Top + id * Spacing + id * ItemHeight);
  60.  
  61.                 _item.anchoredPosition = pos;
  62.  
  63.                 ItemShowed (id, _views [newInd]);
  64.             }
  65.         }
  66.         //added to begin
  67.         else {
  68.             var newInd = inx % _views.Length;
  69.  
  70.             _item = _views [newInd].GetComponent<RectTransform> ();
  71.  
  72.             var pos = _item.anchoredPosition;
  73.  
  74.             pos.y = -(Top + inx * Spacing + inx * ItemHeight);
  75.  
  76.             _item.anchoredPosition = pos;
  77.  
  78.             ItemShowed (inx, _views [newInd]);
  79.         }
  80.  
  81.         _oldInd = inx;
  82.     }
  83.  
  84.     public void SetData (int count)
  85.     {
  86.         _oldInd = 0;
  87.  
  88.         Count = count;
  89.  
  90.         var h = ItemHeight * count * 1f + Top + Bottom + (count == 0 ? 0 : ((count - 1) * Spacing));
  91.  
  92.         _content.sizeDelta = new Vector2 (_content.sizeDelta.x, h);
  93.  
  94.         var pos = _content.anchoredPosition;
  95.         pos.y = 0;
  96.         _content.anchoredPosition = pos;
  97.  
  98.         bool showed = false;
  99.  
  100.         var y = Top;
  101.  
  102.         for (int i = 0; i < _views.Length; i++) {
  103.             showed = i < count;
  104.  
  105.             _views [i].gameObject.SetActive (showed);
  106.  
  107.             if (showed) {
  108.                 pos = _views [i].GetComponent<RectTransform> ().anchoredPosition;
  109.                 pos.y = -y;
  110.                 _views [i].GetComponent<RectTransform> ().anchoredPosition = pos;
  111.  
  112.                 y += Spacing + ItemHeight;
  113.  
  114.                 ItemShowed (i, _views [i]);
  115.             }
  116.         }
  117.     }
  118. }
  119.  
  120. public class FriendItemView : MonoBehaviour
  121. {
  122.     [SerializeField]
  123.     private Text _nameText;
  124.  
  125.     public void SetData (string friendName)
  126.     {
  127.         _nameText.text = friendName;
  128.     }
  129. }
  130.  
  131. public class Controller : MonoBehaviour
  132. {
  133.     [SerializeField]
  134.     private FriendsPopup _popup;
  135.  
  136.     // Use this for initialization
  137.     void Start ()
  138.     {
  139.         _popup.ItemShowed += (index, view) => {
  140.             view.SetData ("item " + index);
  141.         };
  142.  
  143.         _popup.SetData (1000);
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment