FancyScrollView Documentation

repository·master·Indexed 25 days ago

https://github.com/setchi/fancyscrollview

A high-performance Unity component for custom scroll views featuring advanced animations and infinite scrolling support. It provides a framework using FancyCell<T> and FancyScrollView<T> to implement flexible scrolling behaviors, including normalized position-based cell animations and snapping via the Scroller class.

Tokens
1.2K
Snippets
3
Records
6
Agent score
36%

What's inside FancyScrollView

  1. Implement Infinite Scroll

    master

    To achieve infinite scrolling, combine the following settings:

    1. Enable Loop on the FancyScrollView component in the Inspector (this allows cells to wrap around).
    2. When using the Scroller class, set the Movement Type to Unrestricted to allow unbounded scrolling.

    Note: FancyScrollRect and FancyGridView do not support infinite scrolling.

  2. Implement a basic FancyScrollView

    master

    To implement a basic scroll view, you need three components: a data object, a cell class inheriting from FancyCell<T>, and a scroll view class inheriting from FancyScrollView<T>.

    // 1. Define the data object
    class ItemData
    {
        public string Message { get; }
    
        public ItemData(string message)
        {
            Message = message;
        }
    }
    
    // 2. Implement the cell
    using UnityEngine;
    using UnityEngine.UI;
    using FancyScrollView;
    
    class MyCell : FancyCell<ItemData>
    {
        [SerializeField] Text message = default;
    
        public override void UpdateContent(ItemData itemData)
        {
            message.text = itemData.Message;
        }
    
        public override void UpdatePosition(float position)
        {
            // position is a value from 0.0 to 1.0
            // Use this to control the visual appearance during scroll
        }
    }
    
    // 3. Implement the scroll view
    using UnityEngine;
    using System.Linq;
    using FancyScrollView;
    
    class MyScrollView : FancyScrollView<ItemData>
    {
        [SerializeField] Scroller scroller = default;
        [SerializeField] GameObject cellPrefab = default;
    
        protected override GameObject CellPrefab => cellPrefab;
    
        void Start()
        {
            scroller.OnValueChanged(base.UpdatePosition);
        }
    
        public void UpdateData(IList<ItemData> items)
        {
            base.UpdateContents(items);
            scroller.SetTotalCount(items.Count);
        }
    }
    
    // 4. Entry point to feed data
    using UnityEngine;
    using System.Linq;
    
    class EntryPoint : MonoBehaviour
    {
        [SerializeField] MyScrollView myScrollView = default;
    
        void Start()
        {
            var items = Enumerable.Range(0, 20)
                .Select(i => new ItemData($"Cell {i}"))
                .ToArray();
    
            myScrollView.UpdateData(items);
        }
    }
  3. Implement Cell Animations via UpdatePosition

    master
    FancyScrollView provides a normalized position value (from 0.0 to 1.0) to each cell during scroll updates. You can override UpdatePosition(float position) in your FancyCell class to implement custom animations (e.g., using Unity Animators or mathematical formulas) based on this value.
  4. Configure Scroller behavior and snapping

    master

    The Scroller class allows you to control scroll behavior, including inertia and deceleration rates. It also supports snapping to the nearest cell.

    Note: FancyScrollRect and FancyGridView do not support snapping.