Sliders滑块交互界面设计
1. 简介
点击参考教程链接 可产看原文教程
原理就是应用Oculus Integration包里面的OVRInputModule.cs脚本。
功能示意图:
为实现上述功能,共创建三个脚本:
- ControllerInfo
- ControllerPointer
- SetUITransformRay
2. 介绍
ControllerInfo 脚本
Controller info is just a class that makes sure that scripts always have the correct information.
可以理解为获得手柄信息,建立trackingSpace的连接
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 脚本
ControllerPointer draws a line from the controller. This represents the way the controller is pointing. Add this to a LineRenderer.
这个脚本的作用是使手柄射出一条射线,这样可以表示手柄正在指哪里。
注意,这个脚本是加在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 脚本
SetUITransformRay will automatically set the controller for the OVRInputModule ray. It is required as normally you have two controllers in your scene; one for left and one for right. See the full method below for more info on how to set this up. Add this component to your EventSystem that is generated when you add a canvas.
这个脚本的作用是利用OVRInputModule ray设置手柄,这个脚本需要加在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;
}
}
3. 操作步骤
Step 1) Scene Setup
Import the Oculus Integration pack. Drag the OVRCameraRig onto your scene. Drag a OVRTrackedRemote into the left and right hand anchors. Set each remote to left or right depending on the anchor.
- 导入Oculus Integration包
- 把OVRCameraRig拖入场景中
- 将OVRTrackedRemote 拖入到 lefthand anchor 和 right hand anchor
- 设定OVRTrackedRemote 控制对应left or right手柄
Step 2) Set up your canvas
设置Canvas
Step 3) Set up Eventsystem.
设置Eventsystem
Step 4) Set up correct objects
Create 3 objects;
- Controller Manager,
- Controller Pointer,
- OVRGazePointer.
For the OVRGazePointer, I just quickly went in the example scene for UI, Oculus\VR\Scenes, and prefabed the OVRGazePointer there.
On Controller Pointer, there is a LineRenderer. This has two points, doesn’t matter where. It uses world space. It has a width of 0.005.
Only having two points, and using world space is very important. This is because ControllerPointer the script relies on those two being set like that.