gpt4 book ai didi

c# - Unity VR 中的 slider

转载 作者:太空狗 更新时间:2023-10-30 01:29:38 27 4
gpt4 key购买 nike

我们正在为 Go 开发视频播放器应用。我们构建了一个简单的 raycaster 脚本来在用户指向 UI 按钮元素并拉动触发器时触发 onClick 事件:

bool triggerPulled = OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger);
if (Physics.Raycast(transform.position, transform.forward, out hit, 1000))
{

if ( triggerPulled )
{

// if we hit a button
Button button = hit.transform.gameObject.GetComponent<Button>();

if (button != null)
{
button.onClick.Invoke();
}

}
....
}

我们非常希望能够使用激光指示器和按钮来操纵 UI slider ,但不清楚是否存在我们可以触发的类似事件以实现适当的行为。我们可以调用 onValueChanged 来更改值,但这并不能真正为我们提供我们想要的滑动行为,只有在我们知道结束位置后才能设置新值。

有人对如何解决这个问题有好的想法吗?

最佳答案

Oculus Integration有一个名为 OVRInputModule.cs 的脚本。我相信这是为您正在尝试做的事情而设计的。为了做到这一点,有几个步骤。结果的 GIF。

enter image description here

为此,我将代码分成三个脚本; ControllerInfoControllerPointerSetUITransformRay

Controller 信息只是一个确保脚本始终具有正确信息的类。

using UnityEngine;
using static OVRInput;

public class ControllerInfo : MonoBehaviour {
[SerializeField]
private Transform trackingSpace;
public static Transform TRACKING_SPACE;
public static Controller CONTROLLER;
public static GameObject CONTROLLER_DATA_FOR_RAYS;

private void Start () {
TRACKING_SPACE = trackingSpace;
}

private void Update()
{
CONTROLLER = ((GetConnectedControllers() & (Controller.LTrackedRemote | Controller.RTrackedRemote) & Controller.LTrackedRemote) != Controller.None) ? Controller.LTrackedRemote : Controller.RTrackedRemote;
}
}

ControllerPointer 从 Controller 绘制一条线。这表示 Controller 指向的方式。将其添加到 LineRenderer

using UnityEngine;
using UnityEngine.EventSystems;
using static OVRInput;

[RequireComponent(typeof(LineRenderer))]
public class ControllerPointer : MonoBehaviour
{
[SerializeField]
private SetUITransformRay uiRays;
private LineRenderer pointerLine;
private GameObject tempPointerVals;

private void Start()
{
tempPointerVals = new GameObject();
tempPointerVals.transform.parent = transform;
tempPointerVals.name = "tempPointerVals";
pointerLine = gameObject.GetComponent<LineRenderer>();
pointerLine.useWorldSpace = true;

ControllerInfo.CONTROLLER_DATA_FOR_RAYS = tempPointerVals;
uiRays.SetUIRays();
}

private void LateUpdate()
{
Quaternion rotation = GetLocalControllerRotation(ControllerInfo.CONTROLLER);
Vector3 position = GetLocalControllerPosition(ControllerInfo.CONTROLLER);
Vector3 pointerOrigin = ControllerInfo.TRACKING_SPACE.position + position;
Vector3 pointerProjectedOrientation = ControllerInfo.TRACKING_SPACE.position + (rotation * Vector3.forward);
PointerEventData pointerData = new PointerEventData(EventSystem.current);
Vector3 pointerDrawStart = pointerOrigin - pointerProjectedOrientation * 0.05f;
Vector3 pointerDrawEnd = pointerOrigin + pointerProjectedOrientation * 500.0f;
pointerLine.SetPosition(0, pointerDrawStart);
pointerLine.SetPosition(1, pointerDrawEnd);

tempPointerVals.transform.position = pointerDrawStart;
tempPointerVals.transform.rotation = rotation;
}
}

SetUITransformRay 将自动为 OVRInputModule 射线设置 Controller 。这是必需的,因为通常您的场景中有两个 Controller ;一左一右。有关如何设置的更多信息,请参阅下面的完整方法。将此组件添加到添加 Canvas 时生成的 EventSystem

using UnityEngine;
using UnityEngine.EventSystems;

public class SetUITransformRay : MonoBehaviour
{
[SerializeField]
private OVRInputModule inputModule;
[SerializeField]
private OVRGazePointer gazePointer;

public void SetUIRays()
{
inputModule.rayTransform = ControllerInfo.CONTROLLER_DATA_FOR_RAYS.transform;
gazePointer.rayTransform = ControllerInfo.CONTROLLER_DATA_FOR_RAYS.transform;
}
}

使用步骤

第一步)场景设置

导入 Oculus 集成包。将 OVRCameraRig 拖到您的场景中。将 OVRTrackedRemote 拖入左右 anchor 。根据 anchor 将每个 Remote 设置为左侧或右侧。

enter image description here

第 2 步)设置 Canvas 和事件系统。

在 Canvas 上,

enter image description here

在事件系统上,

enter image description here

步骤 3) 设置正确的对象

创建3个对象; Controller ManagerController PointerOVRGazePointer

对于 OVRGazePointer,我只是快速进入 UI 的示例场景,Oculus\VR\Scenes,并在那里预制了 OVRGazePointer .

在 Controller Pointer 上,有一个 LineRenderer。这有两个点,不管在哪里。它使用世界空间。它的宽度为 0.005。

只有两个点,使用世界空间非常重要。这是因为 ControllerPointer 脚本依赖于这两个设置。

enter image description here

关于c# - Unity VR 中的 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50765762/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com