Version: 2021.3+
This example demonstrates how to use styles to wrap content inside a scroll view. For demonstration purposes, this guide is for the Editor UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary. However, the instructions on styling the scroll view also apply to runtime UI.
This example creates a custom Editor window with two scroll views:
To wrap text of the label inside the scroll view, apply style to the Label control, and use a VisualElement to holder the label.
To wrap elements inside the scroll view, apply style to the content container of the scroll view.
You can find the completed files that this example creates in this GitHub repository.
This guide is for developers familiar with the Unity Editor, UI Toolkit, and C# scripting. Before you start, get familiar with the following:
To try the example, first create a custom Editor window with some default content.
ScrollViewExample
.ScrollViewExample.cs
, ScrollViewExample.uxml
, and ScrollViewExample.uss
.Define the basic scroll view structure in the UI Document (UXML file), style the visual elementsA node of a visual tree that instantiates or derives from the C# VisualElement
class. You can style the look, define the behaviour, and display it on screen as part of the UI. More info
See in Glossary in the USS file, and add 15 buttons inside the second scroll view in the C# script.
Replace the content of ScrollViewExample.uxml
with the following:
<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="True">
<Style src="ScrollViewExample.uss" />
<ui:ScrollView>
<ui:VisualElement>
<ui:Label text="ScrollView Wrapping Example" />
</ui:VisualElement>
</ui:ScrollView>
<ui:ScrollView name="scroll-view-wrap-example" />
</ui:UXML>
Replace the content of ScrollViewExample.uss
with the following:
Label {
font-size: 20px;
-unity-font-style: bold;
color: rgb(68, 138, 255);
/* Style to wrap text of the label */
white-space: normal;
}
/* Style to wrap elements inside the scroll view */
#scroll-view-wrap-example .unity-scroll-view__content-container {
flex-direction: row;
flex-wrap: wrap;
}
Button {
width: 50px;
height: 50px;
}
Replace the content of ScrollViewExample.cs
with the following:
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
public class ScrollViewExample : EditorWindow
{
[MenuItem("Example/ScrollView Wrapping Example")]
public static void ShowExample()
{
var wnd = GetWindow<ScrollViewExample>();
}
public void CreateGUI()
{
// Each editor window contains a root VisualElement object.
VisualElement root = rootVisualElement;
// Import UXML.
var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/ScrollViewExample.uxml");
VisualElement ScrollViewExample = visualTree.Instantiate();
root.Add(ScrollViewExample);
// Find the scroll view by name.
VisualElement scrollview = root.Query<ScrollView>("scroll-view-wrap-example");
// Add 15 buttons inside the scroll view.
for (int i = 0; i < 15; i++)
{
Button button = new Button();
button.text = "Button";
scrollview.Add(button);
}
}
}
To test the scroll view wrapping, from the menu, select Example > ScrollView Wrapping Example.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.