模拟 Unity InputSystem 鼠标事件

问题描述

我正忙于开发一款玩家可以通过单击鼠标左键进行攻击的游戏。我有一个 InputManager 类,它将检查输入事件,然后调用 UnityEvent。这都是在玩游戏时起作用的。但是我正在努力在我的测试中模拟鼠标事件。测试如下:

[Test]
public void MouseAttacktest()
{
    var attacked = false;
    var inputManagerObject = new GameObject();
    var inputManager = inputManagerObject.AddComponent<InputManager>();
    inputManager.OnAttack.AddListener(() => attacked = true);

    var mouse = InputSystem.AddDevice<Mouse>();
    using (StateEvent.From(mouse,out var eventPtr))
    {
        mouse.leftButton.WriteValueIntoEvent(true,eventPtr); // Fails on this line
        InputSystem.QueueEvent(eventPtr);
    }

    Assert.IsTrue(attacked);
}

这是输出

MouseAttackTest (0,091s)
---
System.ArgumentException : Expecting control of type 'Boolean' but got 'ButtonControl'
---
at UnityEngine.Inputsystem.inputControlExtensions.WriteValueIntoEvent[TValue] (UnityEngine.Inputsystem.inputControl control,TValue value,UnityEngine.InputSystem.LowLevel.InputEventPtr eventPtr) [0x0003f] in C:\Dev\Game\Library\PackageCache\com.unity.inputsystem@1.0.2\InputSystem\Controls\InputControlExtensions.cs:369 
  at Game.Tests.Play.InputSystemTests.MouseAttackTest () [0x00046] in C:\Dev\Game\Assets\Scripts\Tests\Play\InputSystemTests.cs:24 
  at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj,System.Reflection.BindingFlags invokeAttr,System.Reflection.Binder binder,System.Object[] parameters,System.Globalization.CultureInfo culture) [0x00032] in <9577ac7a62ef43179789031239ba8798>:0

我尝试在线搜索示例,但没有真实示例,甚至没有适当的文档。关于如何模拟这些事件的任何想法?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)