Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Runtime dynamic Scroll view : Unity tutorial

The default Scroll view in unity is fixed. If we add more items in the content gameobject of the UI canvas, then it does not adjust the size accordingly. So, the items which are not inside the content Object will not display inside the scroll view. If our items count is dynamic and if we want to add items to the scroll view content at the run-time, here’s the tutorial how to do it.

In the scene hierarchy right click and create a canvas.

Right click on the canvas and add a scroll view (Create -> UI -> ScrollView).

Expand scroll view.Add vertical layout group component to content game object and enter the spacing value.

Add Content size fitter component to the content game object. Set the Vertical Fit drop down to preferred size.

Right click on the content object and add any ui object of your requirement. Here we add image object. You can see the content object size is fixed.

For adding items at Runtime create a prefab of the item by dragging it into the project folder.Now delete any item under the content object because we will add them at runtime.

create a script and attach it to gameobject in the scene

public class UIScroll : MonoBehaviour
{
public Transform contentGO;
public GameObject itemPrefab;
public int itemsCount;

void Start()
{
    for(int i = 0; i 

}



This post first appeared on Intersting Android Game, please read the originial post: here

Share the post

Runtime dynamic Scroll view : Unity tutorial

×

Subscribe to Intersting Android Game

Get updates delivered right to your inbox!

Thank you for your subscription

×