【Unity学习笔记】New Input System 部分源码和测试用例补充

这篇具有很好参考价值的文章主要介绍了【Unity学习笔记】New Input System 部分源码和测试用例补充。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

转载请注明出处:🔗https://blog.csdn.net/weixin_44013533/article/details/135630016
作者:CSDN@|Ringleader|

主要参考:

  • Unity官方Input System手册与API
  • 【Unity学习笔记】Unity TestRunner使用

NewIputSystem主体内容请参见:【Unity学习笔记】第十二 · New Input System 及其系统结构 和 源码浅析

注:本文使用的unity版本是2021.3.25f,InputSystem版本为1.5.1

deafault Interaction测试用例

  1. ButtonAndValue

    [Test]
    public void Actions_ButtonActions_RespectButtonPressPoints()
    {
        InputSystem.settings.defaultButtonPressPoint = 0.5f;
        InputSystem.settings.buttonReleaseThreshold = 0.9f; // Release at 90% of press point puts release at 0.45.
    
        // Customize gamepad with specific button press point on right trigger (but not on left).
        InputSystem.RegisterLayoutOverride(@"
            {
                ""name"" : ""GamepadRightTriggerWithPressPoint"",
                ""extend"" : ""Gamepad"",
                ""controls"" : [
                    { ""name"" : ""rightTrigger"", ""parameters"" : ""pressPoint=0.75"" }
                ]
            }
        ");
    
        var gamepad = InputSystem.AddDevice<Gamepad>();
    
        var rightTriggerButton = new InputAction(type: InputActionType.Button, binding: "<Gamepad>/rightTrigger");
        var rightTriggerValue = new InputAction(type: InputActionType.Value, binding: "<Gamepad>/rightTrigger");
        var leftTriggerButton = new InputAction(type: InputActionType.Button, binding: "<Gamepad>/leftTrigger");
        var leftTriggerValue = new InputAction(type: InputActionType.Value, binding: "<Gamepad>/leftTrigger");
    
        rightTriggerButton.Enable();
        rightTriggerValue.Enable();
        leftTriggerButton.Enable();
        leftTriggerValue.Enable();
    
        using (var rightTriggerButtonTrace = new InputActionTrace(rightTriggerButton))
        using (var rightTriggerValueTrace = new InputActionTrace(rightTriggerValue))
        using (var leftTriggerButtonTrace = new InputActionTrace(leftTriggerButton))
        using (var leftTriggerValueTrace = new InputActionTrace(leftTriggerValue))
        {
            Set(gamepad.leftTrigger, 0.25f);
            Set(gamepad.rightTrigger, 0.25f);
    
            // Have crossed neither button threshold. Actions start but don't perform.
            Assert.That(leftTriggerButtonTrace, Started(leftTriggerButton, gamepad.leftTrigger, value: 0.25f));
            Assert.That(rightTriggerButtonTrace, Started(rightTriggerButton, gamepad.rightTrigger, value: 0.25f));
    
            Assert.That(leftTriggerValueTrace,
                Started(leftTriggerValue, gamepad.leftTrigger, value: 0.25f)
                    .AndThen(Performed(leftTriggerValue, gamepad.leftTrigger, value: 0.25f)));
            Assert.That(rightTriggerValueTrace,
                Started(rightTriggerValue, gamepad.rightTrigger, value: 0.25f)
                    .AndThen(Performed(rightTriggerValue, gamepad.rightTrigger, value: 0.25f)));
    
            rightTriggerButtonTrace.Clear();
            rightTriggerValueTrace.Clear();
            leftTriggerButtonTrace.Clear();
            leftTriggerValueTrace.Clear();
    
            Set(gamepad.leftTrigger, 0.6f);
            Set(gamepad.rightTrigger, 0.6f);
    
            // Have grossed global but not right trigger threshold.
            Assert.That(leftTriggerButtonTrace, Performed(leftTriggerButton, gamepad.leftTrigger, value: 0.6f));
            Assert.That(rightTriggerButtonTrace, Is.Empty);
    
            Assert.That(leftTriggerValueTrace, Performed(leftTriggerValue, gamepad.leftTrigger, value: 0.6f));
            Assert.That(rightTriggerValueTrace, Performed(rightTriggerValue, gamepad.rightTrigger, value: 0.6f));
    
            rightTriggerButtonTrace.Clear();
            rightTriggerValueTrace.Clear();
            leftTriggerButtonTrace.Clear();
            leftTriggerValueTrace.Clear();
    
            Set(gamepad.leftTrigger, 0.9f);
            Set(gamepad.rightTrigger, 0.9f);
    
            // No change on left trigger action, right trigger crossed threshold.
            Assert.That(leftTriggerButtonTrace, Is.Empty);
            Assert.That(rightTriggerButtonTrace, Performed(rightTriggerButton, gamepad.rightTrigger, value: 0.9f));
    
            Assert.That(leftTriggerValueTrace, Performed(leftTriggerValue, gamepad.leftTrigger, value: 0.9f));
            Assert.That(rightTriggerValueTrace, Performed(rightTriggerValue, gamepad.rightTrigger, value: 0.9f));
    
            rightTriggerButtonTrace.Clear();
            rightTriggerValueTrace.Clear();
            leftTriggerButtonTrace.Clear();
            leftTriggerValueTrace.Clear();
    
            Set(gamepad.leftTrigger, 0.6f);
            Set(gamepad.rightTrigger, 0.6f);
    
            // No change on left trigger action, right trigger action cancels.
            Assert.That(leftTriggerButtonTrace, Is.Empty);
            Assert.That(rightTriggerButtonTrace, Started(rightTriggerButton, gamepad.rightTrigger, value: 0.6f));
    
            Assert.That(leftTriggerValueTrace, Performed(leftTriggerValue, gamepad.leftTrigger, value: 0.6f));
            Assert.That(rightTriggerValueTrace, Performed(rightTriggerValue, gamepad.rightTrigger, value: 0.6f));
    
            rightTriggerButtonTrace.Clear();
            rightTriggerValueTrace.Clear();
            leftTriggerButtonTrace.Clear();
            leftTriggerValueTrace.Clear();
    
            Set(gamepad.leftTrigger, 0.4f);
            Set(gamepad.rightTrigger, 0.4f);
    
            // Left trigger cancels, right trigger *starts* again (but doesn't perform).
            Assert.That(leftTriggerButtonTrace, Started(leftTriggerButton, gamepad.leftTrigger, value: 0.4f));
            Assert.That(rightTriggerButtonTrace, Is.Empty);
    
            Assert.That(leftTriggerValueTrace, Performed(leftTriggerValue, gamepad.leftTrigger, value: 0.4f));
            Assert.That(rightTriggerValueTrace, Performed(rightTriggerValue, gamepad.rightTrigger, value: 0.4f));
    
            rightTriggerButtonTrace.Clear();
            rightTriggerValueTrace.Clear();
            leftTriggerButtonTrace.Clear();
            leftTriggerValueTrace.Clear();
    
            Set(gamepad.leftTrigger, 0f);
            Set(gamepad.rightTrigger, 0f);
    
            // No change on left and right trigger actions.
            Assert.That(leftTriggerButtonTrace, Canceled(leftTriggerButton, gamepad.leftTrigger, 0f));
            Assert.That(rightTriggerButtonTrace, Canceled(rightTriggerButton, gamepad.rightTrigger, 0f));
    
            Assert.That(leftTriggerValueTrace, Canceled(leftTriggerValue, gamepad.leftTrigger, value: 0f));
            Assert.That(rightTriggerValueTrace, Canceled(rightTriggerValue, gamepad.rightTrigger, value: 0f));
    
            rightTriggerButtonTrace.Clear();
            leftTriggerButtonTrace.Clear();
    
            // Make sure that we start and cancel if we press the buttons just a little and then release.
            Set(gamepad.leftTrigger, 0.25f);
            Set(gamepad.rightTrigger, 0.25f);
    
            Assert.That(leftTriggerButtonTrace, Started(leftTriggerButton, gamepad.leftTrigger, value: 0.25f));
            Assert.That(rightTriggerButtonTrace, Started(rightTriggerButton, gamepad.rightTrigger, value: 0.25f));
    
            rightTriggerButtonTrace.Clear();
            leftTriggerButtonTrace.Clear();
    
            Set(gamepad.leftTrigger, 0f);
            Set(gamepad.rightTrigger, 0f);
    
            Assert.That(leftTriggerButtonTrace, Canceled(leftTriggerButton, gamepad.leftTrigger, value: 0f));
            Assert.That(rightTriggerButtonTrace, Canceled(rightTriggerButton, gamepad.rightTrigger, value: 0f));
        }
    }
    
  2. passthrough文章来源地址https://www.toymoban.com/news/detail-815349.html

    [Test]
    public void Actions_passthroughActions()
    {
    
        var gamepad = InputSystem.AddDevice<Gamepad>();
    
        var rightTriggerValue = new InputAction(type: InputActionType.PassThrough, binding: "<Gamepad>/rightTrigger");
    
        rightTriggerValue.Enable();
    
        using (var rightTriggerValueTrace = new InputActionTrace(rightTriggerValue))
        {
            Set(gamepad.rightTrigger, 0.25f);
            Assert.That(rightTriggerValueTrace, Performed(rightTriggerValue, gamepad.rightTrigger, value: 0.25f));
            rightTriggerValueTrace.Clear();
    
            Set(gamepad.rightTrigger, 0.6f);
            Assert.That(rightTriggerValueTrace, Performed(rightTriggerValue, gamepad.rightTrigger, value: 0.6f));
            rightTriggerValueTrace.Clear();
            
            Set(gamepad.rightTrigger, 0.9f);
            Assert.That(rightTriggerValueTrace, Performed(rightTriggerValue, gamepad.rightTrigger, value: 0.9f));
            rightTriggerValueTrace.Clear();
            
            Set(gamepad.rightTrigger, 0f);
            Assert.That(rightTriggerValueTrace, Performed(rightTriggerValue, gamepad.rightTrigger, value: 0f));
            rightTriggerValueTrace.Clear();
        }
    }
    

Press Interaction测试用例

using System.Diagnostics.CodeAnalysis;
using NUnit.Framework;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Utilities;

namespace Scenes.TestAction
{
    [SuppressMessage("ReSharper", "AccessToStaticMemberViaDerivedType")]
    public class TestAction : InputTestFixture
    {

        [Test]
        [TestCase(InputActionType.Value)]
        [TestCase(InputActionType.Button)]
        [TestCase(InputActionType.PassThrough)]
        public void Actions_PressOnly(InputActionType type)
        {
            var gamepad = InputSystem.AddDevice<Gamepad>();

            InputSystem.settings.defaultButtonPressPoint = 0.5f;
            InputSystem.settings.buttonReleaseThreshold = 0.75f; // Puts release point at 0.375.

            var action = new InputAction(type:type, binding: "<Gamepad>/leftTrigger", interactions: "press(behavior=0)");
            action.Enable();

            using (var trace = new InputActionTrace(action))
            {
                Set(gamepad.leftTrigger, 0.35f);

                Assert.That(trace, Started(action, control: gamepad.leftTrigger, value: 0.35f));

                trace.Clear();

                Set(gamepad.leftTrigger, 0.5f);

                Assert.That(trace, Performed(action, control: gamepad.leftTrigger, value: 0.5f));

                trace.Clear();

                Set(gamepad.leftTrigger, 0.6f);

                Assert.That(trace, Is.Empty);

                Set(gamepad.leftTrigger, 0.4f);

                Assert.That(trace, Is.Empty);

                Set(gamepad.leftTrigger, 0.3f);

                Assert.That(trace, Started(action, control: gamepad.leftTrigger, value: 0.3f));
                
                trace.Clear();
                Set(gamepad.leftTrigger, 0);
                Assert.That(trace, Canceled(action, control: gamepad.leftTrigger, value: 0));
            }
        }
        
        [Test]
        [TestCase(InputActionType.Value)]
        [TestCase(InputActionType.Button)]
        [TestCase(InputActionType.PassThrough)]
        public void Actions_RealeasOnly(InputActionType type)
        {
            var gamepad = InputSystem.AddDevice<Gamepad>();

            InputSystem.settings.defaultButtonPressPoint = 0.5f;
            InputSystem.settings.buttonReleaseThreshold = 0.75f; // Puts release point at 0.375.

            var action = new InputAction(type:type, binding: "<Gamepad>/leftTrigger", interactions: "press(behavior=1)");
            action.Enable();

            using (var trace = new InputActionTrace(action))
            {
                Set(gamepad.leftTrigger, 0.35f);
                Assert.That(trace, Started(action, control: gamepad.leftTrigger, value: 0.35f));
                trace.Clear();

                Set(gamepad.leftTrigger, 0.5f);
                Assert.That(trace, Is.Empty);
                trace.Clear();

                Set(gamepad.leftTrigger, 0.6f);
                Assert.That(trace, Is.Empty);
                trace.Clear();

                Set(gamepad.leftTrigger, 0.4f);
                Assert.That(trace, Is.Empty);
                trace.Clear();

                Set(gamepad.leftTrigger, 0.3f);
                Assert.That(trace,
                    Performed(action, control: gamepad.leftTrigger, value: 0.3f)
                        .AndThen(Canceled(action, control: gamepad.leftTrigger, value: 0)));
                trace.Clear();

                Set(gamepad.leftTrigger, 0.1f);
                Assert.That(trace, Started(action, control: gamepad.leftTrigger, value: 0.1f));
                trace.Clear();

                Set(gamepad.leftTrigger, 0);
                Assert.That(trace, Canceled(action, control: gamepad.leftTrigger, value: 0));
                trace.Clear();
                
                Set(gamepad.leftTrigger, 0.4f);
                Set(gamepad.leftTrigger, 0.3f);//onlyRelease 不超过press-threshold只回落release是不会触发的
                Assert.That(trace, Started(action, control: gamepad.leftTrigger, value: 0.4f));
                trace.Clear();
                
                
            }
        }
        
        [Test]
        [TestCase(InputActionType.Value)]
        [TestCase(InputActionType.Button)]
        [TestCase(InputActionType.PassThrough)]
        public void Actions_PressAndRelease(InputActionType type)
        {
            var gamepad = InputSystem.AddDevice<Gamepad>();

            InputSystem.settings.defaultButtonPressPoint = 0.5f;
            InputSystem.settings.buttonReleaseThreshold = 0.75f; // Puts release point at 0.375.

            var action = new InputAction(type:type, binding: "<Gamepad>/leftTrigger", interactions: "press(behavior=2)");
            action.Enable();

            using (var trace = new InputActionTrace(action))
            {
                Set(gamepad.leftTrigger, 0.35f);
                Assert.That(trace, Started(action, control: gamepad.leftTrigger, value: 0.35f));
                trace.Clear();

                Set(gamepad.leftTrigger, 0.5f);
                Assert.That(trace, Performed(action, control: gamepad.leftTrigger, value: 0.5f));
                trace.Clear();

                Set(gamepad.leftTrigger, 0.6f);
                Assert.That(trace, Is.Empty);
                trace.Clear();
                
                Set(gamepad.leftTrigger, 0.4f);
                Assert.That(trace, Is.Empty);
                trace.Clear();

                Set(gamepad.leftTrigger, 0.3f);
                Assert.That(trace,
                    Performed(action, control: gamepad.leftTrigger, value: 0.3f));
                trace.Clear();
                
                Set(gamepad.leftTrigger, 0);
                Assert.That(trace, Is.Empty);
                trace.Clear();
                
                Set(gamepad.leftTrigger, 0.9f);
                Assert.That(trace, 
                    Started(action, control: gamepad.leftTrigger, value: 0.9f)
                        .AndThen(Performed(action, control: gamepad.leftTrigger, value: 0.9f)));
                trace.Clear();
                
                Set(gamepad.leftTrigger, 0);
                Assert.That(trace, 
                    Performed(action, control: gamepad.leftTrigger, value: 0f)
                        .AndThen(Canceled(action, control: gamepad.leftTrigger, value: 0f)));
                trace.Clear();
                
                Set(gamepad.leftTrigger, 0.4f);
                Set(gamepad.leftTrigger, 0.3f);//press&release 不超过press-threshold只回落release是不会触发的
                Assert.That(trace, Started(action, control: gamepad.leftTrigger, value: 0.4f));
                trace.Clear();
            }
        }
        
    }

}

UnityEvent方式注册回调关键逻辑

  1. PlayerInput.InitializeActions()
    UnityEvent方式注册回调的核心在于PlayerInput组件enable时进行InitializeActions初始化操作
    【Unity学习笔记】New Input System 部分源码和测试用例补充,unity,unity,InputSystem,源码,PlayerInput,UnityEvent
  2. PlayerInputEditor
    但对于上面ActionEvent的eventId怎么匹配上actionId,则要看PlayerInputEditor.OnInspectorGUI()OnActionAssetChange()
    【Unity学习笔记】New Input System 部分源码和测试用例补充,unity,unity,InputSystem,源码,PlayerInput,UnityEvent
  3. UnityEvent<T0>
    上面action触发后,对应actionEvent调用Invoke,但此时真正的回调方法并没有注册,是在下面的InvokableCall中注册的,最后在InvokableCall.Invoke中执行最终的方法回调,处理输入处理。
    【Unity学习笔记】New Input System 部分源码和测试用例补充,unity,unity,InputSystem,源码,PlayerInput,UnityEvent
  4. InvokableCall<T1> 注册回调
    【Unity学习笔记】New Input System 部分源码和测试用例补充,unity,unity,InputSystem,源码,PlayerInput,UnityEvent
    从上面看,UnityAction方式远比原生C#事件复杂得多。(而且我也并没完全弄懂)

到了这里,关于【Unity学习笔记】New Input System 部分源码和测试用例补充的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Unity_Input System】Input System新输入系统(三)——在游戏中更换按键绑定

    Binding只由一个部分组成,一般绑定单个按键或者摇杆 CompositeBinding由两个以上部分组成,一般是用于将多个按键组成虚拟轴 更换按键绑定时,Binding和Composite Binding需要分别处理,对Composite Binding需要循环各个部分进行修改。 可以用InputBinding.isComposite来判断是否是Composite Bind

    2024年02月04日
    浏览(43)
  • Unity 新版输入系统(Input System)

    官方教程 注意 新的输入法系统需要 Unity 2019.4+ 和 .NET 4 运行时。它不适用于 .NET 3.5 的项目。 教程版本:Unity 2021.3.26 导航栏 - Window - Package Manager 选择 Unity Registry 在列表中找到 Input System 点击 Install 安装 点击 Yes 启用新版 Input System 等待Unity重新启动 Unity 默认会同时启用旧版和

    2024年04月23日
    浏览(45)
  • Unity 3D Input System的使用

    Input System是Unity新推出的输入系统,可以用作以前Input Manager的更具扩展性和可自定义性的替代方案。这里我将使用Unity 3D的Input System来实现一个FPS第一人称的游戏场景。 演示效果如下: 新建一个Unity 3D项目,在Asset Store里面下载StarterAssets - FirstPerson,然后在Package Manager里面导

    2024年02月20日
    浏览(32)
  • Unity新(Input System)老(Input Manager)输入系统代码对比

    以下介绍都是基于Unity2022版本 一、键盘操作 当w键按下时 当w键抬起时 当w键按着时 二、鼠标操作 获取鼠标位置 获取鼠标滚轮 获取鼠标左键按下 获取鼠标右键抬起 获取鼠标中间按着

    2024年02月16日
    浏览(43)
  • 第五十二章 Unity Input System 新输入系统

    新输入系统InputSystem是2019年Unity新推出的插件。请注意,Unity默认使用旧的Input Manager,新的Input System处于未启用状态。当你安装Input System组件时,Unity会询问你是否启用新的输入系统。如果你选择Yes,Unity会启用新的并禁用旧的,之后编辑器将重新启动。具体的操作是,打开包

    2024年02月07日
    浏览(36)
  • 【Unity 学习笔记】 Sprite的部分属性和功能

    在unity2D中,每导入一个图片资源,unity都会为其添加一些属性,这些属性可以在对应图片资源的Inspector中查看。在unity中,图片资源被称作sprite,翻译成中文就是“精灵”。精灵sprite指的是一张集成场景中的一部分图像,个人形象地理解就是unity中的图片资源都是游戏场景(或

    2024年02月07日
    浏览(35)
  • Unity VR:XR Interaction Toolkit 输入系统(Input System):获取手柄的输入

    输入系统是 VR 应用中非常重要的一部分。我们通常需要获取 VR 手柄上某个按键的输入,然后将其作用到应用中,比如按下手柄的 Grip 键进行抓取。在我的其他 Unity XR Interaction Toolkit 的开发教程里,已经有介绍如何去获取手柄的输入。那么这篇教程我将做一个总结,将相关的

    2024年02月12日
    浏览(41)
  • 【学习笔记】Vue3源码解析:第五部分 - 实现渲染(2)

    课程地址:【已完结】全网最详细Vue3源码解析!(一行行带你手写Vue3源码) 第五部分-:(对应课程的第33 - 35节) 第33节:《讲解组件渲染流程》 1、在 render 函数中拿到虚拟dom vnode后,编写patch函数,它接收3个参数:null、vnode、container,分别代表:上一次的虚拟dom即旧的虚

    2024年04月23日
    浏览(50)
  • uniapp小程序,部分 ios 真机测试出现 input 输入框内容 跳动 问题

    前言 :最近在写uniapp小程序,遇到一个问题,在此记录一下,也希望能帮助到一些小伙伴。 问题 :表单输入的时候,在个别 ios 真机上面测试时,快速输入和快速删除,会发现输入的文字或者数字,有自动删除、闪动、来回跳动等现象,经过查询各种资料,以及咨询官方得

    2024年02月11日
    浏览(42)
  • 【Unity InputSystem】实用教程-实现角色移动跳跃功能(Input System在PC端鼠键!手机端触摸屏!主机手柄!三端使用教程)

    在上一篇文章当中我们手把手的跟大家分享了InputSystem的基础操作(如何使用5种不同的方式获取到InputSystem种的输入检查信息)。那么接下来我们继续分享一下在我们获取到InputSystem输入信息后,如何使用检测到的信息实现我们对应的输入功能。 本次我们使用的Unity编辑器版本为

    2024年02月11日
    浏览(52)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包