Search code examples
mrtk

How is the PinchSlider meant to be configured?


I tried using the slider component for the first time and immediately some question came up:

  • How do I make the slider snap to the positions of the TickMarks?
    • If that's not possible, what are they for?
    • How do I change the amount of marks or remove all of them if I want to slider without stepping?
  • Why, when I change the Slider Track settings the visuals do not adjust?

I already see this going into a feature request ;)


Solution

  • These questions are probably best filed as issues on the MRTK github page. here's a link to file an issue.

    But, answering your questions here for future visitors:

    • How do I make the slider snap to the positions of the TickMarks?

    The default PinchSlider does not provide that functionality, but here is an example of how to do this by extending pinchslider (reference: issue 4140

    public class SliderWithSnapPoints : PinchSlider
        {
            [SerializeField]
            [Tooltip("The number of snap points")]
            float snapPoints = 100;
    
            float lastSnapPoint;
            float snapPointSize;
    
            public override void OnPointerDown(MixedRealityPointerEventData eventData)
            {
                base.OnPointerDown(eventData);
                if (eventData.used)
                {
                    lastSnapPoint = SliderValue;
                    snapPointSize = 1f / snapPoints;
                }
            }
    
            /// <summary>
            /// Handle slider value changes by dragging, and commit these changes.
            /// </summary>
            /// <remarks>
            /// Note, this requires the MRTK pinch slider to implement this function signature,
            /// and the pinch slider needs to call this function instead of instead setting SliderValue 
            /// directly.
            /// </remarks>
            protected override void OnPointerDragged(float newSliderValue)
            {
                var valueChange = Mathf.Abs(lastSnapPoint - newSliderValue);
                if (valueChange >= snapPointSize)
                {
                    lastSnapPoint = SliderValue = newSliderValue;
                }
            }
        }
    
    • How do I change the amount of marks or remove all of them if I want to slider without stepping?

    You can just delete or disable the tick marks in the scene hierarchy.

    • Why, when I change the Slider Track settings the visuals do not adjust?

    The visuals do not adjust because they are not tied to the number of snap points, they are just provided for visual guidance.