PinchSlider 是如何配置的?

How is the PinchSlider meant to be configured?

我第一次尝试使用滑块组件,立即出现了一些问题:

我已经在功能请求中看到了这一点 ;)

这些问题可能最好作为问题提交到 MRTK github 页面上。 here's a link to file an issue.

但是,在这里为未来的访客回答您的问题:

  • 如何使滑块与刻度线的位置对齐?

默认 PinchSlider 不提供该功能,但这里有一个示例,说明如何通过扩展 pinchslider(参考: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;
            }
        }
    }
  • 如果我想在不步进的情况下滑动,如何更改标记的数量或删除所有标记?

您可以只删除或禁用场景层次结构中的刻度线。

  • 为什么,当我更改 Slider Track 设置时,视觉效果没有调整?

视觉效果不会调整,因为它们与捕捉点的数量无关,它们只是提供视觉指导。

我稍微修改了这个实现:

using Microsoft.MixedReality.Toolkit.UI;
using UnityEngine;

public class SliderWithSnapPoints : PinchSlider
{
#pragma warning disable 649
    [Tooltip("The number of snap points")]
    [Min(2)]
    [SerializeField] private int snapPoints = 5;
#pragma warning restore 649

    /// <summary>
    /// Handle slider value changes by dragging, and commit snap point aligned 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 setting SliderValue 
    /// directly.
    /// </remarks>
    protected override void OnPointerDragged(float newSliderValue)
    {
        float gap = 1f / (snapPoints - 1f);
        newSliderValue = 0.5f * gap + newSliderValue;
        newSliderValue -= (newSliderValue % gap);

        SliderValue = newSliderValue;
    }
}



正如评论所建议的那样,您还必须从以下位置编辑主 PinchSlider 脚本:

public void OnPointerDragged(MixedRealityPointerEventData eventData)
{
    if (eventData.Pointer == activePointer && !eventData.used)
    {
        var delta = activePointer.Position - startPointerPosition;
        var handDelta = Vector3.Dot(SliderTrackDirection.normalized, delta);

        SliderValue = Mathf.Clamp(startSliderValue + handDelta / SliderTrackDirection.magnitude, 0, 1);

        // Mark the pointer data as used to prevent other behaviors from handling input events
        eventData.Use();
    }
}

收件人:

public void OnPointerDragged(MixedRealityPointerEventData eventData)
{
    if (eventData.Pointer == activePointer && !eventData.used)
    {
        var delta = activePointer.Position - startPointerPosition;
        var handDelta = Vector3.Dot(SliderTrackDirection.normalized, delta);

        OnPointerDragged(Mathf.Clamp(startSliderValue + handDelta / SliderTrackDirection.magnitude, 0, 1));

        // Mark the pointer data as used to prevent other behaviors from handling input events
        eventData.Use();
    }
}

protected virtual void OnPointerDragged(float newSliderValue)
{
    SliderValue = newSliderValue;
}