UnitySteamVR按键监听函数

如何配置一个自定义的按钮

点击+号创建 然后点击Save and generate保存 在点击 Open binding UI 打开编辑面板绑定按键

请添加图片描述

最后点击替换默认按键设置保存自己的修改

方法一

//--在update中监听按键---不推荐
private void Update()
    {
        //模范参数写按键的类型,函数参数写按键的名称,最后参数为按键的输入来源:左右手柄或任意手柄
        if (SteamVR_Input.GetAction<SteamVR_Action_Boolean>("Fire").GetState(SteamVR_Input_Sources.Any))
        {
            Debug.Log("Firing");
        }
        if (SteamVR_Input.GetAction<SteamVR_Action_Boolean>("Fire").GetStateUp(SteamVR_Input_Sources.Any))
        {
            Debug.Log("Fire Up");
        }
        if (SteamVR_Input.GetAction<SteamVR_Action_Boolean>("Fire").GetStateDown(SteamVR_Input_Sources.Any))
        {
            Debug.Log("Firing Down");
        }
    }

方法二

 void Start()
    {
        //---这种写法比较简单,但是无法指定输入手,并且需要在不用的时候一个个删除
        SteamVR_Actions.default_Fire.onStateUp += FireUp;
        SteamVR_Actions.default_Fire.onState += Firing;
        SteamVR_Actions.default_Fire.onStateDown += FireDown;
    }
 void End()
 {
	 	SteamVR_Actions.default_Fire.onStateUp -= FireUp;
        SteamVR_Actions.default_Fire.onState -= Firing;
        SteamVR_Actions.default_Fire.onStateDown -= FireDown;
 }
private void FireDown(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
{
    Debug.Log("Fire Down");
}
private void Firing(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
{
    Debug.Log("Firing");
}
private void FireUp(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
{
  	Debug.Log("Fire up");       
}

方法三

//--需要注意的是这种方式 按键持续状态下的监听事件要分别在按下和弹起监听函数中  添加/移除
 void Start()
 {
   	SteamVR_Actions.default_Fire.AddOnStateUpListener(FireUp,SteamVR_Input_Sources.Any);
    SteamVR_Actions.default_Fire.AddOnStateDownListener(FireDown,SteamVR_Input_Sources.Any);
 }
 void End()
 {
 	SteamVR_Actions.default_Fire.RemoveAllListeners(SteamVR_Input_Sources.Any);
 }

 private void FireDown(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
 {
     Debug.Log("Fire Down");
     SteamVR_Actions.default_Fire.AddOnUpdateListener(Firing, SteamVR_Input_Sources.Any);
 }
     private void Firing(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource, bool newState)
 {
     Debug.Log("Firing");
 }
 private void FireUp(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
 {
     Debug.Log("Fire up");
     SteamVR_Actions.default_Fire.RemoveOnUpdateListener(Firing, SteamVR_Input_Sources.Any);
 }
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值