Search Unity

Input System Update

Discussion in 'New Input System' started by Rene-Damm, Dec 12, 2017.

  1. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    964
    1.0.0-preview.5 is out now. Most importantly fixes the kb&mouse control scheme regression introduced in 1.0.0-preview.4. Full changelog here.

    Status-wise, no change to the agenda. We'll keep whittling down the bug pile. I expect we have another package with more fixes in 2 weeks.
     
  2. spanagiotis

    spanagiotis

    Joined:
    Feb 5, 2013
    Posts:
    12
    Is software cursor from the VirtualMouse component supposed to trigger IPointer events?
    If I have selected "Use hardware cursor if available" my IPointer events work, but "Software Cursor" does not.
     
  3. spanagiotis

    spanagiotis

    Joined:
    Feb 5, 2013
    Posts:
    12
    I yoinked the button state change code for my own virtual mouse and I can get all IPointer events to fire.

    The software pointer selection in the new VirtualMouse component however doesn't seem to fire any IPointer event.

    Edit:

    A few more things to note:
    The canvas clamping doesn't work. It seems m_Canvas is null, even after setting a cursor graphic.

    If I manually grab the canvas from the cursor graphic during update and populate m_Canvas the clamping works, but it's coordinate space differs completely from newPosition. This explains why IPointer events aren't going off, the newPosition is not in the correct coordinate space for the canvas.

    See attached picture. The red dot is where the newPosition cursor is at. The magenta box is the true pixel rect location, and the green dot shows where that red dot is in actual canvas space.

    upload_2020-2-14_17-32-30.png
     
    Last edited: Feb 15, 2020 at 12:34 AM
  4. spanagiotis

    spanagiotis

    Joined:
    Feb 5, 2013
    Posts:
    12
    Incase anyone is looking for a quick working virtual cursor:
    You'll need to replace my PlayerInput lines with your control bindings.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3. using UnityEngine.InputSystem.LowLevel;
    4.  
    5. public class UICursorWidget : MonoBehaviour
    6. {
    7.     [SerializeField] private RectTransform _canvasTransform = null;
    8.     [SerializeField] private float _cursorSpeed = 0.0f;
    9.     private RectTransform _rectTransform = null;
    10.     private Mouse _virtualMouseDevice = null;
    11.     private Vector2 _virutalMousePosition = new Vector2(0, 0);
    12.  
    13.     public void Awake()
    14.     {
    15.         PlayerInput.Controls.UI.Confirm.performed += _ConfirmOnperformed;
    16.         PlayerInput.Controls.UI.Confirm.canceled += _ConfirmOncanceled;
    17.         Cursor.visible = false;
    18.  
    19.         _virtualMouseDevice = InputSystem.AddDevice<Mouse>();
    20.         _rectTransform = GetComponent<RectTransform>();
    21.         _rectTransform.anchoredPosition = _virutalMousePosition;
    22.         _UpdateMouseMovementState(_virtualMouseDevice, _CalculateRealScreenPosition(), Vector2.zero);
    23.     }
    24.  
    25.     public void Update()
    26.     {
    27.         Vector2 stickInput = PlayerInput.Controls.UI.LeftStick.ReadValue<Vector2>();
    28.         Vector2 stickStep = stickInput * (Time.deltaTime * _cursorSpeed);
    29.         if (stickStep == Vector2.zero) return;
    30.         _virutalMousePosition += stickStep;
    31.         _virutalMousePosition.x = Mathf.Clamp(_virutalMousePosition.x, -_canvasTransform.sizeDelta.x  *.5f, _canvasTransform.sizeDelta.x  *.5f);
    32.         _virutalMousePosition.y = Mathf.Clamp(_virutalMousePosition.y, -_canvasTransform.sizeDelta.y  *.5f, _canvasTransform.sizeDelta.y  *.5f);
    33.         _UpdateMouseMovementState(_virtualMouseDevice, _CalculateRealScreenPosition(), stickStep);
    34.         _rectTransform.anchoredPosition = _virutalMousePosition;
    35.     }
    36.  
    37.     private Vector2 _CalculateRealScreenPosition()
    38.     {
    39.         return _virutalMousePosition * _canvasTransform.localScale + new Vector2(Screen.width * .5f, Screen.height - Screen.height * .5f);
    40.     }
    41.  
    42.     private void _UpdateMouseMovementState(Mouse device, Vector2 position, Vector2 delta)
    43.     {
    44.         InputSystem.QueueStateEvent(device, new MouseState
    45.         {
    46.             position = position,
    47.             delta = delta
    48.         });
    49.     }
    50.  
    51.     private void _UpdateMouseButtonState(MouseButton button, bool isPressed)
    52.     {
    53.         _virtualMouseDevice.CopyState<MouseState>(out var mouseState);
    54.         mouseState.WithButton(button, isPressed);
    55.         InputState.Change(_virtualMouseDevice, mouseState);
    56.     }
    57.  
    58.     private void _ConfirmOnperformed(InputAction.CallbackContext obj)
    59.     {
    60.         _UpdateMouseButtonState(MouseButton.Left, true);
    61.     }
    62.  
    63.     private void _ConfirmOncanceled(InputAction.CallbackContext obj)
    64.     {
    65.         _UpdateMouseButtonState(MouseButton.Left, false);
    66.     }
    67. }
     
    Last edited: Feb 15, 2020 at 1:27 AM
  5. spanagiotis

    spanagiotis

    Joined:
    Feb 5, 2013
    Posts:
    12
    Final find:
    The virtual mouse script does *not* work with UI Scale Mode set to Scale With Screen Size.
    Constant Pixel Size works fine, but it won't scale with the screen size setting.
     
unityunity