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);
}
}
}