Class ScrollRect
Implements
Inherited Members
Namespace: UnityEngine.UI
Assembly: UnityEngine.UI.dll
Syntax
[AddComponentMenu("UI/Scroll Rect", 37)]
[SelectionBase]
[ExecuteAlways]
[DisallowMultipleComponent]
[RequireComponent(typeof(RectTransform))]
public class ScrollRect : UIBehaviour, IInitializePotentialDragHandler, IBeginDragHandler, IEndDragHandler, IDragHandler, IScrollHandler, IEventSystemHandler, ICanvasElement, ILayoutElement, ILayoutGroup, ILayoutController
Constructors
ScrollRect()
Declaration
protected ScrollRect()
Fields
m_ContentBounds
Declaration
protected Bounds m_ContentBounds
Field Value
Type | Description |
---|---|
Bounds |
m_ContentStartPosition
Declaration
protected Vector2 m_ContentStartPosition
Field Value
Type | Description |
---|---|
Vector2 |
Properties
content
The content that can be scrolled. It should be a child of the GameObject with ScrollRect on it.
Declaration
public RectTransform content { get; set; }
Property Value
Type | Description |
---|---|
RectTransform |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
public ScrollRect myScrollRect;
public RectTransform scrollableContent;
//Do this when the Save button is selected.
public void Start()
{
// assigns the contect that can be scrolled using the ScrollRect.
myScrollRect.content = scrollableContent;
}
}
decelerationRate
The rate at which movement slows down.
Declaration
public float decelerationRate { get; set; }
Property Value
Type | Description |
---|---|
float |
Remarks
The deceleration rate is the speed reduction per second. A value of 0.5 halves the speed each second. The default is 0.135. The deceleration rate is only used when inertia is enabled.
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
public ScrollRect myScrollRect;
public void Start()
{
// assigns a new value to the decelerationRate of the scroll rect.
// The higher the number the longer it takes to decelerate.
myScrollRect.decelerationRate = 5.0f;
}
}
elasticity
The amount of elasticity to use when the content moves beyond the scroll rect.
Declaration
public float elasticity { get; set; }
Property Value
Type | Description |
---|---|
float |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ExampleClass : MonoBehaviour
{
public ScrollRect myScrollRect;
public void Start()
{
// assigns a new value to the elasticity of the scroll rect.
// The higher the number the longer it takes to snap back.
myScrollRect.elasticity = 3.0f;
}
}
flexibleHeight
Called by the layout system.
Declaration
public virtual float flexibleHeight { get; }
Property Value
Type | Description |
---|---|
float |
flexibleWidth
Called by the layout system.
Declaration
public virtual float flexibleWidth { get; }
Property Value
Type | Description |
---|---|
float |
horizontal
Should horizontal scrolling be enabled?
Declaration
public bool horizontal { get; set; }
Property Value
Type | Description |
---|---|
bool |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
public ScrollRect myScrollRect;
public void Start()
{
// Is horizontal scrolling enabled?
if (myScrollRect.horizontal == true)
{
Debug.Log("Horizontal Scrolling is Enabled!");
}
}
}
horizontalNormalizedPosition
The horizontal scroll position as a value between 0 and 1, with 0 being at the left.
Declaration
public float horizontalNormalizedPosition { get; set; }
Property Value
Type | Description |
---|---|
float |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
public ScrollRect myScrollRect;
public Scrollbar newScrollBar;
public void Start()
{
//Change the current horizontal scroll position.
myScrollRect.horizontalNormalizedPosition = 0.5f;
}
}
horizontalScrollbar
Optional Scrollbar object linked to the horizontal scrolling of the ScrollRect.
Declaration
public Scrollbar horizontalScrollbar { get; set; }
Property Value
Type | Description |
---|---|
Scrollbar |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
public ScrollRect myScrollRect;
public Scrollbar newScrollBar;
public void Start()
{
// Assigns a scroll bar element to the ScrollRect, allowing you to scroll in the horizontal axis.
myScrollRect.horizontalScrollbar = newScrollBar;
}
}
horizontalScrollbarSpacing
The space between the scrollbar and the viewport.
Declaration
public float horizontalScrollbarSpacing { get; set; }
Property Value
Type | Description |
---|---|
float |
horizontalScrollbarVisibility
The mode of visibility for the horizontal scrollbar.
Declaration
public ScrollRect.ScrollbarVisibility horizontalScrollbarVisibility { get; set; }
Property Value
Type | Description |
---|---|
ScrollRect.ScrollbarVisibility |
inertia
Should movement inertia be enabled?
Declaration
public bool inertia { get; set; }
Property Value
Type | Description |
---|---|
bool |
Remarks
Inertia means that the scrollrect content will keep scrolling for a while after being dragged. It gradually slows down according to the decelerationRate.
layoutPriority
Called by the layout system.
Declaration
public virtual int layoutPriority { get; }
Property Value
Type | Description |
---|---|
int |
minHeight
Called by the layout system.
Declaration
public virtual float minHeight { get; }
Property Value
Type | Description |
---|---|
float |
minWidth
Called by the layout system.
Declaration
public virtual float minWidth { get; }
Property Value
Type | Description |
---|---|
float |
movementType
The behavior to use when the content moves beyond the scroll rect.
Declaration
public ScrollRect.MovementType movementType { get; set; }
Property Value
Type | Description |
---|---|
ScrollRect.MovementType |
normalizedPosition
The scroll position as a Vector2 between (0,0) and (1,1) with (0,0) being the lower left corner.
Declaration
public Vector2 normalizedPosition { get; set; }
Property Value
Type | Description |
---|---|
Vector2 |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
public ScrollRect myScrollRect;
public Vector2 myPosition = new Vector2(0.5f, 0.5f);
public void Start()
{
//Change the current scroll position.
myScrollRect.normalizedPosition = myPosition;
}
}
onValueChanged
Callback executed when the position of the child changes.
Declaration
public ScrollRect.ScrollRectEvent onValueChanged { get; set; }
Property Value
Type | Description |
---|---|
ScrollRect.ScrollRectEvent |
Remarks
onValueChanged is used to watch for changes in the ScrollRect object. The onValueChanged call will use the UnityEvent.AddListener API to watch for changes. When changes happen script code provided by the user will be called. The UnityEvent.AddListener API for UI.ScrollRect._onValueChanged takes a Vector2.
Note: The editor allows the onValueChanged value to be set up manually.For example the value can be set to run only a runtime. The object and script function to call are also provided here.
The onValueChanged variable can be alternatively set-up at runtime.The script example below shows how this can be done.The script is attached to the ScrollRect object.
Examples
using UnityEngine;
using UnityEngine.UI;
public class ExampleScript : MonoBehaviour
{
static ScrollRect scrollRect;
void Start()
{
scrollRect = GetComponent<ScrollRect>();
scrollRect.onValueChanged.AddListener(ListenerMethod);
}
public void ListenerMethod(Vector2 value)
{
Debug.Log("ListenerMethod: " + value);
}
}
preferredHeight
Called by the layout system.
Declaration
public virtual float preferredHeight { get; }
Property Value
Type | Description |
---|---|
float |
preferredWidth
Called by the layout system.
Declaration
public virtual float preferredWidth { get; }
Property Value
Type | Description |
---|---|
float |
scrollSensitivity
The sensitivity to scroll wheel and track pad scroll events.
Declaration
public float scrollSensitivity { get; set; }
Property Value
Type | Description |
---|---|
float |
Remarks
Higher values indicate higher sensitivity.
velocity
The current velocity of the content.
Declaration
public Vector2 velocity { get; set; }
Property Value
Type | Description |
---|---|
Vector2 |
Remarks
The velocity is defined in units per second.
vertical
Should vertical scrolling be enabled?
Declaration
public bool vertical { get; set; }
Property Value
Type | Description |
---|---|
bool |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
public ScrollRect myScrollRect;
public void Start()
{
// Is Vertical scrolling enabled?
if (myScrollRect.vertical == true)
{
Debug.Log("Vertical Scrolling is Enabled!");
}
}
}
verticalNormalizedPosition
The vertical scroll position as a value between 0 and 1, with 0 being at the bottom.
Declaration
public float verticalNormalizedPosition { get; set; }
Property Value
Type | Description |
---|---|
float |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
public ScrollRect myScrollRect;
public Scrollbar newScrollBar;
public void Start()
{
//Change the current vertical scroll position.
myScrollRect.verticalNormalizedPosition = 0.5f;
}
}
verticalScrollbar
Optional Scrollbar object linked to the vertical scrolling of the ScrollRect.
Declaration
public Scrollbar verticalScrollbar { get; set; }
Property Value
Type | Description |
---|---|
Scrollbar |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
public ScrollRect myScrollRect;
public Scrollbar newScrollBar;
public void Start()
{
// Assigns a scroll bar element to the ScrollRect, allowing you to scroll in the vertical axis.
myScrollRect.verticalScrollbar = newScrollBar;
}
}
verticalScrollbarSpacing
The space between the scrollbar and the viewport.
Declaration
public float verticalScrollbarSpacing { get; set; }
Property Value
Type | Description |
---|---|
float |
verticalScrollbarVisibility
The mode of visibility for the vertical scrollbar.
Declaration
public ScrollRect.ScrollbarVisibility verticalScrollbarVisibility { get; set; }
Property Value
Type | Description |
---|---|
ScrollRect.ScrollbarVisibility |
viewRect
Declaration
protected RectTransform viewRect { get; }
Property Value
Type | Description |
---|---|
RectTransform |
viewport
Reference to the viewport RectTransform that is the parent of the content RectTransform.
Declaration
public RectTransform viewport { get; set; }
Property Value
Type | Description |
---|---|
RectTransform |
Methods
CalculateLayoutInputHorizontal()
Called by the layout system.
Declaration
public virtual void CalculateLayoutInputHorizontal()
CalculateLayoutInputVertical()
Called by the layout system.
Declaration
public virtual void CalculateLayoutInputVertical()
GraphicUpdateComplete()
Callback sent when this ICanvasElement has completed Graphic rebuild.
Declaration
public virtual void GraphicUpdateComplete()
IsActive()
See member in base class.
Declaration
public override bool IsActive()
Returns
Type | Description |
---|---|
bool |
Overrides
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class ExampleClass : MonoBehaviour
{
public ScrollRect myScrollRect;
public void Start()
{
//Checks if the ScrollRect called "myScrollRect" is active.
if (myScrollRect.IsActive())
{
Debug.Log("The Scroll Rect is active!");
}
}
}
LateUpdate()
Declaration
protected virtual void LateUpdate()
LayoutComplete()
Callback sent when this ICanvasElement has completed layout.
Declaration
public virtual void LayoutComplete()
OnBeginDrag(PointerEventData)
Handling for when the content is beging being dragged.
Declaration
public virtual void OnBeginDrag(PointerEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
PointerEventData | eventData |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems; // Required when using event data
public class ExampleClass : MonoBehaviour, IBeginDragHandler // required interface when using the OnBeginDrag method.
{
//Do this when the user starts dragging the element this script is attached to..
public void OnBeginDrag(PointerEventData data)
{
Debug.Log("They started dragging " + this.name);
}
}
OnDisable()
Declaration
protected override void OnDisable()
Overrides
OnDrag(PointerEventData)
Handling for when the content is dragged.
Declaration
public virtual void OnDrag(PointerEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
PointerEventData | eventData |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems; // Required when using event data
public class ExampleClass : MonoBehaviour, IDragHandler // required interface when using the OnDrag method.
{
//Do this while the user is dragging this UI Element.
public void OnDrag(PointerEventData data)
{
Debug.Log("Currently dragging " + this.name);
}
}
OnEnable()
Declaration
protected override void OnEnable()
Overrides
OnEndDrag(PointerEventData)
Handling for when the content has finished being dragged.
Declaration
public virtual void OnEndDrag(PointerEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
PointerEventData | eventData |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems; // Required when using event data
public class ExampleClass : MonoBehaviour, IEndDragHandler // required interface when using the OnEndDrag method.
{
//Do this when the user stops dragging this UI Element.
public void OnEndDrag(PointerEventData data)
{
Debug.Log("Stopped dragging " + this.name + "!");
}
}
OnInitializePotentialDrag(PointerEventData)
Called by a BaseInputModule when a drag has been found but before it is valid to begin the drag.
Declaration
public virtual void OnInitializePotentialDrag(PointerEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
PointerEventData | eventData |
OnRectTransformDimensionsChange()
This callback is called when the dimensions of an associated RectTransform change. It is always called before Awake, OnEnable, or Start. The call is also made to all child RectTransforms, regardless of whether their dimensions change (which depends on how they are anchored).
Declaration
protected override void OnRectTransformDimensionsChange()
Overrides
OnScroll(PointerEventData)
Use this callback to detect scroll events.
Declaration
public virtual void OnScroll(PointerEventData data)
Parameters
Type | Name | Description |
---|---|---|
PointerEventData | data |
OnValidate()
Declaration
protected override void OnValidate()
Overrides
Rebuild(CanvasUpdate)
Rebuilds the scroll rect data after initialization.
Declaration
public virtual void Rebuild(CanvasUpdate executing)
Parameters
Type | Name | Description |
---|---|---|
CanvasUpdate | executing | The current step in the rendering CanvasUpdate cycle. |
SetContentAnchoredPosition(Vector2)
Sets the anchored position of the content.
Declaration
protected virtual void SetContentAnchoredPosition(Vector2 position)
Parameters
Type | Name | Description |
---|---|---|
Vector2 | position |
SetDirty()
Override to alter or add to the code that keeps the appearance of the scroll rect synced with its data.
Declaration
protected void SetDirty()
SetDirtyCaching()
Override to alter or add to the code that caches data to avoid repeated heavy operations.
Declaration
protected void SetDirtyCaching()
SetLayoutHorizontal()
Called by the layout system.
Declaration
public virtual void SetLayoutHorizontal()
SetLayoutVertical()
Called by the layout system.
Declaration
public virtual void SetLayoutVertical()
SetNormalizedPosition(float, int)
Set the horizontal or vertical scroll position as a value between 0 and 1, with 0 being at the left or at the bottom.
Declaration
protected virtual void SetNormalizedPosition(float value, int axis)
Parameters
Type | Name | Description |
---|---|---|
float | value | The position to set, between 0 and 1. |
int | axis | The axis to set: 0 for horizontal, 1 for vertical. |
StopMovement()
Sets the velocity to zero on both axes so the content stops moving.
Declaration
public virtual void StopMovement()
UpdateBounds()
Calculate the bounds the ScrollRect should be using.
Declaration
protected void UpdateBounds()
UpdatePrevData()
Helper function to update the previous data fields on a ScrollRect. Call this before you change data in the ScrollRect.
Declaration
protected void UpdatePrevData()