An optimized approach to lists with dozens of elements and a Pooling system
An optimized approach to lists with dozens of elements.
you can find a pratical example inside this repository in DynamicScrollScene scene
public class ExampleData { public int postId; public int id; public string name; public string email; public string body; }
DynamicScrollObjectand implement its abstract members (make sure to call
base.updateScrollObject(item, index);) and set the object width and height in
currentWidthand
currentHeight.
public class ExampleDynamicObject : DynamicScrollObject { 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().rect.height; currentWidth = GetComponent().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;
} }
public class ExampleScroll : MonoBehaviour { public DynamicScrollRect verticalScroll; public GameObject referenceObject;private DynamicScroll mVerticalDynamicScroll = new DynamicScroll();
public IEnumerator Start() { WWW www = new WWW(@"https://jsonplaceholder.typicode.com/comments"); yield return www; var data = JsonHelper.getJsonArray(www.text);
mVerticalDynamicScroll.spacing = 5f; mVerticalDynamicScroll.Initiate(verticalScroll, data, 0, referenceObject);
}
}
publicoverview
|name |type |description | |--|--|--| |
spacing|float |Value that represent the spacing between elements of the list | |
centralizeOnStop|bool |If the list should centralize the closest element to the center of the viewport after stop moving | |
objectPool|readonly Pooling < T1 > |The elements of the list | |
OnDragEvent|Action < Vector2 > |Event that triggers whenever the user scrolls the list, the parameter represent the velocity of the drag | |
OnBeginDragEvent|UnityEvent < PointerEventData > |Event that triggers in the first frame of dragging | |
OnEndDragEvent|UnityEvent < PointerEventData > |Event that triggers in the last frame of dragging |
dynamicScroll.Initiate- Description: Initiate the scroll rect withobjReferenceobjects applyinginfoListdata.
|name |type |description | |--|--|--| |
scrollRect|ScrollRect |a reference to the scroll rect | |
infoList|T[] |the list with the data information | |
startIndex|int |the item of index
startindexwill be the first element of the list | |
objReference|GameObject |a reference of the object that will be inside the list | |
createMoreIfNeeded|bool |if the list needs more itens, it will create more if
createMoreIfNeeded== true | |
forceAmount|int? |if setted, it will force
forceAmountobjects to be created at start |
dynamicScroll.ChangeList- Description: Change the current list of the scroll rect.
|name |type |description | |--|--|--| |
infoList|T[] |the list with the data information | |
startIndex|int |the item of index
startindexwill be the first element of the list. If -1, the current index will be setted. | |
resetContentPosition|bool |reset list position |
dynamicScroll.RefreshPosition- Description: Repaint the whole scroll rect. This is useful if any item inside the scroll rect changes the size (currentWidthandcurrentHeight).dynamicScroll.ToggleScroll- Description: Enable or Disable the ability to scroll the list.
|name |type |description | |--|--|--| |
active|bool |enable or Disable the ability to scroll the list |
dynamicScroll.CanMove- Description: Returns true if all directions send thro parameter are available.
|name |type |description | |--|--|--| |
directions|ScrollDirection |Enum flag with all the directions you want to know if are available |
dynamicScroll.MoveToIndex- Description: Tweens the content to centralize the object of index specified in the parameters.
|name |type |description | |--|--|--| |
i|int |Index of the element to be centralized | |
totalTime|float? |Total time to the animation happen (if you choose to input this value, the next one will be ignored) | |
timePerElement|float? |This value will be multiplied by the difference between the current centralized element and the target element to get the totalTime |
dynamicScroll.GetCentralizedObject- Description: Returns the closest element to the center of the viewport.dynamicScroll.GetLowest- Description: Returns the most left (if horizontal scroll) or most bottom (if vertical scroll) T1 object.dynamicScroll.GetHighest- Description: Returns the most right (if horizontal scroll) or most upper (if vertical scroll) T1 object.