文章目录
前言
官方文档input system:链接: input system
安装部分:链接: unity xr interaction toolkit的安装(二)
一、xri default input actions
1.导入官方案例
需要导入官方案例:starter assets,方便学习
2.设置控制器绑定,如手柄、主/辅助按钮、操纵杆等
1.要设置控制器绑定,如左右手 手柄、主/辅助按钮、操纵杆等,请双击:xri default input actions
2.获取左手xy键事件(右手ab同理)
选择顺序 xr controller–>xr controller (righthand)或者xr controller(lefthand)–>usage
二、按键对应名称
三、代码示例
代码如下(示例):
using unityengine;
using unityengine.inputsystem;
namespace twq
{
public class menumanage : monobehaviour
{
public inputactionreference myleftbutton_x;
public inputactionreference myleftbutton_y;
public inputactionreference myrightbutton_x;
public inputactionreference myrightbutton_y;
private void onenable()
{
setupinteractorevents();
}
private void ondisable()
{
teardowninteractorevents();
}
void setupinteractorevents()
{
//左手
var myleftbutton_x_action = getinputaction(myleftbutton_x);
if (myleftbutton_x_action != null)
{
myleftbutton_x_action.performed += onmyleftbutton_x_action;
}
var myleftbutton_y_action = getinputaction(myleftbutton_y);
if (myleftbutton_y_action != null)
{
myleftbutton_y_action.performed += onmyleftbutton_y_action;
}
//右手
var myrightbutton_x_action = getinputaction(myrightbutton_x);
if (myrightbutton_x_action != null)
{
myrightbutton_x_action.performed += onmyrightbutton_x_action;
}
var myrightbutton_y_action = getinputaction(myrightbutton_y);
if (myrightbutton_y_action != null)
{
myrightbutton_y_action.performed += onmyrightbutton_y_action;
}
}
void teardowninteractorevents()
{
var myleftbutton_x_action = getinputaction(myleftbutton_x);
if (myleftbutton_x_action != null)
{
myleftbutton_x_action.performed -= onmyleftbutton_x_action;
}
var myleftbutton_y_action = getinputaction(myleftbutton_y);
if (myleftbutton_y_action != null)
{
myleftbutton_y_action.performed -= onmyleftbutton_y_action;
}
}
/// <summary>
/// 左手x键
/// </summary>
/// <param name="context"></param>
private void onmyleftbutton_x_action(inputaction.callbackcontext context)
{
debug.log("按下左手x键--------------------");
}
/// <summary>
/// 左手y键
/// </summary>
/// <param name="context"></param>
private void onmyleftbutton_y_action(inputaction.callbackcontext context)
{
debug.log("按下左手y键--------------------");
}
/// <summary>
/// 右手x键
/// </summary>
/// <param name="context"></param>
private void onmyrightbutton_x_action(inputaction.callbackcontext context)
{
debug.log("按下右手a键--------------------");
}
/// <summary>
/// 右手y键
/// </summary>
/// <param name="context"></param>
private void onmyrightbutton_y_action(inputaction.callbackcontext context)
{
debug.log("按下右手b键--------------------");
}
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
}
}
}
总结
好记性不如烂笔头
发表评论