Question by
HenryJooste · Apr 11, 2021 at 08:18 AM ·
inputtestingtest
Simulate InputSystem mouse events
Hey everybody. I'm busy working on a game where the player can attack by clicking the left mouse button. I have a InputManager class that will check the input events and then invoke a UnityEvent. This is all working while playing the game. But I'm struggling to simulate mouse events in my tests. Here's the test:
[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);
}
And here's the output:
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
I tried to search for examples online, but I can't find any real examples. There's also not a lot of information on how to use these methods in the documentation. Any ideas on how I can simulate these events?
Comment