Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Fake Input to UI Toolkit?

Discussion in 'UI Toolkit' started by osaharoy, Jul 28, 2020.

  1. osaharoy

    osaharoy

    Joined:
    May 6, 2020
    Posts:
    5
    We're using the the UI Toolkit and we love it. For testing purposes, we'd like to have a virtual mouse click on some UI Toolkit buttons to check they do the right thing. Can anyone think of a way to achieve this?

    We're using the new input system so I'm not sure how that affects things.

    Thanks for the help :)
     
  2. Kirsche

    Kirsche

    Joined:
    Apr 14, 2015
    Posts:
    120
  3. osaharoy

    osaharoy

    Joined:
    May 6, 2020
    Posts:
    5
    Thanks so much :) here's my code for synthesizing a click on a button:

    Code (CSharp):
    1.  
    2. var evt1 = new Event()
    3. {
    4.     type = EventType.MouseDown,
    5.     button = 0,
    6.     mousePosition = buttonCentre,
    7.     clickCount = 1
    8. };
    9.      
    10. using (MouseDownEvent mouseDownEvent = MouseDownEvent.GetPooled(evt1))
    11. {
    12.     rootVisualElement.SendEvent(mouseDownEvent);
    13. }
    14.          
    15. var evt2 = new Event()
    16. {
    17.     type = EventType.MouseUp,
    18.     button = 0,
    19.     mousePosition = buttonCentre,
    20.     clickCount = 1
    21. };
    22.  
    23. using (MouseUpEvent mouseUpEvent = MouseUpEvent.GetPooled(evt2))
    24. {
    25.     rootVisualElement.SendEvent(mouseUpEvent);
    26. }
    27.  
    (buttonCentre is any Vector2)
     
    Arlorean likes this.
  4. AlienFreak

    AlienFreak

    Joined:
    Jul 2, 2012
    Posts:
    40
    For an actual case where you need mouse input to be triggered by code here's a good working solution. Case in point: Cursor.lockState doesn't work in Editor properly but will work in Standalone on Mac or PC builds just fine.

    For the Editor, I came up with a very legit way by code to move the mouse cursor and another routine to force a mouse click event in the center of the game window because Unity is so annoying in not letting you do this when using Cursor.lockState. Now you can set Cursor.lockState and/or Cursor.visibility in Start() and call either of the following below to force the mouse to behave. (Verified working in macOS Big Sur, Unity 2021.1.17)

    Stupid easy way to force mouse cursor position to center of game window in editor only from code:
    Code (CSharp):
    1. using UnityEngine.InputSystem;
    2. using UnityEngine.InputSystem.LowLevel;
    3.  
    4. public static void ForceMousePositionToCenterOfGameWindow()
    5.     {
    6. #if UNITY_EDITOR
    7.         // Force the mouse to be in the middle of the game screen
    8.         var game = UnityEditor.EditorWindow.GetWindow(typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.GameView"));
    9.         Vector2 warpPosition = game.rootVisualElement.contentRect.center;  // never let it move
    10.         Mouse.current.WarpCursorPosition(warpPosition);
    11.         InputState.Change(Mouse.current.position, warpPosition);
    12. #endif
    13.     }

    Stupid easy way to force click in game window in editor only from code:
    Code (CSharp):
    1. using UnityEngine.InputSystem;
    2. using UnityEngine.InputSystem.LowLevel;
    3.  
    4. public static void ForceClickMouseButtonInCenterOfGameWindow()
    5.     {
    6. #if UNITY_EDITOR
    7.         var game = UnityEditor.EditorWindow.GetWindow(typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.GameView"));
    8.         Vector2 gameWindowCenter = game.rootVisualElement.contentRect.center;
    9.  
    10.         Event leftClickDown = new Event();
    11.         leftClickDown.button = 0;
    12.         leftClickDown.clickCount = 1;
    13.         leftClickDown.type = EventType.MouseDown;
    14.         leftClickDown.mousePosition = gameWindow;
    15.  
    16.         game.SendEvent(leftClickDown);
    17. #endif
    18.     }