Update 和 FixedUpdate 方法开头,每帧调用一次 OVRInput.Update() 和 OVRInput.FixedUpdate()。例如:public class XRInputManager : MonoBehaviour {
void Update() {
OVRInput.Update();
// Handle input logic here
}
void FixedUpdate() {
OVRInput.FixedUpdate();
// Handle physics-based input here
}
}
InputManager.asset 配置文件中引用这些绑定。Get():返回按钮、轴或触摸传感器的当前状态GetDown():返回该按键是否在本帧被按下GetUp():回该按键是否在本帧被松开if (OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger, OVRInput.Controller.RTouch)) {
// Trigger pressed on right controller
}
OVRManager.display.RecenterPose() 可将控制器与头戴设备姿势重置到当前位置。GetLocalControllerPosition():以 Vector3 返回位置GetLocalControllerRotation():以 Quaternion 返回朝向Primary:左控制器Secondary:右控制器Get() 变体,可访问不同类型的控制项,包括:| 控制项 | 枚举 |
|---|---|
OVRInput.Button | 游戏手柄、控制器上的传统按键及返回键。 |
OVRInput.Touch | 控制器上的电容感应触控面。 |
OVRInput.NearTouch | 控制器上的接近感应触控面。 |
OVRInput.Axis1D | 一维控制(如触发器),返回浮点状态。 |
OVRInput.Axis2D | 二维控制(如摇杆)。返回 Vector2 状态。 |
| 控制项 |
|---|
OVRInput.RawButton |
OVRInput.RawTouch |
OVRInput.RawNearTouch |
OVRInput.RawAxis1D |
OVRInput.RawAxis2D |
Touch),以及何时处于接近状态 (NearTouch)。这可精确检测用户与特定控制面的多种不同互动状态。例如,如果用户的食指完全离开控制面,该控制对应的 NearTouch 会报告为 false。当用户的手指靠近控制面并进入其近距离范围时,在用户做出物理接触之前,NearTouch 就会报告为 true。当用户与控制面发生物理接触时,该控制对应的 Touch 会报告为 true。当用户按下食指触发器时,该控制对应的 Button 会报告为 true。这些不同的状态可用于准确检测用户与控制器的互动,并支持多种控制方案。// returns true if the primary button (typically “A”) is currently pressed. OVRInput.Get(OVRInput.Button.One); // returns true if the primary button (typically “A”) was pressed this frame. OVRInput.GetDown(OVRInput.Button.One); // returns true if the “X” button was released this frame. OVRInput.GetUp(OVRInput.RawButton.X); // returns a Vector2 of the primary (typically the Left) thumbstick’s current state. // (X/Y range of -1.0f to 1.0f) OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick); // returns true if the primary thumbstick is currently pressed (clicked as a button) OVRInput.Get(OVRInput.Button.PrimaryThumbstick); // returns true if the primary thumbstick has been moved upwards more than halfway. // (Up/Down/Left/Right - Interpret the thumbstick as a D-pad). OVRInput.Get(OVRInput.Button.PrimaryThumbstickUp); // returns a float of the secondary (typically the Right) index finger trigger’s current state. // (range of 0.0f to 1.0f) OVRInput.Get(OVRInput.Axis1D.SecondaryIndexTrigger); // returns a float of the left index finger trigger’s current state. // (range of 0.0f to 1.0f) OVRInput.Get(OVRInput.RawAxis1D.LIndexTrigger); // returns true if the left index finger trigger has been pressed more than halfway. // (Interpret the trigger as a button). OVRInput.Get(OVRInput.RawButton.LIndexTrigger); // returns true if the secondary gamepad button, typically “B”, is currently touched by the user. OVRInput.Get(OVRInput.Touch.Two);
Get() 还接受一个可选控制器参数。支持的控制器列表见 OVRInput 的“枚举控制器”章节。Get() 未传入控制器参数,默认使用 Active(当前活动)控制器,即最近产生输入的控制器。例如,用户可能先使用一对控制器,将它们放下后,再拿起一个 Xbox 控制器,在这种情况下,一旦用户使用 Xbox 控制器进行输入,当前活动控制器就会切换为该 Xbox 控制器。当前的活动控制器可通过 OVRInput.GetActiveController() 查询,而所有已连接控制器的位掩码可通过 OVRInput.GetConnectedControllers() 查询。// returns a float of the hand trigger's current state on the left controller. OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, OVRInput.Controller.Touch); // returns a float of the hand trigger's current state on the right controller. OVRInput.Get(OVRInput.Axis1D.SecondaryHandTrigger, OVRInput.Controller.Touch);
OVRInput.Controller.Touch),也可单独指定(通过 OVRInput.Controller.LTouch 和 RTouch)。这一点很重要,因为指定 LTouch 或 RTouch 时会使用另一套不同的虚拟输入映射,这套映射能让开发者更方便地编写左右手无关的输入代码。PrimaryHandTrigger 来实现左右手无关的输入,其中 Primary 始终映射到所指定的手:// returns a float of the hand trigger's current state on the left controller. OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, OVRInput.Controller.LTouch); // returns a float of the hand trigger's current state on the right controller. OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, OVRInput.Controller.RTouch);
// public variable that can be set to LTouch or RTouch in the Unity Inspector public OVRInput.Controller controller; // returns a float of the hand trigger’s current state on the controller // specified by the controller variable. OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, controller); // returns true if the primary button (“A” or “X”) is pressed on the controller // specified by the controller variable. OVRInput.Get(OVRInput.Button.One, controller);
OVRInput.Controller.Touch 以组合方式访问控制器时,虚拟映射与左右手分离的标准游戏手柄布局高度一致。
OVRInput.Controller.LTouch 或 OVRInput.Controller.RTouch 单独访问左/右控制器时,虚拟映射会切换为左右手无关的输入绑定。例如:同一脚本可根据挂载的左右手动态查询对应控制器,Button.One 会自动映射为 A 键或 X 键。
