Oculus Integration 有一个名为OVRInputModule.cs 的脚本。我相信这是为您尝试做的事情而设计的。为了做到这一点,有几个步骤。结果的 GIF 动图。
为此,我将代码拆分为三个脚本; ControllerInfo、ControllerPointer 和 SetUITransformRay。
控制器信息只是一个确保脚本始终具有正确信息的类。
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 从控制器中画一条线。这表示控制器指向的方式。将此添加到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 射线设置控制器。通常,您的场景中有两个控制器是必需的;一左一右。有关如何设置的更多信息,请参阅下面的完整方法。将此组件添加到您添加画布时生成的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;
}
}
使用步骤
步骤 1) 场景设置
导入 Oculus 集成包。将OVRCameraRig 拖到您的场景中。将OVRTrackedRemote 拖入左右锚点。根据锚点将每个遥控器设置为向左或向右。
第 2 步)设置您的画布和事件系统。
在画布上,
在事件系统上,
步骤 3) 设置正确的对象
创建 3 个对象; Controller Manager、Controller Pointer 和 OVRGazePointer。
对于OVRGazePointer,我只是快速进入了 UI 的示例场景Oculus\VR\Scenes,并在那里预制了OVRGazePointer。
在控制器指针上,有一个LineRenderer。这有 两个 点,无论在哪里。它使用世界空间。它的宽度为 0.005。
只有两个点,使用世界空间非常重要。这是因为ControllerPointer 脚本依赖于这两个设置。