Home

Awesome

UnityDynamicScrollRect

An optimized approach to lists with dozens of elements.

<p align="center"> <img src="https://github.com/Mukarillo/UnityDynamicScrollRect/blob/master/ReadmeAssets/dynamic_list_example.gif?raw=true" alt="Example"/> </p>

How to use

you can find a pratical example inside this repository in DynamicScrollScene scene

1 - Create a class to store all the information that each element of the list will need.

public class ExampleData
{
      public int postId;
      public int id;
      public string name;
      public string email;
      public string body;
}

2 - Create a class that extends DynamicScrollObject<ExampleData> and implement its abstract members (make sure to call base.updateScrollObject(item, index);) and set the object width and height in currentWidth and currentHeight.

public class ExampleDynamicObject : DynamicScrollObject<ExampleData>
{
  public override float currentHeight { get; set; }
  public override float currentWidth { get; set; }

  private Text idText;
  private Text nameEmailText;
  private Text bodyText;

  public void Awake()
  {
    currentHeight = GetComponent<RectTransform>().rect.height;
    currentWidth = GetComponent<RectTransform>().rect.width;

    idText = transform.Find("PostId").GetComponent<Text>();
    nameEmailText = transform.Find("NameEmail").GetComponent<Text>();
    bodyText = transform.Find("Body").GetComponent<Text>();         
  }

  public override void updateScrollObject(ExampleData item, int index)
  {
    base.updateScrollObject(item, index);

    idText.text = item.id.ToString();
    nameEmailText.text = string.Format("{0} ({1})", item.name, item.email);
    bodyText.text = item.body;
  }
}

3 - Create a class to initiate the DynamicList (use DynamicScrollRect instead of ScrollRect)

public class ExampleScroll : MonoBehaviour
{
  public DynamicScrollRect verticalScroll;
  public GameObject referenceObject;

  private DynamicScroll<ExampleData, ExampleDynamicObject> mVerticalDynamicScroll = new DynamicScroll<ExampleData, ExampleDynamicObject>();

  public IEnumerator Start()
  {
    WWW www = new WWW(@"https://jsonplaceholder.typicode.com/comments");
    yield return www;
    var data = JsonHelper.getJsonArray<ExampleData>(www.text);

    mVerticalDynamicScroll.spacing = 5f;
    mVerticalDynamicScroll.Initiate(verticalScroll, data, 0, referenceObject);
  }      
}

DynamicScroll<T, T1> public overview

Properties

nametypedescription
spacingfloatValue that represent the spacing between elements of the list
centralizeOnStopboolIf the list should centralize the closest element to the center of the viewport after stop moving
objectPoolreadonly Pooling < T1 >The elements of the list
OnDragEventAction < Vector2 >Event that triggers whenever the user scrolls the list, the parameter represent the velocity of the drag
OnBeginDragEventUnityEvent < PointerEventData >Event that triggers in the first frame of dragging
OnEndDragEventUnityEvent < PointerEventData >Event that triggers in the last frame of dragging

Methods

dynamicScroll.Initiate

nametypedescription
scrollRectScrollRecta reference to the scroll rect
infoListT[]the list with the data information
startIndexintthe item of index startindex will be the first element of the list
objReferenceGameObjecta reference of the object that will be inside the list
createMoreIfNeededboolif the list needs more itens, it will create more if createMoreIfNeeded == true
forceAmountint?if setted, it will force forceAmount objects to be created at start

dynamicScroll.ChangeList

nametypedescription
infoListT[]the list with the data information
startIndexintthe item of index startindex will be the first element of the list. If -1, the current index will be setted.
resetContentPositionboolreset list position

dynamicScroll.RefreshPosition

dynamicScroll.ToggleScroll

nametypedescription
activeboolenable or Disable the ability to scroll the list

dynamicScroll.CanMove

nametypedescription
directionsScrollDirectionEnum flag with all the directions you want to know if are available

dynamicScroll.MoveToIndex

nametypedescription
iintIndex of the element to be centralized
totalTimefloat?Total time to the animation happen (if you choose to input this value, the next one will be ignored)
timePerElementfloat?This value will be multiplied by the difference between the current centralized element and the target element to get the totalTime

dynamicScroll.GetCentralizedObject

dynamicScroll.GetLowest

dynamicScroll.GetHighest