您的位置:首页 > 移动开发 > Unity3D

Unity +HTC VIVE 虚拟现实开发— 手柄控制

2016-08-04 17:45 423 查看
<p>Hello 大家好,我是帅气的交换机,这是我人生中第一次写博客,但绝不是最后一次,哈哈,想想都好激动啊。</p><p>言归正传,写这篇文章的原因主要是因为现在虚拟现实非常的火爆但目前主流的虚拟现实设备(HTC VIVE)的教程却少的可怜,这个我深有体会。所以,我想将我平时开发中遇到的问题以及解决方法记录下来,分享给大家,若其中有什么错误或者大家有什么更好的方案也请大家指出,大家互相学习,哈哈。</p><p>好了直接上代码。</p>
</pre><pre name="code" class="html"><pre name="code" class="csharp">using UnityEngine;
using System.Collections;

public class shoubingkongzhi : MonoBehaviour {
/// <summary>
/// 手柄
/// </summary>
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()类似。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: