< Return to Video

UI Scrollbar - Unity Official Tutorials

  • 0:02 - 0:06
    The scrollbar allows users to select a numeric value
  • 0:06 - 0:10
    from 0 to 1 by dragging the handle of the scrollbar.
  • 0:10 - 0:14
    Scrollbars are usually used to set or change a value
  • 0:14 - 0:16
    on another UI element or component.
  • 0:17 - 0:20
    Scrollbars are commonly used with a scroll rect,
  • 0:20 - 0:22
    which can hold and mask contents
  • 0:22 - 0:24
    too large to be easily viewed.
  • 0:24 - 0:27
    The scroll rect will use the value from the
  • 0:27 - 0:30
    scrollbar to control how it's contents are scrolled.
  • 0:31 - 0:33
    For more information on scroll rects
  • 0:33 - 0:35
    please see the information linked below.
  • 0:36 - 0:39
    The scrollbar's value is set by the position of
  • 0:39 - 0:41
    the handle along the length of the scrollbar.
  • 0:42 - 0:44
    The value is a normalised value that starts at
  • 0:44 - 0:47
    0 at one end of the scrollbar
  • 0:48 - 0:50
    and increases up to 1 at the other end.
  • 0:51 - 0:55
    A value of 0.5 indicates the halfway point.
  • 0:55 - 0:58
    To create a new scrollbar use the Create
  • 0:58 - 1:03
    menu to Create - UI - Scrollbar.
  • 1:05 - 1:07
    like all UI elements, the scrollbar element
  • 1:07 - 1:09
    must be the child of a canvas.
  • 1:10 - 1:12
    If one is not present when creating a pre-made
  • 1:12 - 1:15
    scrollbar a new canvas will be created.
  • 1:16 - 1:18
    For more information on the canvas
  • 1:18 - 1:20
    please see the information linked below.
  • 1:21 - 1:24
    When the scrollbar is created it is made from a
  • 1:24 - 1:26
    family of game objects.
  • 1:27 - 1:29
    The children are the rect transform and image components
  • 1:29 - 1:32
    that define the sliding area and the handle graphic.
  • 1:33 - 1:35
    The parent game object is where the required
  • 1:35 - 1:37
    scrollbar script resides.
  • 1:38 - 1:41
    The background image is a component on the parent game object.
  • 1:43 - 1:46
    Interact-able determines whether or not this particular
  • 1:46 - 1:48
    scrollbar will accept input.
  • 1:49 - 1:51
    To prevent interaction with a scrollbar
  • 1:51 - 1:53
    set Interact-able to false.
  • 1:54 - 1:56
    When interact-able is set to false
  • 1:56 - 1:58
    the transition state for the scrollbar element
  • 1:58 - 2:01
    will be set to the disabled state.
  • 2:01 - 2:04
    By default the scrollbar element will display
  • 2:04 - 2:06
    the normal state of the scrollbar.
  • 2:07 - 2:09
    Depending upon the transition mode chosen
  • 2:09 - 2:11
    a scrollbar can also display a highlighted
  • 2:11 - 2:13
    pressed and disabled state.
  • 2:14 - 2:16
    Transition Mode determines whether or not the
  • 2:16 - 2:20
    scrollbar element will display any of these additional states,
  • 2:21 - 2:24
    and if so controls the slider's look in each state
  • 2:24 - 2:27
    and the style of the transitions between the states.
  • 2:28 - 2:30
    The transition mode property is shared
  • 2:30 - 2:32
    by many UI elements and will be
  • 2:32 - 2:34
    covered in it's own lesson.
  • 2:34 - 2:36
    For more information on transitions and the
  • 2:36 - 2:38
    transition mode property please see the
  • 2:38 - 2:40
    information linked below.
  • 2:41 - 2:43
    When using transitions with a scrollbar
  • 2:43 - 2:45
    it is worth noting that, with the exception of
  • 2:45 - 2:48
    the animation transition mode,
  • 2:48 - 2:51
    any visual changes are only seen in the handle graphic
  • 2:51 - 2:53
    not the rest of the UI element.
  • 2:54 - 2:56
    Navigation describes how the selection
  • 2:56 - 2:58
    of UI elements is handled when using
  • 2:58 - 2:59
    keyboard or controller input.
  • 3:00 - 3:03
    Navigation is shared by many UI elements
  • 3:03 - 3:05
    and will be covered in it's own lesson.
  • 3:06 - 3:08
    For most circumstances navigation can simply
  • 3:08 - 3:10
    be set to automatic.
  • 3:11 - 3:13
    For more information on navigation
  • 3:13 - 3:16
    please see the information linked below.
  • 3:16 - 3:19
    Handle rect is a reference to the rect transform
  • 3:19 - 3:21
    on the graphic used for the handle.
  • 3:22 - 3:24
    Direction sets both the axis and
  • 3:24 - 3:26
    the direction of the value changed.
  • 3:27 - 3:30
    The value is a normalised value that starts with 0
  • 3:30 - 3:32
    at one end of the scrollbar and increasing
  • 3:32 - 3:34
    up to 1 at the other end.
  • 3:35 - 3:38
    For example, with the choice of left to right,
  • 3:38 - 3:42
    the starting value of 0 will be at the left of the scrollbar
  • 3:43 - 3:45
    and the value of the scrollbar will increase
  • 3:45 - 3:48
    proportionally as the handle moves from left to right
  • 3:48 - 3:50
    with a value of 1 at the right.
  • 3:51 - 3:53
    The choices for direction are
  • 3:54 - 3:55
    LeftToRight,
  • 3:55 - 3:57
    RightToLeft,
  • 3:57 - 4:00
    BottomToTop and TopToBottom.
  • 4:01 - 4:04
    The value is the current value of the scrollbar
  • 4:04 - 4:08
    When the value is set in the inspector during edit time
  • 4:08 - 4:11
    this will set the starting value for the scrollbar.
  • 4:12 - 4:14
    Size is the fractional size of the
  • 4:14 - 4:16
    handle within the scrollbar,
  • 4:16 - 4:18
    in a range of 0 to 1.
  • 4:19 - 4:22
    Number of steps is the number of distinct
  • 4:22 - 4:24
    positions allowed by the scrollbar.
  • 4:25 - 4:28
    For a smooth continuous scroll
  • 4:28 - 4:31
    set this value to 0 or 1.
  • 4:32 - 4:36
    The UI framework contains a powerful events system
  • 4:36 - 4:39
    This is created when the canvas is created.
  • 4:40 - 4:42
    When the event system detects a click
  • 4:42 - 4:44
    or a drag on the scrollbar
  • 4:44 - 4:47
    the event system will call an OnValueChanged event.
  • 4:48 - 4:50
    The scrollbar component includes a built-in
  • 4:50 - 4:52
    on value changed function list
  • 4:52 - 4:54
    that contains a list of all the functions
  • 4:54 - 4:57
    that are called when the scroll values change.
  • 4:58 - 5:00
    Additional events can be detected and used
  • 5:00 - 5:02
    by the scrollbar element , but this requires
  • 5:02 - 5:04
    an additional component.
  • 5:05 - 5:07
    For more information on how to use the build in
  • 5:07 - 5:12
    function's list, events, the event system and event triggers
  • 5:12 - 5:14
    please see the information linked below.
  • 5:15 - 5:17
    In most cases the scrollbar will
  • 5:17 - 5:19
    be used with the scroll rect.
  • 5:19 - 5:21
    which will automatically use the dynamic
  • 5:21 - 5:24
    value of the scrollbar when they are associated.
  • 5:25 - 5:27
    For more information on the scroll rect
  • 5:27 - 5:29
    please see the information linked below.
Title:
UI Scrollbar - Unity Official Tutorials
Description:

more » « less
Video Language:
English, British
Duration:
05:31

English subtitles

Revisions