|
馬上注冊,結交更多好友,享用更多功能,讓你輕松玩轉社區。
您需要 登錄 才可以下載或查看,沒有帳號?注冊帳號
x
[p=20 null left]#【前言】蠻牛的排版是真的難用,發一次帖子帖子格式都要搞好久好久
[mw_shl_code=csharptrue]public static int Compute(Type t)
{
string toBeHashed = \"s\\0\\0\\0\" + t.Namespace + t.Name;
using (HashAlgorithm hash = new MD4())
{
byte[] hashed = hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(toBeHashed));
int result = 0;
for (int i = 3; i >= 0; --i)
{
result <<= 8; result |= hashed;
}
return result;
}
}[/mw_shl_code]
[p=20 null left]
##之前搞SteamVR需要用到手柄的交互,然后從網上找了steamVR1.0的輸入事件用,并發現不能應用于2.0的SteanVR,所以測試時也是找的steamVR1.0的SDK,后來回頭看了下2.0升級的相關功能,還是要跟隨官方的步伐,所以研究了段時間的2.0的按鈕輸入事件,由于之前不知道公司電腦unity的文件被加密,導致steamVR讀取不了事件的json文件,導致綁定不上按鈕,所以也是幾經周折,吸取了經驗,把學習的兩個版本的的手柄按鈕事件分享下來。
[img=3000]leanote://file/getImage?fileId=601ace1fb0761c698b000000[/img]
[p=20 null left]圖為HTC VIVE Controller手柄按鈕圖示
【SteamVR1.0按鈕事件輸入】
```
using UnityEngine;
using System.Collections;
public class shoubingkongzhi : MonoBehaviour {
/// 手柄
SteamVR_TrackedObject tracked;
[mw_shl_code=cpptrue]public class test:monobehabeiha
{
void Start(){
}
void Update(){
}
}
[/mw_shl_code]
void Awake(){
//獲取手柄tracked = GetComponent();
}
// Update is called once per
void FixedUpdate()
{
var device = SteamVR_Controller.Input((int)tracked.index);
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
{
Debug.Log(\\\"按下圓盤\\\");
}
else if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log(\\\"按下扳機鍵\\\");
}
else if (device.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
{
Debug.Log(\\\"按下手柄側鍵\\\");
}
else if (device.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
{
Debug.Log(\\\"按下手柄菜單鍵\\\");
}else if (device.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
{
Debug.Log(\\\"按下手柄菜單鍵\\\");
}
}
}
```
[p=20 null left]以上都是HTC VIVE手柄中按鍵按下的代碼,直接從手柄上調用即可。其他還有:
按鍵松開—device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger);
按鍵長按—device.GetPress(SteamVR_Controller.ButtonMask.Trigger);
按鍵按下還有另一種方式,但是我自我感覺用著很別扭,沒上述的好。device.GetTouchDown(SteamVR_Controller.ButtonMask.ApplicationMenu);
其他用法與GetPressDown()類似。
**SteamVR1.0插件下載
#【SteamVR2.0按鈕事件輸入】
##新版SDK需要事件綁定到對應的按鈕,我也測過官方自帶的默認綁定事件
[p=20 null left]我用的是Unity2018.4.0f1下載的SDK,官方默認已經綁定好的按鈕事件:
【InteractUI——扳機
【Teleport——圓盤按鈕
【GrabPinch——扳機
【GrabGrip——兩側鍵
但功能不太齊全,建議還是自定義綁定使用會比較方便。
https://www.pianshen.com/article/4804330780/——SteamVR Plugin 2.0 InteractionSystem場景中如何使用新手柄按鍵
所以改版后的事件調用大致如下:
[p=20 null left]public SteamVR_Action_Boolean tigger;//握持鍵
[p=20 null left]
[p=20 null left]void Update()
[p=20 null left]{
[p=20 null left]if (tigger.GetStateDown(hand.handType))//按下
{
Debug.Log(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"tigger\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
}
[p=20 null left]}
[p=20 null left]
##【基于事件系統的調用機制封裝了代碼,讓開發者更方便綁定和調用事件】
[p=20 null left]我自己封裝了一個靜態類,用于玩家自定義事件和封裝按鈕方法,讓玩家像Unity的Iuput方法一樣調用按鈕按下、正在按當中、抬起三種按鈕狀態的方法封裝。也讓左右手和對應事件調用更簡明。
[p=20 null left]using UnityEngine;
using UnityEngine.Events;
using Valve.VR;
using System;
using System.Collections.Generic;
using UnityEditor;
[p=20 null left]namespace Valve.VR
{
public enum E_GetKey { 扳機 觸控板 握持 菜單 }//這里的枚舉元素要對應上底下手柄輸入事件,枚舉長度索引對應手柄輸入事件的名稱
[p=20 null left]public static class VRInput
{
//手柄自定義的輸入事件要綁定到對應的按鈕如果要增刪事件,就修改這個數組和上面與之對應的枚舉
static SteamVR_Action_Boolean[] m_action = {
SteamVR_Input.GetBooleanAction(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"扳機事件\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\")//第1個事件,可自己選擇
SteamVR_Input.GetBooleanAction(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"觸控板事件\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\")//第2個事件,可自己選擇
SteamVR_Input.GetBooleanAction(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"握持事件\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\")//第3個事件,可自己選擇
SteamVR_Input.GetBooleanAction(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"菜單事件\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\")//第3個事件,可自己選擇
};
[p=20 null left]public static bool GetKeyDown(SteamVR_Input_Sources inputType E_GetKey getKeyDown)
{
//Debug.LogError(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"GetKeyDown\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
if (m_action[(int)getKeyDown].GetStateDown(inputType))
{
//Debug.LogError(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"GetKeyDown\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
return true;
}
return false;
}
[p=20 null left]public static bool GetKey(SteamVR_Input_Sources inputType E_GetKey getkeyActive)
{
//Debug.LogError(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"GetKey\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
if (m_action[(int)getkeyActive].GetState(inputType))
{
return true;
}
return false;
}
[p=20 null left]public static bool GetKeyUp(SteamVR_Input_Sources inputType E_GetKey getKeyUp)
{
//Debug.LogError(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"GetKeyUp\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
if (m_action[(int)getKeyUp].GetStateUp(inputType))
{
return true;
}
return false;
}
}
}
[p=20 null left] **SteamVR2.0插件下載:
|
-
0.png
(75.19 KB, 下載次數: 39)
SteamVRController手柄按鈕
|