ScrollView Issues

I am trying to make use of a ScrollView in Runtime UI. I created 4 of them:

  • Horizontal, populated by code
  • Vertical, populated by code
  • Horizontal, populated manually
  • Vertical, populated manually

As you’ll see below, multiple issues come up:

  • Horizontal items get listed in vertical order. Am I doing something incorrectly?
  • The ScrollView “content-container” doesn’t grow in size with addition of new items (both manual and via code) and items are squeezed in the bounds. Are we supposed to manually tweak “content-container” VisualElement size (via code) in line with child size?

Here is roughly how it looks like in the UI Builder:

Here is how it looks like at runtime:

USS:

.horizontal-dark {
    width: 300px;
    height: 350px;
    background-color: rgb(68, 68, 68);
}

.horizontal-light {
    height: 300px;
    width: 300px;
    background-color: rgb(102, 102, 102);
}

.vertical-dark {
    width: 1050px;
    height: 150px;
    background-color: rgb(68, 68, 68);
}

.vertical-light {
    width: 1000px;
    height: 150px;
    background-color: rgb(102, 102, 102);
}

.full-width-scrollView {
    width: 100%;
    height: 400px;
}

UXML:

<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
    <ui:VisualElement name="contents" style="flex-grow: 1; background-color: rgb(221, 221, 221);">
        <Style src="test2.uss" />
        <ui:ScrollView name="scrollView-horizontal" mode="Horizontal" show-horizontal-scroller="true" class="full-width-scrollView" />
        <ui:ScrollView show-vertical-scroller="true" name="scrollView-vertical" class="full-width-scrollView" style="background-color: rgb(238, 238, 238);" />
        <ui:ScrollView mode="Horizontal" name="scrollView-horizontal-manual" show-horizontal-scroller="true" class="full-width-scrollView">
            <ui:VisualElement name="horizontal-dark" class="horizontal-dark" />
            <ui:VisualElement name="horizontal-light" class="horizontal-light" />
            <ui:VisualElement name="horizontal-dark" class="horizontal-dark" />
            <ui:VisualElement name="horizontal-light" class="horizontal-light" />
            <ui:VisualElement name="horizontal-dark" class="horizontal-dark" />
            <ui:VisualElement name="horizontal-light" class="horizontal-light" />
            <ui:VisualElement name="horizontal-dark" class="horizontal-dark" />
            <ui:VisualElement name="horizontal-light" class="horizontal-light" />
        </ui:ScrollView>
        <ui:ScrollView name="scrollView-vertical-manual" show-vertical-scroller="true" class="full-width-scrollView" style="background-color: rgb(238, 238, 238);">
            <ui:VisualElement name="vertical-dark" class="vertical-dark" />
            <ui:VisualElement name="vertical-light" class="vertical-light" />
            <ui:VisualElement name="vertical-dark" class="vertical-dark" />
            <ui:VisualElement name="vertical-light" class="vertical-light" />
            <ui:VisualElement name="vertical-dark" class="vertical-dark" />
            <ui:VisualElement name="vertical-light" class="vertical-light" />
            <ui:VisualElement name="vertical-dark" class="vertical-dark" />
            <ui:VisualElement name="vertical-light" class="vertical-light" />
        </ui:ScrollView>
    </ui:VisualElement>
</ui:UXML>

Controller MonoBehaviour:

using UnityEngine;
using UnityEngine.UIElements;

public class TestUIController : MonoBehaviour
{
    [SerializeField] private string horizontalDarkClass;
    [SerializeField] private string horizontalLightClass;
    [SerializeField] private string verticalDarkClass;
    [SerializeField] private string verticalLightClass;
    [SerializeField] private int itemCount; // 8

    [Space]
    [SerializeField] private UIDocument rootDocument;
  
    void Start()
    {
        var templateContainer = rootDocument.rootVisualElement.Q<TemplateContainer> (null, "unity-ui-document__root");
        templateContainer.style.flexGrow = 1;

        ScrollView horizontalScrollView = rootDocument.rootVisualElement.Q<ScrollView> ("scrollView-horizontal");
        for (int itemIndex = 0; itemIndex < itemCount; itemIndex++)
        {
            VisualElement newVisualElement = new VisualElement ();
            newVisualElement.AddToClassList (itemIndex % 2 == 0 ? horizontalDarkClass : horizontalLightClass);
            horizontalScrollView.Add (newVisualElement);
        }

        ScrollView verticalScrollView = rootDocument.rootVisualElement.Q<ScrollView> ("scrollView-vertical");
        for (int itemIndex = 0; itemIndex < itemCount; itemIndex++)
        {
            VisualElement newVisualElement = new VisualElement ();
            newVisualElement.AddToClassList (itemIndex % 2 == 0 ? verticalDarkClass : verticalLightClass);
            verticalScrollView.Add (newVisualElement);
        }
    }
}

You have bunch of scroll-list in the same container, how are you even supposed to define its behavior?
Separate them in their own VE container and apply proper behavior for these according to the content it will have.
Read about flexbox and how it behaves with dynamic content: A Complete Guide To Flexbox | CSS-Tricks

The same applies when we have just a single ScrollView. I put 4 of these to see a variety of these in action (horizontal & vertical, done via code & manual). Each ScrollView has a height of 400px, so they should not (and do not, as per Debugger) impact each other.

Sadly this has nothing to do with general flexbox concept. Flex-direction of the “content-container” becomes row, when you select Horizontal mode in ScrollView. So no reason for them to be stacked in a column.

First, can I assume what you see in the UI Builder is correct? It looks correct but I want to confirm it’s what you expect.

If so, the big question, have you changed the USS “Theme Style Sheet” on the PanelSettings asset used for runtime? You cannot change this field from “Default.uss” to anything else. It’s there because of a future feature (theming support) but right now it’s not meant to be changed.

2 Likes

Thanks for your input, Damian.

Yes, I’ve changed it and once you do that, it doesn’t show up in the list of options to revert back to. I found it in Packages (Packages/UI Toolkit/PackageResources/StyleSheers/Generated/Default.uss) and replaced back, after which it started working as expected.

Those bits work without any issues now. Cheers!

1 Like