简体   繁体   English

如何为 Unity VR 应用程序在 Update() 内设计平滑的 UI 滚动?

[英]How to design a smooth UI scroll inside Update() for a Unity VR application?

I am developing a VR application in Unity and I am struggling to develop a smooth UI scroll using my VR controller's joystick.我正在 Unity 中开发 VR 应用程序,并且正在努力使用 VR 控制器的操纵杆开发平滑的 UI 滚动。 So far what I have looks like this...到目前为止,我看起来像这样......

private void Update() 
{
    float joyStickDirection = globals.menuInteraction_Scroll.GetAxis(SteamVR_Input_Sources.Any).y; // this is either 1 for up or -1 for down
    if (joyStickDirection != 0) 
    {
        float multiplier = joyStickDirection * 5f; 
        scrollRect.verticalNormalizedPosition = scrollRect.verticalNormalizedPosition + (multiplier * Time.deltaTime);
    }
}

...this works, but has two problems. ...这有效,但有两个问题。 Firstly, it scrolls at different speeds depending how big the scrolling container is.首先,它根据滚动容器的大小以不同的速度滚动。 Secondly, the scrolling is not very smooth, as it is clearly just skipping varying gaps between 0 and 1.其次,滚动不是很流畅,因为它显然只是跳过了 0 和 1 之间的不同间隙。

I think I know what's wrong but I don't have enough experience working inside Update() to figure out the correct approach.我想我知道出了什么问题,但我没有足够的在 Update() 内部工作的经验来找出正确的方法。 Can anyone advise?任何人都可以建议吗?

Actually you don't necessarily go through the ScrollRect component itself.实际上,您不一定要通过ScrollRect组件本身。

I usually would simply do我通常会简单地做

public class ScrollExample : MonoBehaviour
{
    public float speed = 5f;

    public Transform ScrollContent;
    
    void Update()
    {
        // this is either 1 for up or -1 for down
        var joyStickDirection = globals.menuInteraction_Scroll.GetAxis(SteamVR_Input_Sources.Any).y; 
        if (joyStickDirection != 0) 
        {
            var multiplier = joyStickDirection * speed; 
            
            // You want to invert the direction since scrolling down actually means
            // moving the content up
            ScrollContent.position -= Vector3.up * multiplier * Time.deltaTime;
        }
    }
}

The ScrollRect then updates and handles the rest itself.然后ScrollRect更新并自行处理其余部分。 The speed is in Units/seconds or in a Screenspace Overlay canvas in pixels per seconds regardless of how big the content is.无论内容有多大, speed都以单位/秒或在屏幕空间叠加画布中的像素/秒为单位。

Usually you would want to adjust the elasticity of the ScrollRect or simply set the Movement Type to Clamped right away.通常你会想要调整 ScrollRect 的弹性,或者直接将移动类型设置为 Clamped。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM