Skip to content

Commit 74c5096

Browse files
committed
Pair simulated touchscreen with PlayerInput user
1 parent 8fbff9b commit 74c5096

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ however, it has to be formatted properly to pass verification tests.
1717
- Fixed "MissingReferenceException" errors when closing an in-game dropdown field [ISXB-1081](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1081).
1818
- Fixed potential crash on Mac when using stale references to deleted InputDevice objects [ISXB-606](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-606).
1919
- Fixed conditional compilation for non-editor analytics on platforms not enabling analytics.
20+
- Fixed simulated touch input not working with PlayerInput component [ISXB-483](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-483)
2021

2122
### Changed
2223
- Renamed editor Resources directories to PackageResources to fix package validation warnings.

Packages/com.unity.inputsystem/InputSystem/Plugins/EnhancedTouch/TouchSimulation.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Unity.Collections.LowLevel.Unsafe;
33
using UnityEngine.InputSystem.Controls;
44
using UnityEngine.InputSystem.LowLevel;
5+
using UnityEngine.InputSystem.Users;
56
using UnityEngine.InputSystem.Utilities;
67
#if UNITY_EDITOR
78
using UnityEditor;
@@ -236,6 +237,14 @@ private void OnDeviceChange(InputDevice device, InputDeviceChange change)
236237
}
237238
}
238239
}
240+
241+
private void TryPerformUserPairingOfSimulatedTouchscreen()
242+
{
243+
using (InputActionRebindingExtensions.DeferBindingResolution())
244+
{
245+
InputUser.PerformPairingWithDevice(simulatedTouchscreen, m_PlayerInput.user);
246+
}
247+
}
239248

240249
protected void OnEnable()
241250
{
@@ -264,6 +273,28 @@ protected void OnEnable()
264273

265274
InputSystem.onDeviceChange += m_OnDeviceChange;
266275
InputSystem.onEvent += m_OnEvent;
276+
277+
// In case there's a PlayerInput component in the scene, we want to pair the simulated touchscreen to
278+
// the PlayerInput user. The touchscreen device does not queue events so we need to pair it to the user.
279+
m_PlayerInput = PlayerInput.s_AllActivePlayersCount > 0 ? PlayerInput.s_AllActivePlayers[0] : null;
280+
if (m_PlayerInput)
281+
{
282+
TryPerformUserPairingOfSimulatedTouchscreen();
283+
InputUser.onChange += PairSimulatedTouchscreenOnChange;
284+
}
285+
}
286+
private void PairSimulatedTouchscreenOnChange(InputUser user, InputUserChange change, InputDevice device)
287+
{
288+
if (change == InputUserChange.ControlSchemeChanged)
289+
{
290+
// When the control scheme is changed, we pair the simulated touchscreen in case
291+
// the new control scheme has action bindings that can be used in it.
292+
// E.g. UI/Point action with touch/position binding is set to Keyboard&Mouse control scheme.
293+
if (!user.pairedDevices.ContainsReference(simulatedTouchscreen))
294+
{
295+
TryPerformUserPairingOfSimulatedTouchscreen();
296+
}
297+
}
267298
}
268299

269300
protected void OnDisable()
@@ -284,6 +315,8 @@ protected void OnDisable()
284315

285316
InputSystem.onDeviceChange -= m_OnDeviceChange;
286317
InputSystem.onEvent -= m_OnEvent;
318+
if(m_PlayerInput)
319+
InputUser.onChange -= PairSimulatedTouchscreenOnChange;
287320
}
288321

289322
private unsafe void UpdateTouch(int touchIndex, int pointerIndex, TouchPhase phase, InputEventPtr eventPtr = default)
@@ -356,6 +389,7 @@ private unsafe void UpdateTouch(int touchIndex, int pointerIndex, TouchPhase pha
356389
[NonSerialized] private Action<InputEventPtr, InputDevice> m_OnEvent;
357390

358391
internal static TouchSimulation s_Instance;
392+
private PlayerInput m_PlayerInput;
359393

360394
#if UNITY_EDITOR
361395
static TouchSimulation()

0 commit comments

Comments
 (0)