Unity HTC VIVE VR一体机基础操作
老规矩,直接上代码:
VIVE Focus 触摸板
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using wvr;
public class EventType_ZH : MonoBehaviour
{
WVR_Event_t _Event = new WVR_Event_t();//系统事件
//检测手柄
WaveVR_Controller.EDeviceType curFocusControllerType = WaveVR_Controller.EDeviceType.Dominant;
Transform _WaveVR;
private void Start()
{
_WaveVR = GameObject.Find("WaveVR").transform;
}
private void Update()
{
switch (_Event.common.type)
{
//当手指在触摸板上从左向右滑过时触发
case WVR_EventType.WVR_EventType_LeftToRightSwipe:
//向右走
_WaveVR.position = Vector3.right * Time.deltaTime;
break;
//当手指在触摸板上从右往左滑过时触发
case WVR_EventType.WVR_EventType_RightToLeftSwipe:
//向左走
_WaveVR.position = Vector3.left * Time.deltaTime;
break;
//当手纸在触摸板上从上到下滑过时触发
case WVR_EventType.WVR_EventType_UpToDownSwipe:
//向后走
_WaveVR.position = Vector3.back * Time.deltaTime;
break;
//当手纸在触摸板上从下到上滑过时触发
case WVR_EventType.WVR_EventType_DownToUpSwipe:
//向前走
_WaveVR.position = Vector3.forward * Time.deltaTime;
break;
default:
break;
}
//触摸板
if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_DPad_Up))
{
//向前走
_WaveVR.position = Vector3.forward * Time.deltaTime;
print("触摸板 上");
}
if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_DPad_Down))
{
//向后走
_WaveVR.position = Vector3.back * Time.deltaTime;
print("触摸板 下");
}
if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_DPad_Left))
{
//向左走
_WaveVR.position = Vector3.left * Time.deltaTime;
print("触摸板 左");
}
if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_DPad_Right))
{
//向右走
_WaveVR.position = Vector3.right * Time.deltaTime;
print("触摸板 右");
}
}
}
VIVE Focus 按键操作
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Video;
using WaveVR_Log;
using wvr;
using UnityEngine.UI;
public class Button_Color : MonoBehaviour/*, IPointerHoverHandler, IPointerExitHandler,IPointerEnterHandler,IPointerClickHandler*/
{
Material _Mater; //材质
VideoPlayer _Video; //视频
bool _bColor; //材质布尔
public Text _ButtonText;
//检测手柄
WaveVR_Controller.EDeviceType curFocusControllerType = WaveVR_Controller.EDeviceType.Dominant;
//检测按键方式
//WVR_DeviceType curFocusControllerType = WVR_DeviceType.WVR_DeviceType_Controller_Right;
void Start()
{
_Mater = GetComponent<Renderer>().material;
_Video = GameObject.Find("Video").GetComponent<VideoPlayer>();
}
void Update()
{
//如果按下了手柄的 Trigger 键
if (WaveVR_Controller.Input(curFocusControllerType).GetPressDown(WVR_InputId.WVR_InputId_Alias1_Trigger))
{
//ColorTrigger();
print("扳机键");
_ButtonText.text = "扳机键";
}
//如果按下了手柄的 菜单键
else if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_Menu))
{
print("菜单键");
_ButtonText.text = "菜单键";
}
//如果按下了手柄的 握柄键
else if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_Grip))
{
print("握柄键");
_ButtonText.text = "握柄键";
}
//如果按下了手柄的 系统键
else if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_System))
{
print("系统键");
_ButtonText.text = "系统键";
}
//如果按下了手柄的 触摸键
else if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_Touchpad))
{
print("触摸键");
// _ButtonText.text = "触摸键";
}
//如果持续按下了手柄的 Trigger键
else if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_Volume_Down))
{
print("持续按下Trigger键");
_ButtonText.text = "持续按下Trigger键";
// WaveVR_Controller.Input(curFocusControllerType).
}
//如果持续松开了手柄的 Trigger键
else if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_Volume_Up))
{
print("持续松开Trigger键");
_ButtonText.text = "持续松开Trigger键";
}
//触摸板
else if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_DPad_Up))
{
print("触摸板 上");
_ButtonText.text = "触摸板 上";
}
else if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_DPad_Down))
{
print("触摸板 下");
_ButtonText.text = "触摸板 下";
}
else if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_DPad_Left))
{
print("触摸板 左");
_ButtonText.text = "触摸板 左";
}
else if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_DPad_Right))
{
print("触摸板 右");
_ButtonText.text = "触摸板 右";
}
else if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_Digital_Trigger))
{
print("Digital_Trigger");
_ButtonText.text = "Digital_Trigger";
}
else if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_Enter))
{
print("Enter");
_ButtonText.text = "Enter";
}
else if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Alias1_Thumbstick))
{
print("Thumbstick");
_ButtonText.text = "Thumbstick";
}
else if (WaveVR_Controller.Input(curFocusControllerType).GetPress(WVR_InputId.WVR_InputId_Max))
{
print("Max");
_ButtonText.text = "Max";
}
}
public void OnClick_Button()
{
if (Input.GetMouseButtonDown(0))
{
_bColor = !_bColor;
RaycastHit _Hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out _Hit))
{
if (_Hit.collider.name == "Cube")
{
if (_bColor)
{
_Mater.color = Color.green;
}
else
{
_Mater.color = Color.yellow;
}
}
}
}
}
//颜色替换
public void ColorTrigger()
{
_bColor = !_bColor;
if (_bColor)
{
_Mater.color = Color.green;
}
else
{
_Mater.color = Color.yellow;
}
}
//视频播放
public void VideoPlayOnClick()
{
_Video.Play();
}
//视频停止
public void VideoStopOnClick()
{
_Video.Stop();
}
}
VIVE Focus 碰撞触发
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HTC.UnityPlugin.ColliderEvent;
public class VIVE_Collider_ZH : MonoBehaviour,
IColliderEventHoverEnterHandler,
IColliderEventHoverExitHandler,
IColliderEventPressEnterHandler,
IColliderEventPressUpHandler,
IColliderEventDragUpdateHandler,
IColliderEventAxisChangedHandler
{
public void OnColliderEventAxisChanged(ColliderAxisEventData eventData)
{
//当触发器进入物体
//如果是触摸板
if (eventData.axis==ColliderAxisEventData.InputAxis.Scroll2D)
{
//打印触摸版数值
print(eventData.axis);
}
//如果是Trigger 键
else if (eventData.axis == ColliderAxisEventData.InputAxis.Trigger1D)
{
//打印 Trigger 值
print(eventData.axis);
}
}
public void OnColliderEventDragUpdate(ColliderButtonEventData eventData)
{
//当触发器进入物体 并开始移动的时候触发
print("移动");
}
public void OnColliderEventHoverEnter(ColliderHoverEventData eventData)
{
//当触发器进入物体时 触发
print("进入");
}
public void OnColliderEventHoverExit(ColliderHoverEventData eventData)
{
//当触发器退出物体时 触发
print("退出");
}
public void OnColliderEventPressEnter(ColliderButtonEventData eventData)
{
//进入物体并按下按钮
print("按下");
}
public void OnColliderEventPressUp(ColliderButtonEventData eventData)
{
//进入物体并松开按钮
print("松开");
}
}
VIVE Focus 动态输出
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HTC.UnityPlugin.Vive;
using UnityEngine.UI;
public class VIVE_Input_ZH : MonoBehaviour
{
[Header("按钮")]
public Text _InputText;
[Header("Value_Trigger")]
public Text _InputValue;
[Header("Value_Pad_X")]
public Text _InpuPad_X;
[Header("Value_Pad_Y")]
public Text _InputPad_Y;
void Update()
{
if (ViveInput.GetPressDownEx(HandRole.RightHand, ControllerButton.Trigger))
{
//右手扳机键
_InputText.text = "右手扳机键";
}
if (ViveInput.GetPressDownEx(HandRole.RightHand, ControllerButton.Menu))
{
//菜单键
_InputText.text = "菜单键";
}
if (ViveInput.GetPressDownEx(HandRole.RightHand, ControllerButton.Grip))
{
//握柄键
_InputText.text = "握柄键";
}
if (ViveInput.GetPressDownEx(HandRole.RightHand, ControllerButton.DPadUp))
{
//触摸板 上
_InputText.text = "触摸板 上";
}
if (ViveInput.GetPressDownEx(HandRole.RightHand, ControllerButton.DPadDown))
{
//触摸板 上
_InputText.text = "触摸板 下";
}
if (ViveInput.GetPressDownEx(HandRole.RightHand, ControllerButton.DPadLeft))
{
//触摸板 上
_InputText.text = "触摸板 左";
}
if (ViveInput.GetPressDownEx(HandRole.RightHand, ControllerButton.DPadRight))
{
//触摸板 上
_InputText.text = "触摸板 右";
}
// 扳机程度
_InputValue.text = ViveInput.GetAxisEx(HandRole.RightHand, ControllerAxis.Trigger).ToString();
// 触摸板 X轴
_InpuPad_X.text = ViveInput.GetAxisEx(HandRole.RightHand, ControllerAxis.PadX).ToString();
// 触摸板 Y轴
_InputPad_Y.text = ViveInput.GetAxisEx(HandRole.RightHand, ControllerAxis.PadY).ToString();
}
}
VIVE Focus 指针操作
/// <summary>
/// 射线接口实现
/// </summary>
public class VIVE_Collider_ZH : MonoBehaviour,
IPointerHoverHandler,
IPointerEnterHandler,
IPointerClickHandler,
IPointerExitHandler
{
//指针悬停
public void OnPointerHover(PointerEventData eventData)
{
//GameObject.Find("Video").transform.localScale = Vector3.zero;
}
//指针退出
public void OnPointerExit(PointerEventData eventData)
{
//GameObject.Find("Video").transform.localScale = Vector3.one;
}
//指针进入
public void OnPointerEnter(PointerEventData eventData)
{
//GameObject.Find("Video").SetActive(false);
}
//指针触发
public void OnPointerClick(PointerEventData eventData)
{
//按下扳机键 惯用手(默认右手)
if (WaveVR_Controller.Input(WaveVR_Controller.EDeviceType.Dominant).GetPressDown(WVR_InputId.WVR_InputId_Alias1_Trigger))
{
print("右手确认!");
}
//按下扳机键 非惯用手(默认左手)
if (WaveVR_Controller.Input(WaveVR_Controller.EDeviceType.NonDominant).GetPressDown(WVR_InputId.WVR_InputId_Alias1_Trigger))
{
print("左手确认!");
}
}
}
暂时先这样吧,如果有时间的话就会更新,如果实在看不明白就留言,看到我会回复的。
路长远兮,与君共勉。