diff --git a/Assets/Samples/GamepadMouseCursor/GamepadMouseCursorUIActions.inputactions b/Assets/Samples/GamepadMouseCursor/GamepadMouseCursorUIActions.inputactions index fcec675666..64d4e7c143 100644 --- a/Assets/Samples/GamepadMouseCursor/GamepadMouseCursorUIActions.inputactions +++ b/Assets/Samples/GamepadMouseCursor/GamepadMouseCursorUIActions.inputactions @@ -34,7 +34,7 @@ { "name": "", "id": "39af39e2-c75c-421e-84bf-1e4ff9793ab4", - "path": "/position", + "path": "/position", "interactions": "", "processors": "", "groups": "", @@ -45,7 +45,7 @@ { "name": "", "id": "f9d5a98e-68a3-44c8-a096-a1a30237ca81", - "path": "/leftButton", + "path": "/leftButton", "interactions": "", "processors": "", "groups": "", @@ -56,7 +56,7 @@ { "name": "", "id": "7ffd960c-11ef-4fc0-b91b-4cbd02eba1b8", - "path": "/scroll", + "path": "/scroll", "interactions": "", "processors": "", "groups": "", diff --git a/Assets/Samples/GamepadMouseCursor/README.md b/Assets/Samples/GamepadMouseCursor/README.md index 1b9a255f1e..54ddcca2fb 100644 --- a/Assets/Samples/GamepadMouseCursor/README.md +++ b/Assets/Samples/GamepadMouseCursor/README.md @@ -7,10 +7,11 @@ This sample demonstrates how to set this up with the input system. 1) It uses a custom [actions file](./GamepadMouseCursorUIActions.inputactions) for feeding input to the UI as the default actions are set up for gamepad navigation – something we don't want here as it would conflict with gamepad input being used for virtual cursor navigation. 2) Note how `InputSystemUIInputModule` on the `EventSystem` GameObject is set up to reference actions from that file. 3) The key component to take a look at is `VirtualMouseInput` on `Canvas >> Cursor`. The component is set up to receive input from the gamepad and translates it into motion on the `RectTransform` it is given. When going into play mode, you should also see a `Virtual Mouse` being added to the devices by the component. -4) Note how the anchor position on the `RectTransform` is set to bottom left. This way the coordinate system responds to how mouse screen space operates. -5) Note how `Cursor` is the last child of `Canvas` so that it draws on top of everything else. -6) Note that `Raycast Target` on the `Image` component of the cursor is turned off to avoid raycasts from the mouse cursor hitting the cursor itself. -7) Note that `Cursor Mode` on the `VirtualMouseInput` component is set to `Hardware Cursor If Available`. This will cause the component to look for a system mouse. If present, the system mouse is disabled and the system mouse cursor is warped to the virtual mouse position using `Mouse.WarpCursorPosition`. If no system mouse is present, `Cursor Graphic` will be used as a software mouse cursor. +4) Note how `Cursor` is the last child of `Canvas` so that it draws on top of everything else. +5) Note that `Raycast Target` on the `Image` component of the cursor is turned off to avoid raycasts from the mouse cursor hitting the cursor itself. +6) Note that `Cursor Mode` on the `VirtualMouseInput` component is set to `Hardware Cursor If Available`. This will cause the component to look for a system mouse. If present, the system mouse is disabled and the system mouse cursor is warped to the virtual mouse position using `Mouse.WarpCursorPosition`. If no system mouse is present, `Cursor Graphic` will be used as a software mouse cursor. + +>NOTE: The `VirtualMouseInput` component currently does not work correctly with world-space canvases. # Licenses diff --git a/Packages/com.unity.inputsystem/CHANGELOG.md b/Packages/com.unity.inputsystem/CHANGELOG.md index 36548dfd48..4c0c52fe17 100644 --- a/Packages/com.unity.inputsystem/CHANGELOG.md +++ b/Packages/com.unity.inputsystem/CHANGELOG.md @@ -13,6 +13,11 @@ however, it has to be formatted properly to pass verification tests. ### Fixed - Fixed unclosed profiler marker in `InvokeCallbacksSafe_AnyCallbackReturnsTrue` which would lead to eventually broken profiler traces in some cases like using `PlayerInput` (case ISXB-393). +- `VirtualMouseInput` not working correctly with `CanvasScaler` settings other than `ConstantPixelSize`. Fix contributed by [ad-walker](https://github.com/ad-walker) in [#1078](https://github.com/Unity-Technologies/InputSystem/pull/1078). + +### Added + +- Added an explicit `Disable System Mouse` setting to `VirtualMouseInput` to allow toggling the default behavior off which makes the component turn off the system mouse while it is enabled (so as to suppress the system mouse doubling the input activity on the VirtualMouse). ## [1.5.0] - 2023-01-24 diff --git a/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/VirtualMouseInput.cs b/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/VirtualMouseInput.cs index 41499e2052..8b8750ca19 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/VirtualMouseInput.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/VirtualMouseInput.cs @@ -5,6 +5,8 @@ ////TODO: respect cursor lock mode +////TODO: find a way to automatically turn the gamepad cursor on/off based on gamepad use + ////TODO: investigate how driving the HW cursor behaves when FPS drops low //// (also, maybe we can add support where we turn the gamepad mouse on and off automatically based on whether the system mouse is used) @@ -14,6 +16,8 @@ ////TODO: make it work with PlayerInput such that it will automatically look up actions in the actual PlayerInput instance it is used with (based on the action IDs it has) +////FIXME: make this work with world-space canvases + ////REVIEW: should we default the SW cursor position to the center of the screen? ////REVIEW: consider this for inclusion directly in the input system @@ -78,9 +82,10 @@ public float cursorSpeed /// that is made to correspond to the position of . If this is set to and there is a native device present, /// the component will take over that mouse device and disable it (so as for it to not also generate position - /// updates). It will then use to move the system mouse cursor to - /// correspond to the position of the . In this case, - /// will be disabled and will not be updated. + /// updates) except when this is explicitly disabled via . It will then use + /// to move the system mouse cursor to correspond to the position of the + /// . In this case, will be disabled and + /// will not be updated. /// /// Whether the system mouse cursor (if present) should be made to correspond with the virtual mouse position. /// @@ -100,7 +105,8 @@ public CursorMode cursorMode // If we're turning it off, make sure we re-enable the system mouse. if (m_CursorMode == CursorMode.HardwareCursorIfAvailable && m_SystemMouse != null) { - InputSystem.EnableDevice(m_SystemMouse); + if (m_DisableSystemMouse) + InputSystem.EnableDevice(m_SystemMouse); m_SystemMouse = null; } @@ -157,11 +163,51 @@ public float scrollSpeed /// public Mouse virtualMouse => m_VirtualMouse; + /// + /// If is set to , whether to + /// disable the system mouse, if present. This is on by default. + /// + /// If true, the system mouse will be disabled if present and while the VirtualMouseInput component is active. + /// + /// If the component drives the system mouse cursor rather than using a software cursor, the system mouse cursor position will + /// get warped around using . This, however, has the side-effect of generating input + /// on the system device. In other words, mouse motion from the gamepad will come in through both + /// the VirtualMouse created for the gamepad and the added by the system (with a one frame lag for + /// the latter). + /// + /// To avoid this, the system mouse will by default get disabled while the VirtualMouseInput component is enabled. Thus, + /// the system will not receive input while the VirtualMouseInput component is enabled. The idea here + /// is that the application should determine when the gamepad is used and only turn on the gamepad cursor while this is + /// the case. One possible approach is to set up a control scheme for gamepads and enable the gamepad mouse cursor only + /// while the gamepad scheme is active. + /// + /// However, by setting this property to false, the disabling of the system device can be suppressed. + /// This means the system mouse will stay fully functional. You will, however, see concurrent input on both the VirtualMouse + /// and the system mouse. This may, for example, interfere with automatic control scheme switching (which is why the disabling + /// is on by default). + /// + public bool disableSystemMouse + { + get => m_DisableSystemMouse; + set + { + if (value == m_DisableSystemMouse) + return; + m_DisableSystemMouse = value; + + if (m_SystemMouse != null) + { + if (value) + InputSystem.DisableDevice(m_SystemMouse); + else + InputSystem.EnableDevice(m_SystemMouse); + } + } + } + /// /// The Vector2 stick input that drives the mouse cursor, i.e. on - /// and the anchoredPosition - /// on (if set). + /// and the position on (if set). /// /// Stick input that drives cursor position. /// @@ -294,7 +340,7 @@ protected void OnEnable() // Set initial cursor position. if (m_CursorTransform != null) { - var position = m_CursorTransform.anchoredPosition; + var position = m_CursorTransform.position; InputState.Change(m_VirtualMouse.position, position); m_SystemMouse?.WarpCursorPosition(position); } @@ -388,7 +434,8 @@ private void TryEnableHardwareCursor() return; } - InputSystem.DisableDevice(m_SystemMouse); + if (m_DisableSystemMouse) + InputSystem.DisableDevice(m_SystemMouse); // Sync position. if (m_VirtualMouse != null) @@ -450,7 +497,7 @@ private void UpdateMotion() if (m_CursorTransform != null && (m_CursorMode == CursorMode.SoftwareCursor || (m_CursorMode == CursorMode.HardwareCursorIfAvailable && m_SystemMouse == null))) - m_CursorTransform.anchoredPosition = newPosition; + m_CursorTransform.position = newPosition; m_LastStickValue = stickValue; m_LastTime = currentTime; @@ -475,6 +522,11 @@ private void UpdateMotion() [Tooltip("Whether the component should set the cursor position of the hardware mouse cursor, if one is available. If so, " + "the software cursor pointed (to by 'Cursor Graphic') will be hidden.")] [SerializeField] private CursorMode m_CursorMode; + [Tooltip("With 'Cursor Mode' set to 'Hardware Cursor If Available', whether to disable the system mouse. The cursor warping that is used " + + "to drive the HW cursor will generate input on the mouse device which mirrors the input on the virtual mouse. By enabling this flag, " + + "this input can be suppressed by disabling the system mouse altogether. Note that this will make *no* input from the system mouse come " + + "through while the VirtualMouseInput component is active.")] + [SerializeField] private bool m_DisableSystemMouse = true; [Tooltip("The graphic that represents the software cursor. This is hidden if a hardware cursor (see 'Cursor Mode') is used.")] [SerializeField] private Graphic m_CursorGraphic; [Tooltip("The transform for the software cursor. Will only be set if a software cursor is used (see 'Cursor Mode'). Moving the cursor "