1.设备的安装与配置 https://wenku.baidu.com/view/fa172fd7482fb4daa48d4b44.html?from=search
2.接入SDK、实现简单示例场景、更换手柄模型、实现手柄投掷功能
导入SteamVR Plugin.unitypackage
实现头盔观看功能
更换手柄模型
要点
把物体拖到model同级目录,坐标归零
在运行状态下调整模型的大小坐标和旋转值
复制组件value,然后取消运行,粘贴value到物体上
实现手柄投掷功能
按下trigger键在手柄上生成一个物体,松开trigger键投掷出去
3.手柄脚本讲解
手柄上各按键的输入
扳机键:trigger
菜单键(三横)menu:
侧边键(两个一样):grip
圆盘(可点可触摸):touchpad
using UnityEngine;
using System.Collections;
using Valve.VR;
using UnityEngine.iOS;
using System.Runtime.CompilerServices; public delegate void VRHandle(); public class VRInput : MonoBehaviour
{ SteamVR_TrackedObject trackedObject;//获取手柄对象
SteamVR_Controller.Device device;//定义手柄信息对象 public VRHandle onTrigger;
public VRHandle onTriggerDown;
public VRHandle onTriggerUp; void Start()
{
// 首先要获取响应按键的设备
trackedObject = GetComponent<SteamVR_TrackedObject>();
device = SteamVR_Controller.Input((int)trackedObject.index);
} Vector2 startPoint;
Vector2 endPoint;
//获取触摸偏移 ,旋转左手零件库,小部件什么的。
public float GetTouchPadDeltaPos(SteamVR_Controller.Device device)
{
if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis0))
{
endPoint = startPoint = device.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);
} //按下后滑动
if (device.GetTouch(SteamVR_Controller.ButtonMask.Axis0))
{ if ((device.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad) - endPoint).sqrMagnitude > 0.01f)
{ startPoint = endPoint; endPoint = device.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad); //Vector2 deltaVector = endPoint - startPoint;
//float deltaPosY = endPoint.y - startPoint.y; float deltaPosX = endPoint.x - startPoint.x; //使用delta
//device.TriggerHapticPulse (1000); // 用这个可以震动手柄
//target.Rotate (0, 0, deltaPosX * 100);
return deltaPosX;
}
}
return 0f;
} void Update()
{ // Press 和 Touch 除了圆盘,其他基本通用 Touch可以直接响应圆盘
// GetPressDown 和 GetTouchDown
// GetPress 和 GetTouch ___ 类似键盘的GetKey
// GetPressUp 和 GetTouchUp
//以下不全写了,没必要,就主要以GetTouch 试一下所有按钮 //按下圆盘 if (device.GetPress(SteamVR_Controller.ButtonMask.Touchpad))
{
//打印触摸的位置
print(device.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad));
} // Trigger键
if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("我是手柄上的trigger键,保持按下状态");
if (onTrigger!=null) {
onTrigger ();
} } if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("我是手柄上的trigger键,按下状态");
if (onTriggerDown!=null) {
onTriggerDown ();
} }
if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
{ Debug.Log("我是手柄上的trigger键,松开状态");
if (onTriggerUp!=null) {
onTriggerUp ();
}
} //触摸圆盘
if (device.GetTouch(SteamVR_Controller.ButtonMask.Touchpad))
{
Debug.Log("我是左手柄上的触摸屏,手指保持触摸状态");
} // 菜单键 (三横线)
if (device.GetTouch(SteamVR_Controller.ButtonMask.ApplicationMenu))
{
Debug.Log("我是手柄菜单键,保持按下状态");
} //手心键(随便取个名^^) 左右两边可以按但其实是一个键!
if (device.GetTouch(SteamVR_Controller.ButtonMask.Grip))
{
Debug.Log("我是左手柄左右侧按键,保持按下状态");
} // 这些键位都有了!! 好像还有个System键把!!能用吗? 经过短时间测试,不要用,没效果不说,容易导致其他的都不灵还要重新连接手柄!
//查看定义可以知道: public const ulong System= (1ul << (int)EVRButtonId.k_EButton_System); // reserved reserved了,reserved什么意思?保留! 不是留留给你的 ^ ^!
if (device.GetTouch(SteamVR_Controller.ButtonMask.System))
{
print("你看不见我!");
} //以下三个获取 追踪设备的姿态(矩阵等信息),按键的状态等等,请自行研究,欢迎回帖补充
device.GetPose();
device.GetState();
device.GetPrevState(); //还没完!继续
// vive 有个Trigger键,有一定键程,那么你按下这个trigger到什么程度会触发呢?
//经过测试,大概是 75%的样子!!(why?为什么要研究这个,有意义么? Of Course!)
// 因为 device.GetHairTrigger(), hairTrigger:一触即发的意思!就是说Trigger键你可以轻轻一碰就能触发!
if (device.GetHairTriggerDown())
{
print("看到我时,食指不要动,看你按到了多少?");
}
// 你肯定按了0.1,也就是10%的键程,我怎么知道?
//print("因为你按了:" + device.hairTriggerDelta);
// 那么,修改了hairTriggerDelta,是不是就修改了 一触即发的键程? √ //修改一触即发键程为一半,再按按试试看!
if (Input.GetKeyDown(KeyCode.A))
{
device.hairTriggerDelta = 0.5f;
} // 最后!
//Axis,轴
//经测试,Axis0对应的是圆盘SteamVR_Controller.ButtonMask.Touchpad ,都可以用
//Axis1,对应的是trigger键位!
// 在SteamVR_Controller.ButtonMask类里面定义了还有2,3,4Axis呢,它们对于啥呢?
//它们不对应啥,猜想一:这个是SteamVR!,又不是只给火腿肠vive专用,还有其他VR设备呢,如果其他设备手柄轴用的多呢?
// 二:Axis2,3,4能对应菜单键和手心键吗?no no no , 感受一下轴和按钮的区别就明白了,轴一般是范围值,button是yes or no; if (device.GetTouch(SteamVR_Controller.ButtonMask.Axis0))
{
Debug.Log("触摸盘");
}
if (device.GetTouch(SteamVR_Controller.ButtonMask.Axis1))
{
Debug.Log("trigger键");
}
if (device.GetTouch(SteamVR_Controller.ButtonMask.Axis2))
{ }
if (device.GetTouch(SteamVR_Controller.ButtonMask.Axis3))
{ }
if (device.GetTouch(SteamVR_Controller.ButtonMask.Axis4))
{ } } //把圆盘划成4个象限区域按钮,强行用圆盘变出一些按钮以防按钮不够用!
public enum TouchPadButton
{
Default,
UpLeft,
UpRight,
DowmLeft,
DownRight
} public TouchPadButton GetTouchButton()
{
TouchPadButton touchButton = TouchPadButton.Default;
var device = SteamVR_Controller.Input((int)trackedObject.index); if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis0))
{ Vector2 pad = device.GetAxis(EVRButtonId.k_EButton_SteamVR_Touchpad);
if (pad.x >= && pad.y >= )
{
touchButton = TouchPadButton.UpRight;
} if (pad.x >= && pad.y <= )
{
touchButton = TouchPadButton.DownRight;
} if (pad.x <= && pad.y >= )
{
touchButton = TouchPadButton.UpLeft;
} if (pad.x <= && pad.y <= )
{
touchButton = TouchPadButton.DowmLeft;
} }
return touchButton;
}
}
4.手柄圆盘键的自定义划分
将一个触摸板按照4象限划分成4个按键
通过坐标轴值来划分
将一个触摸板按照4象限旋转45度划分成4个按键
通过斜率值或角度来划分
5.UI交互
1.插件:Vive Input Utility.unitypackage 导入
2.文档
3DUI.docx
6.瞬移
1.插件:Vive - teleport.unitypackage 导入
2.使用文档
瞬移.docx