Skip to content

Commit c40f0ce

Browse files
committed
FIX: [UUM-145509] Simulator Plugin disabling input devices always.
Fix an issue where the Simulator Window would disable all input devices while open even though the window was not focused. This caused "Game View" to not properly receive input events. - Added a UI foldout with helpbox explaining this behavior. - Used created UIElement to capture the panel `InputSystemPlugin` connects to. - Enable/Disable devices based on SimulatorWindow focus state.
1 parent 93845e5 commit c40f0ce

3 files changed

Lines changed: 135 additions & 17 deletions

File tree

Assets/Tests/InputSystem/Plugins/DeviceSimulatorTests.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,69 @@ public void TouchscreenAddedAndRemoved()
6464
Assert.IsFalse(touchscreen.added);
6565
}
6666

67+
[Test]
68+
[Category("Device Simulator")]
69+
public void ConflictingDevicesAreNotDisabledOnCreate()
70+
{
71+
runtime.ReportNewInputDevice<Mouse>();
72+
InputSystem.Update();
73+
var mouse = Mouse.current;
74+
Assert.That(mouse.native, Is.True);
75+
76+
var plugin = new InputSystemPlugin();
77+
plugin.OnCreate();
78+
79+
// Conflicting devices are only disabled once the Simulator gains focus, not on create.
80+
Assert.That(mouse.enabled, Is.True);
81+
82+
plugin.OnDestroy();
83+
}
84+
85+
[Test]
86+
[Category("Device Simulator")]
87+
public void ConflictingDeviceAddedWhileSimulatorFocused_IsDisabledThenReenabledOnDestroy()
88+
{
89+
var plugin = new InputSystemPlugin();
90+
plugin.OnCreate();
91+
92+
// Simulate the Simulator window being the focused window (bypasses the panel-based OnUpdate).
93+
SetPrivateField(plugin, "m_ConflictingDevicesDisabled", true);
94+
95+
runtime.ReportNewInputDevice<Mouse>();
96+
InputSystem.Update();
97+
var mouse = Mouse.current;
98+
99+
Assert.That(mouse.native, Is.True);
100+
Assert.That(mouse.enabled, Is.False); // disabled via the OnDeviceChange gate
101+
102+
plugin.OnDestroy();
103+
Assert.That(mouse.enabled, Is.True); // ReenableConflictingDevices restores it
104+
}
105+
106+
[Test]
107+
[Category("Device Simulator")]
108+
public void ConflictingDeviceAddedWhileSimulatorNotFocused_StaysEnabled()
109+
{
110+
var plugin = new InputSystemPlugin();
111+
plugin.OnCreate();
112+
// m_ConflictingDevicesDisabled defaults to false (Simulator not focused).
113+
114+
runtime.ReportNewInputDevice<Mouse>();
115+
InputSystem.Update();
116+
var mouse = Mouse.current;
117+
118+
Assert.That(mouse.enabled, Is.True);
119+
120+
plugin.OnDestroy();
121+
}
122+
123+
private static void SetPrivateField(object target, string name, object value)
124+
{
125+
var field = target.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
126+
Assert.NotNull(field, $"Field '{name}' not found on {target.GetType().Name}");
127+
field.SetValue(target, value);
128+
}
129+
67130
private TouchEvent CreateTouch(int touchId, Vector2 position, UnityEditor.DeviceSimulation.TouchPhase phase)
68131
{
69132
var touch = new TouchEvent();

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
3535
- Fixed `OnMouseUpAsButton` and `OnMouseUp` being dropped in Play mode when the Game view's focus changes between a press and its release on Unity 6000.5.0a8 and newer. The legacy `SendMouseEvents` pipeline is no longer driven from `InputUpdateType.Editor` updates, which read the editor state buffer (position (0,0), not pressed) and produced a spurious mouse release that cleared the press target.
3636
- Fixed Input Debugger window's incorrect name. It is now called 'Input Debugger' instead of 'Input Debug' [UUM-137124](https://jira.unity3d.com/browse/UUM-137124).
3737
- Fixed `PoseControl.isTracked` always returning false when read through non-optimized code paths (e.g. Input Debugger) due to `sizeInBits = 8` causing the value to be normalized as `1/255` instead of `1.0`.
38+
- Fixed the Device Simulator plugin keeping the real mouse and pen disabled while working in other Editor windows. Conflicting native `Mouse`/`Pen` devices are now only disabled while the Simulator window is focused and re-enabled as soon as focus moves elsewhere [UUM-145509](https://jira.unity3d.com/browse/UUM-145509).
3839

3940
### Changed
4041
- Action-level `IsPressed`, `WasPressedThisFrame`, and `WasReleasedThisFrame` for bindings to `Vector2Control` / `StickControl` no longer consult a per-control `pressPoint` on the vector (that field was removed). Use a `Press` interaction to set a custom threshold, or rely on `defaultButtonPressPoint`.

Packages/com.unity.inputsystem/InputSystem/Editor/DeviceSimulator/InputSystemPlugin.cs

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using UnityEditor;
56
using UnityEditor.DeviceSimulation;
7+
using UnityEditor.UIElements;
68
using UnityEngine.InputSystem.LowLevel;
9+
using UnityEngine.UIElements;
710

811
namespace UnityEngine.InputSystem.Editor
912
{
@@ -13,6 +16,9 @@ internal class InputSystemPlugin : DeviceSimulatorPlugin
1316

1417
private bool m_InputSystemEnabled;
1518
private bool m_Quitting;
19+
private bool m_ConflictingDevicesDisabled;
20+
private VisualElement m_RootElement;
21+
private EditorWindow m_LastFocusedWindow;
1622
private List<InputDevice> m_DisabledDevices;
1723

1824
public override string title => "Input System";
@@ -25,19 +31,16 @@ public override void OnCreate()
2531
// Monitor whether the editor is quitting to avoid risking unsafe EnableDevice while quitting
2632
UnityEditor.EditorApplication.quitting += OnQuitting;
2733

34+
// Poll the active window so conflicting devices are only disabled while the simulator is focused.
35+
UnityEditor.EditorApplication.update += OnUpdate;
36+
2837
m_DisabledDevices = new List<InputDevice>();
2938

3039
// deviceSimulator is never null when the plugin is instantiated by a simulator window, but it can be null during unit tests
3140
if (deviceSimulator != null)
3241
deviceSimulator.touchScreenInput += OnTouchEvent;
3342
InputSystem.onDeviceChange += OnDeviceChange;
3443

35-
// UGUI elements like a button don't get pressed when multiple pointers for example mouse and touchscreen are sending data at the same time
36-
foreach (var device in InputSystem.devices)
37-
{
38-
DisableConflictingDevice(device);
39-
}
40-
4144
SimulatorTouchscreen = InputSystem.AddDevice<Touchscreen>("Device Simulator Touchscreen");
4245
}
4346
}
@@ -56,6 +59,44 @@ internal void OnTouchEvent(TouchEvent touchEvent)
5659
});
5760
}
5861

62+
public override VisualElement OnCreateUI()
63+
{
64+
m_RootElement = new VisualElement();
65+
m_RootElement.Add(new HelpBox(
66+
L10n.Tr("Manages Input System devices while the Simulator is focused."),
67+
HelpBoxMessageType.Info));
68+
return m_RootElement;
69+
}
70+
71+
private void OnUpdate()
72+
{
73+
if (!EditorApplication.isPlaying)
74+
return;
75+
76+
var focusedWindow = EditorWindow.focusedWindow;
77+
if (focusedWindow == m_LastFocusedWindow)
78+
return;
79+
80+
m_LastFocusedWindow = focusedWindow;
81+
var simulatorFocused =
82+
m_RootElement != null
83+
&& focusedWindow != null
84+
&& focusedWindow.rootVisualElement.panel == m_RootElement.panel;
85+
86+
if (simulatorFocused && !m_ConflictingDevicesDisabled)
87+
{
88+
// UGUI elements like a button don't get pressed when multiple pointers for example mouse and touchscreen are sending data at the same time
89+
foreach (var device in InputSystem.devices)
90+
DisableConflictingDevice(device);
91+
m_ConflictingDevicesDisabled = true;
92+
}
93+
else if (!simulatorFocused && m_ConflictingDevicesDisabled)
94+
{
95+
ReenableConflictingDevices();
96+
m_ConflictingDevicesDisabled = false;
97+
}
98+
}
99+
59100
private void DisableConflictingDevice(InputDevice device)
60101
{
61102
if (device.native && (device is Mouse || device is Pen) && device.enabled)
@@ -65,8 +106,28 @@ private void DisableConflictingDevice(InputDevice device)
65106
}
66107
}
67108

109+
private void ReenableConflictingDevices()
110+
{
111+
foreach (var device in m_DisabledDevices)
112+
{
113+
// Note that m_Quitting is used here to mitigate the problem reported in issue tracker:
114+
// https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-10774.
115+
// Enabling a device will call into IOCTL of backend which may be destroyed prior
116+
// to this callback on Unity version. This is not a fix for the actual problem
117+
// of shutdown order but a package fix to mitigate this problem.
118+
// The core problem with the destruction order was still there in Unity 6.5.
119+
if (device.added && !m_Quitting)
120+
InputSystem.EnableDevice(device);
121+
}
122+
m_DisabledDevices.Clear();
123+
}
124+
68125
private void OnDeviceChange(InputDevice device, InputDeviceChange change)
69126
{
127+
// Only disable newly added/reconnected devices while the simulator is the active window.
128+
if (!m_ConflictingDevicesDisabled)
129+
return;
130+
70131
if (change == InputDeviceChange.Added || change == InputDeviceChange.Reconnected)
71132
DisableConflictingDevice(device);
72133
}
@@ -100,20 +161,13 @@ public override void OnDestroy()
100161
InputSystem.onDeviceChange -= OnDeviceChange;
101162

102163
UnityEditor.EditorApplication.quitting -= OnQuitting;
164+
UnityEditor.EditorApplication.update -= OnUpdate;
103165

104166
if (SimulatorTouchscreen != null)
105167
InputSystem.RemoveDevice(SimulatorTouchscreen);
106-
foreach (var device in m_DisabledDevices)
107-
{
108-
// Note that m_Quitting is used here to mitigate the problem reported in issue tracker:
109-
// https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-10774.
110-
// Enabling a device will call into IOCTL of backend which may be destroyed prior
111-
// to this callback on Unity version. This is not a fix for the actual problem
112-
// of shutdown order but a package fix to mitigate this problem.
113-
// The core problem with the destruction order was still there in Unity 6.5.
114-
if (device.added && !m_Quitting)
115-
InputSystem.EnableDevice(device);
116-
}
168+
169+
ReenableConflictingDevices();
170+
m_RootElement = null;
117171
}
118172
}
119173

0 commit comments

Comments
 (0)