首页 > 代码库 > 用Unity开发HTC VIVE——手柄控制篇

用Unity开发HTC VIVE——手柄控制篇

写这篇文章的原因主要是因为现在虚拟现实非常的火爆但目前主流的虚拟现实设备(HTC VIVE)的教程却少的可怜,这个我深有体会。所以,我想将我平时开发中遇到的问题以及解决方法记录下来,分享给大家,若其中有什么错误或者大家有什么更好的方案也请大家指出,大家互相学习,哈哈。

 

好了直接上代码。


using UnityEngine;
using System.Collections;

public class shoubingkongzhi : MonoBehaviour {
    /// 
    /// 手柄
    /// 
    SteamVR_TrackedObject tracked;



    void Awake()
    {
        //获取手柄
        tracked = GetComponent<steamvr_trackedobject>();

    }

    // Update is called once per frame
    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("按下手柄菜单键");
        }
        
    }
}

Debug.Log("按下手柄菜单键");}}}

以上都是HTC VIVE手柄中按键按下的代码。其他还有:
按键松开—device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger);
按键长安—device.GetPress(SteamVR_Controller.ButtonMask.Trigger);
按键按下还有另一种方式,但是我自我感觉用着很别扭,没上述的好。device.GetTouchDown(SteamVR_Controller.ButtonMask.ApplicationMenu);
其他用法与GetPressDown()类似。

转载自:http://www.52vr.com/article-391-1.html

用Unity开发HTC VIVE——手柄控制篇