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;
                }
            }
        }
    }
相关推荐
派葛穆11 小时前
Unity-UI 按钮点击弹窗功能
unity·游戏引擎
geovindu14 小时前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
toponad16 小时前
Unity Ads Bidding 现已正式加入TopOn 聚合平台
unity·topon
小羊先生car18 小时前
RTOS-F429-HAL-(动/静态)任务的创建(2026/7/27)
开发语言·算法·c#
unityのkiven19 小时前
Unity UGUI 实战:动态生成任务面板
unity·游戏引擎
地球驾驶员19 小时前
NX二次开发C#-获取体的外表面
开发语言·c#
郝学胜-神的一滴20 小时前
中级OpenGL教程 026:Assimp库从编译到实战全攻略
c++·unity·游戏引擎·图形渲染·unreal engine·opengl
向夏威夷 梦断明暄20 小时前
从 Bun 的 Rust 重写,看 C# 如何重建 AI 基础设施层
人工智能·rust·c#
心平气和量大福大21 小时前
C#-WPF-控件-LiveChart图表-线性2(LineSeries)-数据绑定
开发语言·c#·wpf
海盗12341 天前
微软技术周报2026-07-27
microsoft·c#·.net