Class Slider
Inherited Members
Namespace: UnityEngine.UI
Syntax
[AddComponentMenu("UI/Slider", 34)]
[ExecuteAlways]
[RequireComponent(typeof(RectTransform))]
public class Slider : Selectable, IMoveHandler, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IDeselectHandler, IDragHandler, IInitializePotentialDragHandler, IEventSystemHandler, ICanvasElement
Constructors
Slider()
Declaration
protected Slider()
Fields
m_Value
Declaration
[SerializeField]
protected float m_Value
Field Value
Type | Description |
---|---|
Single |
Properties
direction
The direction of the slider, from minimum to maximum value.
Declaration
public Slider.Direction direction { get; set; }
Property Value
Type | Description |
---|---|
Slider.Direction |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Example : MonoBehaviour
{
public Slider mainSlider;
public void Start()
{
//Changes the direction of the slider.
if (mainSlider.direction == Slider.Direction.BottomToTop)
{
mainSlider.direction = Slider.Direction.TopToBottom;
}
}
}
fillRect
Optional RectTransform to use as fill for the slider.
Declaration
public RectTransform fillRect { get; set; }
Property Value
Type | Description |
---|---|
RectTransform |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Example : MonoBehaviour
{
public Slider mainSlider;
//Reference to new "RectTransform"(Child of FillArea).
public RectTransform newFillRect;
//Deactivates the old FillRect and assigns a new one.
void Start()
{
mainSlider.fillRect.gameObject.SetActive(false);
mainSlider.fillRect = newFillRect;
}
}
handleRect
Optional RectTransform to use as a handle for the slider.
Declaration
public RectTransform handleRect { get; set; }
Property Value
Type | Description |
---|---|
RectTransform |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Example : MonoBehaviour
{
public Slider mainSlider;
//Reference to new "RectTransform" (Child of "Handle Slide Area").
public RectTransform handleHighlighted;
//Deactivates the old Handle, then assigns and enables the new one.
void Start()
{
mainSlider.handleRect.gameObject.SetActive(false);
mainSlider.handleRect = handleHighlighted;
mainSlider.handleRect.gameObject.SetActive(true);
}
}
maxValue
The maximum allowed value of the slider.
Declaration
public float maxValue { get; set; }
Property Value
Type | Description |
---|---|
Single |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Example : MonoBehaviour
{
public Slider mainSlider;
void Start()
{
// Changes the max value of the slider to 20;
mainSlider.maxValue = 20;
}
}
minValue
The minimum allowed value of the slider.
Declaration
public float minValue { get; set; }
Property Value
Type | Description |
---|---|
Single |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Example : MonoBehaviour
{
public Slider mainSlider;
void Start()
{
// Changes the minimum value of the slider to 10;
mainSlider.minValue = 10;
}
}
normalizedValue
The current value of the slider normalized into a value between 0 and 1.
Declaration
public float normalizedValue { get; set; }
Property Value
Type | Description |
---|---|
Single |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Example : MonoBehaviour
{
public Slider mainSlider;
//Set to invoke when "OnValueChanged" method is called.
void CheckNormalisedValue()
{
//Displays the normalised value of the slider everytime the value changes.
Debug.Log(mainSlider.normalizedValue);
}
}
onValueChanged
Callback executed when the value of the slider is changed.
Declaration
public Slider.SliderEvent onValueChanged { get; set; }
Property Value
Type | Description |
---|---|
Slider.SliderEvent |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Example : MonoBehaviour
{
public Slider mainSlider;
public void Start()
{
//Adds a listener to the main slider and invokes a method when the value changes.
mainSlider.onValueChanged.AddListener(delegate {ValueChangeCheck(); });
}
// Invoked when the value of the slider changes.
public void ValueChangeCheck()
{
Debug.Log(mainSlider.value);
}
}
value
The current value of the slider.
Declaration
public virtual float value { get; set; }
Property Value
Type | Description |
---|---|
Single |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Example : MonoBehaviour
{
public Slider mainSlider;
//Invoked when a submit button is clicked.
public void SubmitSliderSetting()
{
//Displays the value of the slider in the console.
Debug.Log(mainSlider.value);
}
}
wholeNumbers
Should the value only be allowed to be whole numbers?
Declaration
public bool wholeNumbers { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Example : MonoBehaviour
{
public Slider mainSlider;
public void Start()
{
//sets the slider's value to accept whole numbers only.
mainSlider.wholeNumbers = true;
}
}
Methods
FindSelectableOnDown()
See Selectable.FindSelectableOnDown
Declaration
public override Selectable FindSelectableOnDown()
Returns
Type | Description |
---|---|
Selectable |
Overrides
FindSelectableOnLeft()
See Selectable.FindSelectableOnLeft
Declaration
public override Selectable FindSelectableOnLeft()
Returns
Type | Description |
---|---|
Selectable |
Overrides
FindSelectableOnRight()
See Selectable.FindSelectableOnRight
Declaration
public override Selectable FindSelectableOnRight()
Returns
Type | Description |
---|---|
Selectable |
Overrides
FindSelectableOnUp()
See Selectable.FindSelectableOnUp
Declaration
public override Selectable FindSelectableOnUp()
Returns
Type | Description |
---|---|
Selectable |
Overrides
GraphicUpdateComplete()
See ICanvasElement.GraphicUpdateComplete
Declaration
public virtual void GraphicUpdateComplete()
Implements
LayoutComplete()
See ICanvasElement.LayoutComplete
Declaration
public virtual void LayoutComplete()
Implements
OnDidApplyAnimationProperties()
Declaration
protected override void OnDidApplyAnimationProperties()
Overrides
OnDisable()
Declaration
protected override void OnDisable()
Overrides
OnDrag(PointerEventData)
Declaration
public virtual void OnDrag(PointerEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
PointerEventData | eventData |
Implements
OnEnable()
Declaration
protected override void OnEnable()
Overrides
OnInitializePotentialDrag(PointerEventData)
Declaration
public virtual void OnInitializePotentialDrag(PointerEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
PointerEventData | eventData |
Implements
OnMove(AxisEventData)
Declaration
public override void OnMove(AxisEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
AxisEventData | eventData |
Overrides
OnPointerDown(PointerEventData)
Declaration
public override void OnPointerDown(PointerEventData eventData)
Parameters
Type | Name | Description |
---|---|---|
PointerEventData | eventData |
Overrides
OnRectTransformDimensionsChange()
Declaration
protected override void OnRectTransformDimensionsChange()
Overrides
Rebuild(CanvasUpdate)
Declaration
public virtual void Rebuild(CanvasUpdate executing)
Parameters
Type | Name | Description |
---|---|---|
CanvasUpdate | executing |
Implements
Set(Single, Boolean)
Set the value of the slider.
Declaration
protected virtual void Set(float input, bool sendCallback = true)
Parameters
Type | Name | Description |
---|---|---|
Single | input | The new value for the slider. |
Boolean | sendCallback | If the OnValueChanged callback should be invoked. |
Remarks
Process the input to ensure the value is between min and max value. If the input is different set the value and send the callback is required.
SetDirection(Slider.Direction, Boolean)
Sets the direction of this slider, optionally changing the layout as well.
Declaration
public void SetDirection(Slider.Direction direction, bool includeRectLayouts)
Parameters
Type | Name | Description |
---|---|---|
Slider.Direction | direction | The direction of the slider |
Boolean | includeRectLayouts | Should the layout be flipped together with the slider direction |
Examples
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Example : MonoBehaviour
{
public Slider mainSlider;
public void Start()
{
mainSlider.SetDirection(Slider.Direction.LeftToRight, false);
}
}
SetValueWithoutNotify(Single)
Set the value of the slider without invoking onValueChanged callback.
Declaration
public virtual void SetValueWithoutNotify(float input)
Parameters
Type | Name | Description |
---|---|---|
Single | input | The new value for the slider. |
Update()
Update the rect based on the delayed update visuals. Got around issue of calling sendMessage from onValidate.
Declaration
protected virtual void Update()