Unity Version - 2018.2
SteamVR plugin for Unity - v1.2.3
最近在重新学习htc眼镜,真的是忘完了,迷茫了好几天终于找到了一个线头,就是手柄,那就先从这个线头开始捋吧。
一、导入steamvr插件
直接从unity里边下载,好像现在最新的插件跟我现在用的这个有些不同,所以我这个应该是旧版本的教程。然后直接导入就行,这个就不截图了。
二、使用插件
把[CameraRig]直接拖入场景,把场景中原先有的摄像机删掉,避免冲突。
三、手柄事件的介绍
重点来了,介绍手柄的各个功能,写了一些常用的事件,感觉代码里写的挺详细的,在这里就不浪费时间描述了。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
[RequireComponent(typeof(SteamVR_TrackedObject))]
public class SLQHandGripController : MonoBehaviour
{
/// <summary>
/// 控制器
/// </summary>
private SteamVR_TrackedObject controller = null;
/// <summary>
/// 手柄
/// </summary>
private SteamVR_Controller.Device device = null;
public delegate void SLQDeviceAction();
public static event SLQDeviceAction SLQTriggerEvent = null;
public static event SLQDeviceAction SLQTriggerDownEvent = null;
public static event SLQDeviceAction SLQTriggerUpEvent = null;
public static event SLQDeviceAction SLQGripEvent = null;
public static event SLQDeviceAction SLQGripDownEvent = null;
public static event SLQDeviceAction SLQGripUpEvent = null;
public static event SLQDeviceAction SLQApplicationMenuEvent = null;
public static event SLQDeviceAction SLQApplicationMenuDownEvent = null;
public static event SLQDeviceAction SLQApplicationMenuUpEvent = null;
public static event SLQDeviceAction SLQTouchEvent = null;
public static event SLQDeviceAction SLQTouchDownEvent = null;
public static event SLQDeviceAction SLQTouchUpEvent = null;
public delegate void SLQDeviceActionWithData(Vector2 _vec2);
public static event SLQDeviceActionWithData SLQTouchDataEvent = null;
public bool IsSLQTrigger = false;
public bool IsSLQTriggerDown = false;
public bool IsSLQTriggerUp = false;
public bool IsSLQTouch = false;
public bool IsSLQTouchDown = false;
public bool IsSLQTouchUp = false;
public bool IsSLQGrip = false;
public bool IsSLQGripDown = false;
public bool IsSLQGripUp = false;
public bool IsSLQApplicationMenu = false;
public bool IsSLQApplicationMenuDown = false;
public bool IsSLQApplicationMenuUp = false;
public Vector2 TouchData = Vector2.zero;
// Use this for initialization
void Start()
{
controller = GetComponent<SteamVR_TrackedObject>();
}
// Update is called once per frame
void Update()
{
///初始化设备,要放在更新里
device = SteamVR_Controller.Input((int)controller.index);
#region 按下Hair Trigger(我的测试结果是没有区别)
if (device.GetHairTrigger())
{
//print("HairTriggerPress");
}
if (device.GetHairTriggerDown())
{
//print("HairTriggerPressDown");
}
else if (device.GetHairTriggerUp())
{
//print("HairTriggerPressUp");
}
if (device.GetPress(EVRButtonId.k_EButton_SteamVR_Trigger))
{
if (SLQTriggerEvent != null)
{
SLQTriggerEvent();
}
}
if (device.GetPressDown(EVRButtonId.k_EButton_SteamVR_Trigger))
{
if (SLQTriggerDownEvent != null)
{
SLQTriggerDownEvent();
}
//device.TriggerHapticPulse(30000); //手柄震动,第二个参数是手柄震动的区域,不写则全手柄震动
}
else if (device.GetPressUp(EVRButtonId.k_EButton_SteamVR_Trigger))
{
if (SLQTriggerUpEvent != null)
{
SLQTriggerUpEvent();
}
}
if (device.GetTouch(EVRButtonId.k_EButton_Axis1))
{
//print("TriggerPress");
}
if (device.GetTouchDown(EVRButtonId.k_EButton_Axis1))
{
//print("TriggerPressDown");
}
else if (device.GetTouchUp(EVRButtonId.k_EButton_Axis1))
{
//print("TriggerPressUp");
}
IsSLQTrigger = device.GetPress(EVRButtonId.k_EButton_SteamVR_Trigger);
IsSLQTriggerDown = device.GetPressDown(EVRButtonId.k_EButton_SteamVR_Trigger);
IsSLQTriggerUp = device.GetPressUp(EVRButtonId.k_EButton_SteamVR_Trigger);
#endregion
#region 按下Touch键
///按下后才会有数据返回
if (device.GetPress(EVRButtonId.k_EButton_SteamVR_Touchpad))
{
if (SLQTouchDataEvent != null)
{
SLQTouchDataEvent(device.GetAxis());
}
if (SLQTouchEvent != null)
{
SLQTouchEvent();
}
//print("TouchPress: "+device.GetAxis()); //获取坐标,中心点(0,0)。
}
if (device.GetPressDown(EVRButtonId.k_EButton_SteamVR_Touchpad))
{
if (SLQTouchDownEvent != null)
{
SLQTouchDownEvent();
}
//print("TouchPressDown");
}
else if (device.GetTouchUp(EVRButtonId.k_EButton_SteamVR_Touchpad))
{
if (SLQTouchUpEvent != null)
{
SLQTouchUpEvent();
}
//print("TouchPressUp");
}
///轻触即有数据返回
if (device.GetTouch(EVRButtonId.k_EButton_SteamVR_Touchpad))
{
//print("TouchPress");
}
if (device.GetTouchDown(EVRButtonId.k_EButton_SteamVR_Touchpad))
{
//print("TouchPressDown");
}
else if (device.GetTouchUp(EVRButtonId.k_EButton_SteamVR_Touchpad))
{
//print("TouchPressUp");
}
IsSLQTouch = device.GetPress(EVRButtonId.k_EButton_SteamVR_Touchpad);
IsSLQTouchDown = device.GetPressDown(EVRButtonId.k_EButton_SteamVR_Touchpad);
IsSLQTouchUp = device.GetTouchUp(EVRButtonId.k_EButton_SteamVR_Touchpad);
TouchData = device.GetAxis();
#endregion
#region 按下左右侧按键
if (device.GetPress(EVRButtonId.k_EButton_Grip))
{
if (SLQGripEvent != null)
{
SLQGripEvent();
}
}
if (device.GetPressDown(EVRButtonId.k_EButton_Grip))
{
if (SLQGripDownEvent != null)
{
SLQGripDownEvent();
}
}
else if (device.GetPressUp(EVRButtonId.k_EButton_Grip))
{
if (SLQGripUpEvent != null)
{
SLQGripUpEvent();
}
}
IsSLQGrip = device.GetPress(EVRButtonId.k_EButton_Grip);
IsSLQGripDown = device.GetPressDown(EVRButtonId.k_EButton_Grip);
IsSLQGripUp = device.GetPressUp(EVRButtonId.k_EButton_Grip);
#endregion
#region 按下顶部菜单键
if (device.GetPress(EVRButtonId.k_EButton_ApplicationMenu))
{
if (SLQApplicationMenuEvent != null)
{
SLQApplicationMenuEvent();
}
}
if (device.GetPressDown(EVRButtonId.k_EButton_ApplicationMenu))
{
if (SLQApplicationMenuDownEvent != null)
{
SLQApplicationMenuDownEvent();
}
}
else if (device.GetPressUp(EVRButtonId.k_EButton_ApplicationMenu))
{
if (SLQApplicationMenuUpEvent != null)
{
SLQApplicationMenuUpEvent();
}
}
IsSLQApplicationMenu = device.GetPress(EVRButtonId.k_EButton_ApplicationMenu);
IsSLQApplicationMenuDown = device.GetPressDown(EVRButtonId.k_EButton_ApplicationMenu);
IsSLQApplicationMenuUp = device.GetPressUp(EVRButtonId.k_EButton_ApplicationMenu);
#endregion
}
/// <summary>
/// 手柄震动
/// </summary>
/// <param name="_intensity">震动强度</param>
/// <param name="eVRButtonId">震动部位</param>
public void HandShakeMethod(ushort _intensity=500,EVRButtonId eVRButtonId=EVRButtonId.k_EButton_Axis0)
{
device.TriggerHapticPulse(_intensity,eVRButtonId);
}
}
参考资料1:https://www.jianshu.com/p/4268243fd06f
参考资料2:https://blog.csdn.net/weixin_36504157/article/details/80829404