InputAction的使用

感觉Unity中InputAction的使用,步步都是坑。

需求点介绍

当用户长按0.5s 键盘X或者VR left controller primaryButton (即X键)时,显示下一个图片。

步骤总览

  1. 创建InputAction资产
  2. 将该InputAction资产绑定到某个GameObject上
  3. 在对应的script中,接收InputAction触发的事件,完成"显示下一个图片"的逻辑

细节

  1. 创建InputAction资产

  2. 将该InputAction资产绑定到某个GameObject上。

如果Behaviour是send messages或者broadcast messages,那这个GameObject或者其子GameObject包含Component,这个Component有需要检测InputAction输入的function。

如果Behaviour是Invoke Unity Event,那好像绑定在哪个GameObject都没有关系,只要Player Input这个component能在function之前被初始化就行。

至于Behavior如何设置,见官网, 不过官网说得也不明不白的。

在我的例子中,我需要设置成Invoke Unity Events,然后像对待UI控件一样,显性绑定才能触发function的运行。Send/Broadcast Messages都不行。

  1. 在对应的script中,接收InputAction触发的事件,完成"显示下一个图片"的逻辑
csharp 复制代码
    public void ChangeStrokeImage(InputAction.CallbackContext context)
    {   if (context.action.name == "DrawNext") {
            if (context.performed) // Otherwise this will be triggered three times. 
            {
                if (textures != null && textures.Length > 0)
                {
                    currentIndex = currentIndex  % textures.Length;
                    strokeRawImage.texture = textures[currentIndex];
                    currentIndex = currentIndex + 1;
                }
            }
        }
    }
相关推荐
大侠课堂6 小时前
C#经典面试题100道
开发语言·c#
时光追逐者7 小时前
Visual Studio 2026 现已正式发布,更快、更智能!
ide·c#·.net·visual studio
周杰伦fans9 小时前
C# 正则表达式完全指南
mysql·正则表达式·c#
Triumph++11 小时前
电器模C#汇控电子继块驱动(Modbus协议)
c#·visual studio·c#串口通信
沉默金鱼11 小时前
Unity实用技能-格式化format文字
ui·unity·游戏引擎
jyy_9911 小时前
通过网页地址打开unity的exe程序,并传参
unity
咩图14 小时前
C#创建AI项目
开发语言·人工智能·c#
周杰伦fans15 小时前
C# - Task 是什么?想象一下你在餐厅点餐
服务器·开发语言·c#
一只小小汤圆17 小时前
简化点集合 道格拉斯-普克算法(Douglas-Peucker Algorithm)
c#·occ
scixing17 小时前
函数式编程 第八讲 循环者,递归也
开发语言·c#