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;
                }
            }
        }
    }
相关推荐
90后小陈老师2 小时前
Unity教学 项目2 2D闯关游戏
游戏·unity·游戏引擎
噗噗夹的TA之旅3 小时前
Unity Shader 学习20:URP LitForwardPass PBR 解析
学习·unity·游戏引擎·图形渲染·技术美术
nnsix3 小时前
Unity ReferenceFinder插件 多选资源查找bug解决
unity·游戏引擎·bug
gzroy5 小时前
Unity Shader Graph实现全息瞄准器
unity·游戏引擎
咖啡の猫5 小时前
Python字典的查询操作
数据库·python·c#
czhc11400756636 小时前
c# 1213
开发语言·数据库·c#
xiaoid7 小时前
C#向jave平台的API接口推送
c#·post·webapi
90后小陈老师8 小时前
Unity教学 基础介绍
unity·游戏引擎
90后小陈老师8 小时前
Unity教学 项目3 3D坦克大战
3d·unity·游戏引擎
小猪快跑爱摄影9 小时前
【AutoCad 2025】【C#】零基础教程(三)——获取选中的 Entity 插件 =》 初识 Entity 派生类
c#·autocad