1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Unity的HTC VIVE SDK研究(手柄按键功能的研究 比较详细)

Unity的HTC VIVE SDK研究(手柄按键功能的研究 比较详细)

时间:2020-11-26 11:41:03

相关推荐

Unity的HTC VIVE SDK研究(手柄按键功能的研究 比较详细)

/ystistheking/article/details/51553237

想交流的朋友我们可以微博互粉,我的微博黑石铸造厂厂长,缺粉丝啊 。。。。。求粉求粉

研究了几天htc vive的接口,总算是把基本的按键功能研究出来了,这里分享一下,一来当做笔记,二来也希望对大家有所帮助。

如何导入Steam_VR那个包什么的我就不说了,网上有几个前辈已经教了,蛮牛论坛啥的上面都有,这里只把比较详细的按键功能分享一下,不知啥高端的东西,也算一段时间劳动成果啦,所以转载的帮我留个名写个转,谢谢啦。

个人感觉手柄上开始比较难搞明白的就是那个圆盘键,这个键是一个以中心为(0,0)点的直角坐标系,四个端长度都是1,可接收触摸和按压两种事件,大体就是下图这个意思(手绘水平略渣,见谅见谅),触摸touch或按压press会通过GetAxis方法返回一个坐标系中的点,可以判断你按在哪里,触发不同的事件,可以根据角度或各种方法来切分按键为n个按钮(就像切蛋糕一样)

这里用的是C#脚本 ,直接上代码了,我个人写注释比较话唠,适合新手看:

