86人参与 • 2024-08-01 • VR/AR虚拟现实
①打开xri default input action 面板。
②设置左手柄上的按键就点击action maps 列表下的 xri lefthand interaction选项,设置右手柄上的按键就点击xri righthand interaction。
③以设置右手柄上的按键为例,我们将设置右手柄上的 a键、b键、摇杆按下键、摇杆上下左右推动事件、r2键(扳机键)、侧柄键(抓握键)等6个按键的绑定事件方法。
首先,点击action列表右上方的+号新建事件,将事件命名为按键名称。
④命名完成后为每个事件绑定对应的手柄按钮。
根据下面的图依次选择 xr controller、xr controller(righthand)、usage中对应的按钮。
a键 | primarybutton |
b键 | secondarybutton |
x键 | primarybutton |
y键 | secondarybutton |
扳机键(r2键) | tirggerbutton |
抓握键(侧柄键) | gripbutton |
摇杆按下键 | primary2daxisclick |
摇杆上推键 | primary2daxis |
摇杆下推键 | primary2daxis |
摇杆左推键 | primary2daxis |
摇杆右推键 | primary2daxis |
全部添加完成后如下图(本图只设置右手柄按键,所以不包含xy按键):
⑤设置触发方式,这里有一个注意点,就是abxy键、扳机键、侧柄键和摇杆中心键都是通过按下触发的,但是摇杆上下左右四个方向的键是通过推动的方式触发的,所以在设置的时候要区分开来。
abxy键、扳机键、侧柄键和摇杆中心键都是选中action列表下的对应选项设置 press,每一个事件上都要设置。选项下的按钮可以不设置,但如果后面测试没反应,可以在按扭上添加试一下。
如下图:
摇杆上下左右四个方向的键是选择选项下的对应按键设置sector。在 sector 模块下的directions选项中选择对应的摇杆方向,向上推就是north,向下推就是south,向左推就是west,向右推就是east,和看地图一样 上北下南左西右东 。每一个都要设置对应的方向,注意不要多选。
如下图:
⑥最后要记得点击保存!!!
保存后就能在xri default input actions中看到相应的按钮事件项了。
完整代码:
using system.collections;
using system.collections.generic;
using unityengine;
using unityengine.inputsystem;
public class handcontrollertest : monobehaviour
{
public inputactionreference tirgger_action;
public inputactionreference grip_action;
public inputactionreference pressa_action;
public inputactionreference pressb_action;
public inputactionreference pushup_action;
public inputactionreference pushdown_action;
public inputactionreference pushleft_action;
public inputactionreference pushright_action;
public inputactionreference pressrocker_action;
// update is called once per frame
void update()
{
if (pressa_action.action.wasperformedthisframe())
{
debug.log("a键");
}
if (pressb_action.action.wasperformedthisframe())
{
debug.log("b键");
}
if (tirgger_action.action.wasperformedthisframe())
{
debug.log("扳机键");
}
if (grip_action.action.wasperformedthisframe())
{
debug.log("抓握键");
}
if (pushup_action.action.wasperformedthisframe())
{
debug.log("上推");
}
if (pushdown_action.action.wasperformedthisframe())
{
debug.log("下推");
}
if (pushleft_action.action.wasperformedthisframe())
{
debug.log("左推");
}
if (pushright_action.action.wasperformedthisframe())
{
debug.log("右推");
}
if (pressrocker_action.action.wasperformedthisframe())
{
debug.log("按下摇杆键");
}
}
}
这样就可以了,运行设备测试看看吧!
使用注册事件的方式添加,可根据个人需求使用。
using system;
using system.collections;
using system.collections.generic;
using unityengine;
using unityengine.inputsystem;
public class handcontrollertest : monobehaviour
{
public inputactionreference tirgger_action;
private void onenable()
{
setupinteractorevents();
}
private void ondisable()
{
teardowninteractorevents();
}
void setupinteractorevents()
{
var teleportmodeactivateaction = getinputaction(tirgger_action);
if (teleportmodeactivateaction != null)
{
teleportmodeactivateaction.performed += ondowntirggeraction;
}
}
void teardowninteractorevents()
{
var teleportmodeactivateaction = getinputaction(tirgger_action);
if (teleportmodeactivateaction != null)
{
teleportmodeactivateaction.performed -= ondowntirggeraction;
}
}
private void ondowntirggeraction(inputaction.callbackcontext context)
{
debug.log("按下扳机键");
}
static inputaction getinputaction(inputactionreference actionreference)
{
#pragma warning disable ide0031 // use null propagation -- do not use for unityengine.object types
return actionreference != null ? actionreference.action : null;
#pragma warning restore ide0031
}
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论