如何配置PinchSlider?

时间:2019-11-22 08:08:52

标签: mrtk

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

  • 如何使滑块与TickMark的位置对齐?
    • 如果不可能的话,它们是干什么的?
    • 如果要滑动而不会步进,如何更改标记数量或删除所有标记?
  • 为什么当我更改“滑块轨道”设置时,视觉效果无法调整?

我已经看到这进入了功能请求;)

2 个答案:

答案 0 :(得分:1)

这些问题最好作为MRTK github页面上的问题提出。 here's a link to file an issue

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

  • 如何使滑块与TickMark的位置对齐?

默认的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;
            }
        }
    }
  • 如果要滑动而不会步进,如何更改标记数量或删除所有标记?

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

  • 为什么当我更改“滑块轨道”设置时,视觉效果无法调整?

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

答案 1 :(得分:0)

我稍微修改了此实现:

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;
}