[csharp]view plain copy usingUnityEngine;usingSystem.Collections;//检测手柄功能的脚本这个脚本挂到手柄上(controler(right)和controler(left))上publicclassButtonTouchAction:MonoBehaviour{//手柄SteamVR_TrackedObjecttrackdeObjec;voidAwake(){//获取手柄上的这个组件trackdeObjec=GetComponent<SteamVR_TrackedObject>();}//UsethisforinitializationvoidStart(){}voidFixedUpdate(){//获取手柄输入vardevice=SteamVR_Controller.Input((int)trackdeObjec.index);//以下是api中复制出来的按键列表/*publicclassButtonMask{publicconstulongSystem=(1ul<<(int)EVRButtonId.k_EButton_System);//reservedpublicconstulongApplicationMenu=(1ul<<(int)EVRButtonId.k_EButton_ApplicationMenu);publicconstulongGrip=(1ul<<(int)EVRButtonId.k_EButton_Grip);publicconstulongAxis0=(1ul<<(int)EVRButtonId.k_EButton_Axis0);publicconstulongAxis1=(1ul<<(int)EVRButtonId.k_EButton_Axis1);publicconstulongAxis2=(1ul<<(int)EVRButtonId.k_EButton_Axis2);publicconstulongAxis3=(1ul<<(int)EVRButtonId.k_EButton_Axis3);publicconstulongAxis4=(1ul<<(int)EVRButtonId.k_EButton_Axis4);publicconstulongTouchpad=(1ul<<(int)EVRButtonId.k_EButton_SteamVR_Touchpad);publicconstulongTrigger=(1ul<<(int)EVRButtonId.k_EButton_SteamVR_Trigger);}*///同样是三种按键方式,以后不做赘述if(device.GetTouch(SteamVR_Controller.ButtonMask.Trigger)){Debug.Log("按了“trigger”“扳机键”");//右手震动//拉弓类似操作应该就是按住trigger(扳机)gettouch时持续调用震动方法模拟弓弦绷紧的感觉。vardeviceIndex2=SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost);SteamVR_Controller.Input(deviceIndex2).TriggerHapticPulse(500);}if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger)){Debug.Log("按下了“trigger”“扳机键”");}if(device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger)){Debug.Log("松开了“trigger”“扳机键”");//左手震动vardeviceIndex=SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Leftmost);SteamVR_Controller.Input(deviceIndex).TriggerHapticPulse(3000);//右手震动vardeviceIndex1=SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost);SteamVR_Controller.Input(deviceIndex1).TriggerHapticPulse(3000);}//这三种也能检测到后面不做赘述if(device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)){Debug.Log("用press按下了“trigger”“扳机键”");}if(device.GetPress(SteamVR_Controller.ButtonMask.Trigger)){Debug.Log("用press按了“trigger”“扳机键”");}if(device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger)){Debug.Log("用press松开了“trigger”“扳机键”");}//system键圆盘下面那个键//reserved为Steam系统保留,用来调出Steam系统菜单因此貌似自己加的功能没啥用if(device.GetTouchDown(SteamVR_Controller.ButtonMask.System)){Debug.Log("按下了“system”“系统按钮/Steam”");}if(device.GetPressDown(SteamVR_Controller.ButtonMask.System)){Debug.Log("用press按下了“System”“系统按钮/Steam”");}//ApplicationMenu键带菜单标志的那个按键(在方向圆盘上面)if(device.GetTouchDown(SteamVR_Controller.ButtonMask.ApplicationMenu)){Debug.Log("按下了“ApplicationMenu”“菜单键”");}if(device.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu)){Debug.Log("用press按下了“ApplicationMenu”“菜单键”");}//Grip键两侧的键(vive雇佣兵游戏中的换弹键),每个手柄左右各一功能相同,同一手柄两个键是一个键。if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Grip)){Debug.Log("按下了“Grip”“”");}if(device.GetPressDown(SteamVR_Controller.ButtonMask.Grip)){Debug.Log("用press按下了“Grip”“”");}//Axis0键与圆盘有交互与圆盘有关//触摸触发if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis0)){Debug.Log("按下了“Axis0”“方向”");}//按动触发if(device.GetPressDown(SteamVR_Controller.ButtonMask.Axis0)){Debug.Log("用press按下了“Axis0”“方向”");}//Axis1键目前未发现按键位置//触摸触发if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis1)){Debug.Log("按下了“Axis1”“”");}//按动触发if(device.GetPressDown(SteamVR_Controller.ButtonMask.Axis1)){Debug.Log("用press按下了“Axis1”“”");}//Axis2键目前未发现按键位置//触摸触发if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis2)){Debug.Log("按下了“Axis2”“”");}//按动触发if(device.GetPressDown(SteamVR_Controller.ButtonMask.Axis2)){Debug.Log("用press按下了“Axis2”“”");}//Axis3键未目前未发现按键位置//触摸触发if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis3)){Debug.Log("按下了“Axis3”“”");}//按动触发if(device.GetPressDown(SteamVR_Controller.ButtonMask.Axis3)){Debug.Log("用press按下了“Axis3”“”");}//Axis4键目前未发现按键位置//触摸触发if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis4)){Debug.Log("按下了“Axis4”“”");}//按动触发if(device.GetPressDown(SteamVR_Controller.ButtonMask.Axis4)){Debug.Log("用press按下了“Axis4”“”");}<prename="code"class="csharp">//方向圆盘://这里开始区分了press检测与touch检测的不同之处,圆盘可以触摸,顾名思义,touch检测的是触摸,press检测的是按动<prename="code"class="csharp">//Axis0键与圆盘有交互与圆盘有关//触摸触发if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis0)){Debug.Log("按下了“Axis0”“方向”");}//按动触发if(device.GetPressDown(SteamVR_Controller.ButtonMask.Axis0)){Debug.Log("用press按下了“Axis0”“方向”");}//Axis1键目前未发现按键位置//触摸触发if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis1)){Debug.Log("按下了“Axis1”“”");}//按动触发if(device.GetPressDown(SteamVR_Controller.ButtonMask.Axis1)){Debug.Log("用press按下了“Axis1”“”");}//Axis2键目前未发现按键位置//触摸触发if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis2)){Debug.Log("按下了“Axis2”“”");}//按动触发if(device.GetPressDown(SteamVR_Controller.ButtonMask.Axis2)){Debug.Log("用press按下了“Axis2”“”");}//Axis3键未目前未发现按键位置//触摸触发if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis3)){Debug.Log("按下了“Axis3”“”");}//按动触发if(device.GetPressDown(SteamVR_Controller.ButtonMask.Axis3)){Debug.Log("用press按下了“Axis3”“”");}//Axis4键目前未发现按键位置//触摸触发if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis4)){Debug.Log("按下了“Axis4”“”");}//按动触发if(device.GetPressDown(SteamVR_Controller.ButtonMask.Axis4)){Debug.Log("用press按下了“Axis4”“”");}//ATouchpad键圆盘交互//触摸触发if(device.GetTouchDown(SteamVR_Controller.ButtonMask.Touchpad)){Debug.Log("按下了“Touchpad”“”");//方法返回一个坐标接触圆盘位置Vector2cc=device.GetAxis();Debug.Log(cc);//例子:圆盘分成上下左右floatjiaodu=VectorAngle(newVector2(1,0),cc);Debug.Log(jiaodu);//下if(jiaodu>45&&jiaodu<135){Debug.Log("下");}//上if(jiaodu<-45&&jiaodu>-135){Debug.Log("上");}//左if((jiaodu<180&&jiaodu>135)||(jiaodu<-135&&jiaodu>-180)){Debug.Log("左");}//右if((jiaodu>0&&jiaodu<45)||(jiaodu>-45&&jiaodu<0)){Debug.Log("右");}}//按动触发if(device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad)){Debug.Log("用press按下了“Touchpad”“”");}}//UpdateiscalledonceperframevoidUpdate(){}//方向圆盘最好配合这个使用圆盘的.GetAxis()会检测返回一个二位向量,可用角度划分圆盘按键数量//这个函数输入两个二维向量会返回一个夹角180到-180floatVectorAngle(Vector2from,Vector2to){floatangle;Vector3cross=Vector3.Cross(from,to);angle=Vector2.Angle(from,to);returncross.z>0?-angle:angle;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。