Unity -VIVE 开发第一弹
来源:互联网 发布:手机听书软件下载 编辑:程序博客网 时间:2024/06/04 01:08
vive 开发之监听手柄
*进行unityVR 开发的时候必定会用到SteamVR插件。SteamVR已经实现了全部的手柄接口。
很显然,我们需要做实际项目的过程中为了能更好的嵌入项目中,不可避免的需要重写监听脚本。
首先提供三种别人的简易监听脚本,之后提供一下自己用到的监听脚本同大家共勉。*
- 第一种简易的调用–一定要挂载在手柄上才有效(具体原因稍后细说)
using UnityEngine;using System.collections;public class ViveConFirst:MonoBehaviour{ private SteamVR_TrackedObject trackedObject; private SteamVR_Controller.Device device; void Start(){ trackedObject=GetComponent<SteamVR_TrackedObject>(); } void Update(){ device=SteamVR_Controller.Input((int)trackedObject.index); if(device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)){ Debug.log("按钮按下") } //.......同样的方法进行省略 }}
2.第二种简易调用–直接调用SteamVR 给出的事件
using UnityEngine;using System.collections;public class ViveConSecond:MonoBehaviour{ private SteamVR_Controller device; void Start(){ device=GetComponent<SteamVR_Controller>(); device.TriggerCliked+=Trigger; } void Trigger(object sender,ClickedEventArgs e){ DeBug.log("测试调用扳机事件"); }}
3.第三种简易调用 同第一调用方法类似
using UnityEngine;using System.collections;using Valve.VR;public class ViveConThird:MonoBehaviour{ private Valve.VR.EVRButtonId triggerButton=Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger; private SteamVR_TrackedObject trackedObject; private SteamVR_Controller.Device device; void Start(){ trackedObject=GetComponent<SteamVR_TrackedObject>(); } void Update(){ device=SteamVR_Controller.Input((int)trackedObject.index); if(device.GetPressDown(triggerButton){ Debug.log("按钮按下") } }}
具体原因SteamVR 的与Unity 之间存在一定的语法差异,不能用到类似GameObject.Find(类似)查找函数
具体差异请看API
附上封装的的监听函数–(欢迎大家交换意见)
using System.Collections;using System.Collections.Generic;using UnityEngine;using Valve.VR;public class viveEventTest : MonoBehaviour { public enum ViveInput { Grip, Trigger, TouchPad, None }; public SteamVR_TrackedObject trackedObject; /// <summary> /// 抓住 /// </summary> public ViveInput grab = ViveInput.Grip; /// <summary> /// 互动 /// </summary> public ViveInput interact = ViveInput.Trigger; /// <summary> /// 最大震动强度 /// </summary> private float MAX_VIBRATION_STRENGTH=3999f; public bool Pressed(string action) { ulong rawInput = ConvertString(action); if (Device() == null) { return false; } return Device().GetPressDown(rawInput); } public bool Released(string action) { ulong rawInput = ConvertString(action); if (Device() == null) { return false; } return Device().GetPressUp(rawInput); } public bool Holding(string action) { ulong rawInput = ConvertString(action); if (Device() == null) { return false; } return Device().GetPress(rawInput); } ulong ConvertString(string action) { ViveInput input = GetInputFor(action); return Decode(input); } public SteamVR_Controller.Device Device() { if (trackedObject.index == SteamVR_TrackedObject.EIndex.None) { return null; } return SteamVR_Controller.Input((int)trackedObject.index); } ViveInput GetInputFor(string action) { switch (action.ToLower()) { default: case "grab": return grab; case "interact": return interact; } } ulong Decode(ViveInput input) { switch ((int)input) { case 0: return SteamVR_Controller.ButtonMask.Grip; case 1: return SteamVR_Controller.ButtonMask.Trigger; case 2: return SteamVR_Controller.ButtonMask.Touchpad; default: case 3: return (1ul << (int)EVRButtonId.k_EButton_Max + 1); ////.................... } } // strength is a value from 0-1 /// <summary> /// 控制手柄震动的函数--需要用的时候引用一下这个函数就可以了。。 /// 至于怎么引用不同的方式不同 /// </summary> /// <param name="milliseconds"></param> /// <param name="strength"></param> public void Vibrate(int milliseconds, float strength) { float seconds = milliseconds / 1000f; StartCoroutine(LongVibration(seconds, strength)); } IEnumerator LongVibration(float length, float strength) { for (float i = 0; i < length; i += Time.deltaTime) { Device().TriggerHapticPulse((ushort)Mathf.Lerp(0, MAX_VIBRATION_STRENGTH, strength)); yield return null; } }}
阅读全文
1 0
- Unity -VIVE 开发第一弹
- Unity开发HTC VIVE
- Unity开发HTC vive 入门
- htc vive And Unity 开发VR
- Unity开发HTC vive 一、手柄按钮
- Unity开发HTC vive 二、操作GUI
- HTC Vive VR Unity 开发入门
- Unity开发HTC vive 四、传送
- Unity开发HTC vive 一、手柄按钮
- Unity开发HTC vive 一、手柄按钮
- Unity开发HTC vive 二、操作GUI
- Unity开发HTC vive 四、传送
- 【VR开发】htc vive+unity 3D 简单保龄球游戏
- Unity +HTC VIVE 虚拟现实开发— 手柄控制
- Unity +HTC VIVE 虚拟现实开发— 移动漫游
- Unity +HTC VIVE 虚拟现实开发— UI设计(UGUI)
- Unity +HTC VIVE 虚拟现实开发— 手柄震动
- Unity +HTC VIVE 虚拟现实开发— 警示线处理
- Millionaire
- Hadoop YARN配置参数剖析(1)—RM与NM相关参数
- greenplum segment down 实例
- Vim技能修炼教程(3)
- php echo, print(), print_r(), sprintf(), var_dump(), var_export() 的区别
- Unity -VIVE 开发第一弹
- JZOJ 5182. 【NOIP2017提高组模拟6.29】码灵鼠
- 新人上手TensorFlow之TensorFlow基本概念
- 从内存理解c语言中变量的存储类型
- linux常用命令小结
- 机器学习(3)——SVM学习
- CSS中关于table中属性的总结
- 医疗类直播(临床教学、手术观摩学习、医疗器械教学类)图文教程
- 使用AlertDialog实现使用在android中弹出对话框,询问用户是否继续执行