Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Resolved Testing a left mouse button down at a certain position

Discussion in 'Input System' started by ManuelRauber, May 8, 2022.

  1. ManuelRauber

    ManuelRauber

    Joined:
    Apr 3, 2015
    Posts:
    122
    Hi,

    I'm currently writing a Unit Test where I have this action:

    upload_2022-5-8_13-56-53.png
    upload_2022-5-8_13-57-9.png

    It's just a Vector2Control with a Composite with One Modifier, where the Modifier is the left mouse button and the binding is the mouse position.

    Then, I've the following test for that:

    Code (CSharp):
    1. public class InputReaderSOTests : InputTestFixture
    2. {
    3.   [Test]
    4.   public void DoesExecuteClickEventWhenEnabled()
    5.   {
    6.     var mouse = InputSystem.AddDevice<Mouse>();
    7.      
    8.     var inputReader = ScriptableObject.CreateInstance<InputReaderSO>();
    9.     inputReader.EnablePlayerControls();
    10.  
    11.     var clickedVector = Vector2.zero;
    12.     inputReader.Click += position => clickedVector = position;
    13.      
    14.     // Works
    15.     /*InputSystem.QueueStateEvent(mouse, new MouseState() { position = new Vector2(1337, 1337)}.WithButton(MouseButton.Left ));
    16.     InputSystem.Update();*/
    17.      
    18.     // Does not work
    19.     /*Set(mouse.position, new Vector2(1337, 1337), queueEventOnly: false);
    20.     Click(mouse.leftButton);*/
    21.      
    22.     clickedVector.Should().Be(new Vector2(1337, 1337));
    23.   }
    24. }
    In the documentation there's a entry about using Set/Click/Press etc. for setting the state.

    As you can see in the example, I set the mouse.position and then I click the left mouse button. However, the test reports that the mouse was not clicked on that position.

    However, if I use the other method above using InputSystem.QueueStateEvent and InputSystem.Update it works as expected.

    I feel that it also should work with set Set and Click methods. Any idea what I'm missing?

    Thanks!
     

    Attached Files:

  2. ManuelRauber

    ManuelRauber

    Joined:
    Apr 3, 2015
    Posts:
    122
    Dang. Some minutes later I found it. :)

    Instead of using Set, I have to use Move:

    Code (CSharp):
    1. Move(mouse.position, new Vector2(1337, 1337), queueEventOnly: true);
    2. Click(mouse.leftButton);
    Then it works as expected, nice!
     
  3. stephyswe

    stephyswe

    Joined:
    Aug 23, 2017
    Posts:
    2
    are you using this in a EditTest? How to move a mouse without Screen? :)