SliderInt
UnityEngine.UIElements.SliderInt
阅读时间3 分钟最后更新于 1 个月前
The SliderInt lets users select an integer value from a range of values. You can use a SliderInt to adjust a value, such as the number of lives in a game. To set the value, you drag a thumb along a track, or click the track itself. A slider can also include a text input field, where you can modify the value directly.
Create a SliderInt
You can create a SliderInt with UI Builder, UXML, or C#. The following C# example creates a SliderInt with a default value, low value, and high value, and sets the direction of the slider:
var sliderInt = new SliderInt();sliderInt.value = 42;sliderInt.lowValue = 0;sliderInt.highValue = 100;sliderInt.direction = SliderDirection.Horizontal;
Set page size
The SliderInt control has a page-size property that controls how much the thumb moves, and how much the value changes, when you click the track to either side of the thumb.
If the page size value is , clicking the track moves the thumb to the cursor or pointer position, and updates the value accordingly.
0If the page size value is greater than , clicking the track moves the thumb toward the cursor or pointer position by a specific amount. SliderInt controls treat page-size as a percentage of the total range.
0For example, if the total range is , and the page size is , then the page size amount is of , or . Each time you click the track, the thumb moves, and the value increases or decreases by .
2002020%2004040Keyboard Shortcuts
You can use keyboard shortcuts to navigate and adjust the SliderInt control. The following table lists the keyboard shortcuts:
Shortcut | Action |
|---|---|
| Arrow Keys | Move by |
| Arrow Keys + Shift | Move by |
| Home | Jump to the start of the slider. |
| End | Jump to the end of the slider. |
| Page Up | Move up by |
| Page Down | Move down by |
Example
The following UXML example creates a SliderInt:
<UXML xmlns="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements"> <SliderInt label="UXML Slider" name="the-uxml-slider" low-value="0" high-value="100" page-size="1" value="42" /></UXML>
The following USS example styles the SliderInt:
.some-styled-slider { -unity-font-style: bold;}
The following C# example illustrates some of the customizable functionalities of the SliderInt:
// Get a reference to the slider from UXML and assign it its value. var uxmlSlider = container.Q<SliderInt>("the-uxml-slider"); uxmlSlider.value = 42; // Create a new slider, disable it, and give it a style class. var csharpSlider = new SliderInt("C# Slider", 0, 100); csharpSlider.SetEnabled(false); csharpSlider.AddToClassList("some-styled-slider"); csharpSlider.value = uxmlSlider.value; container.Add(csharpSlider); // Mirror value of uxml slider into the C# field. uxmlSlider.RegisterCallback<ChangeEvent<int>>((evt) => { csharpSlider.value = evt.newValue; });
To try this example live in Unity, go to Window > UI Toolkit > Samples.
C# base class and namespace
C# class:
SliderIntNamespace:
UnityEngine.UIElementsBase class:
BaseSlider_1Member UXML attributes
This element has the following member attributes:
Name | Type | Description |
|---|---|---|
| | The value to add or remove to the SliderInt.value when it is clicked. This is casted to int. |
Inherited UXML attributes
This element inherits the following attributes from its base class:
Name | Type | Description |
|---|---|---|
| | Path of the target property to be bound. |
| | This is the actual property to contain the direction of the slider. |
| | Enables fill to set the color and shape of a slider. |
| | If false, this prevents the element from being focused. The element can only be focused if its canGrabFocus property is true. |
| | This is the maximum value that the slider encodes. |
| | This indicates whether or not this slider is inverted. For an inverted horizontal slider, high value is located to the left, low value is located to the right For an inverted vertical slider, high value is located to the bottom, low value is located to the top. |
| | The string representing the label that will appear beside the field. If the string is empty, the label element is removed from the hierarchy. If the string is not empty, the label element is added to the hierarchy. |
| | This is the minimum value that the slider encodes. |
| | Represents the value that should be applied to the calculated scroll offset while scrolling the slider, such as when clicking within the track or clicking the slider arrows. |
| | The visibility of the optional field inside the slider control. Set this property to true to display a numerical text field that provides another way to edit the slider value. |
| | An integer used to sort focusable elements in the focus ring. Must be greater than or equal to zero. Setting the |
| | The actual value of the slider. |
This element also inherits the following attributes from :
VisualElementName | Type | Description |
|---|---|---|
| | Logical container where child elements are added. If a child is added to this element, the child is added to this element's content container instead. When iterating over the If the content container is the same as the element itself, child elements are added directly to the element. This is true for most elements but can be overridden by more complex types. The |
| | Assigns a data source to this VisualElement which overrides any inherited data source. This data source is inherited by all children. |
| | Path from the data source to the value. |
| | The possible type of data source assignable to this VisualElement. This information is only used by the UI Builder as a hint to provide some completion to the data source path field when the effective data source cannot be specified at design time. |
| | Returns true if the This flag isn't changed if the VisualElement is disabled implicitly by one of its parents. To verify this, use |
| | Indicates the directionality of the element's text. The value will propagate to the element's children. Setting |
| | The name of this VisualElement. Use this property to write USS selectors that target a specific element. The standard practice is to give an element a unique name. |
| | Determines if this element can be the target of pointer events or picked by Elements can not be picked if: - They are invisible- Their Elements with a picking mode of |
| | Sets the style values on a The returned style data, computed from USS files or inline styles written to this object in C#, doesn't represent the fully resolved styles, such as the final height and width of a VisualElement. To access these fully resolved styles, use For information about how to use this property and all the supported USS properties, refer to the Apply styles in C# scripts and USS properties reference manual pages. |
| | Text to display inside an information box after the user hovers the element for a small amount of time. This is only supported in the Editor UI. |
| | A combination of hint values that specify high-level intended usage patterns for the |
| | Used for view data persistence, such as tree expanded states, scroll position, or zoom level. This key is used to save and load the view data from the view data store. If you don't set this key, the persistence is disabled for the associated |
USS classes
The following table lists all the C# public property names and their related USS selector.
C# property | USS selector | Description |
|---|---|---|
| | USS class name of elements of this type. |
| | USS class name of labels in elements of this type. |
| | USS class name of input elements in elements of this type. |
| | USS class name of elements of this type. |
| | USS class name of labels in elements of this type. |
| | USS class name of input elements in elements of this type. |
| | USS class name of elements of this type, when they are displayed horizontally. |
| | USS class name of elements of this type, when they are displayed vertically. |
| | USS class name of container elements in elements of this type. |
| | USS class name of tracker elements in elements of this type. |
| | USS class name of dragger elements in elements of this type. |
| | USS class name of the dragger border element in elements of this type. |
| | USS class name of fill element in elements of this type. |
| | USS class name on the dragger that indicates it is currently controlled by |
| | USS class name of elements of this type. |
| | USS class name of labels in elements of this type. |
| | USS class name of input elements in elements of this type. |
| | USS class name of elements of this type, when there is no label. |
| | USS class name of labels in elements of this type, when there is a dragger attached on them. |
| | USS class name of elements that show mixed values |
| | USS class name of elements that are aligned in a inspector element |
| | USS class name of local disabled elements. |
You can also use the Matching Selectors section in the Inspector or the UI Toolkit Debugger to see which USS selectors affect the components of the at every level of its hierarchy.
VisualElement