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;
                }
            }
        }
    }
相关推荐
SmalBox1 小时前
【URP】UnityHLSL顶点片元语义详解
unity·渲染
在路上看风景12 小时前
9. Mono项目与Unity的关系
unity
在路上看风景13 小时前
1.12 Memory Profiler Package - Summary
unity
xiaowu08015 小时前
策略模式-不同的鸭子的案例
开发语言·c#·策略模式
VisionPowerful17 小时前
九.弗洛伊德(Floyd)算法
算法·c#
ArabySide17 小时前
【C#】 资源共享和实例管理:静态类,Lazy<T>单例模式,IOC容器Singleton我们该如何选
单例模式·c#·.net core
SmalBox18 小时前
【URP】Unity Shader Tags
unity·渲染
gc_229919 小时前
C#测试调用OpenXml操作word文档的基本用法
c#·word·openxml
almighty271 天前
C#海康车牌识别实战指南带源码
c#·海康车牌识别·c#实现车牌识别·车牌识别源码·c#车牌识别
极客柒1 天前
Unity 塔防自用可视化路点寻路编辑器
unity·编辑器·游戏引擎