Skip to content
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
af12382
mvp focus event
VeraMommersteeg Jan 28, 2026
00f9a33
Fixed tests to schedule input events. Fixed code to make device focus…
VeraMommersteeg Feb 9, 2026
431a40f
refactored onfocusevent and cleaned up
VeraMommersteeg Feb 10, 2026
07b6445
changed focus state to be flags, fixed tests, clean up
VeraMommersteeg Feb 18, 2026
4533fbf
cleaned up code, moved editor early out to later in update
VeraMommersteeg Feb 20, 2026
e1e5c26
if focus event, dont do anything with keyboard/mouse processing
VeraMommersteeg Feb 26, 2026
687cd5a
Changed tests, as the current architecture relies on focus changes pr…
VeraMommersteeg Mar 2, 2026
2adf1d3
fixed edge case for editor/game view swapping when reset and disable …
VeraMommersteeg Mar 2, 2026
21c13f2
fixed profiler marker scopes. fixed compilation for player
VeraMommersteeg Mar 3, 2026
84a848d
cleanup
VeraMommersteeg Mar 3, 2026
48b2e53
Merge branch 'develop' into input/add-focus-events-to-queue
VeraMommersteeg Mar 3, 2026
1f99472
disabling test for now, will make jira ticket
VeraMommersteeg Mar 4, 2026
c527198
formatting and cleanup/refactoring code
VeraMommersteeg Mar 4, 2026
e63d74d
fixed compilation and failing tests for 6.4 and below
VeraMommersteeg Mar 9, 2026
700c8dd
split legacy behaviour
VeraMommersteeg Mar 9, 2026
a97d931
fixed define
VeraMommersteeg Mar 9, 2026
45a64eb
add property to interface, re-enabled test
VeraMommersteeg Mar 10, 2026
df8c990
formatting
VeraMommersteeg Mar 10, 2026
5b92fe7
cleanup
VeraMommersteeg Mar 10, 2026
2aad9f2
Merge branch 'develop' into input/add-focus-events-to-queue
VeraMommersteeg Mar 10, 2026
eb257b0
fixed merge issue
VeraMommersteeg Mar 11, 2026
eeb7e1c
fixed failing tests, processed feedback
VeraMommersteeg Mar 12, 2026
8c159e8
fixed xmldoc issue
VeraMommersteeg Mar 12, 2026
2bdf412
fixed event discarding for forward peeking and instead soft reset the…
VeraMommersteeg Mar 12, 2026
4ab32cb
renamed function name
VeraMommersteeg Mar 12, 2026
e12c5ef
fixed xmldoc issue
VeraMommersteeg Mar 13, 2026
f388c1f
fixed issue where devices werent being disabled when the not run in b…
VeraMommersteeg Mar 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions Assets/Tests/InputSystem/CoreTests_Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,9 @@

using (var trace = new InputActionTrace(action))
{
runtime.PlayerFocusLost();
ScheduleFocusEvent(false);
InputSystem.Update(InputUpdateType.Dynamic);

Set(gamepad.leftTrigger, 0.123f, queueEventOnly: true);
InputSystem.Update(InputUpdateType.Editor);

Expand Down Expand Up @@ -661,13 +663,13 @@
// could just rely on order of event. Which means this test work for a fixed timestamp and it should
// changed accordingly.
currentTime += 1.0f;
runtime.PlayerFocusLost();
ScheduleFocusEvent(false);
currentTime += 1.0f;
// Queuing an event like it would be in the editor when the GameView is out of focus.
Set(mouse.position, new Vector2(0.234f, 0.345f) , queueEventOnly: true);
currentTime += 1.0f;
// Gaining focus like it would happen in the editor when the GameView regains focus.
runtime.PlayerFocusGained();
ScheduleFocusEvent(true);
currentTime += 1.0f;
// This emulates a device sync that happens when the player regains focus through an IOCTL command.
// That's why it also has it's time incremented.
Expand Down Expand Up @@ -720,14 +722,15 @@

trace.Clear();

runtime.PlayerFocusLost();
ScheduleFocusEvent(false);
InputSystem.Update(InputUpdateType.Dynamic);

Check warning on line 726 in Assets/Tests/InputSystem/CoreTests_Actions.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Assets/Tests/InputSystem/CoreTests_Actions.cs#L725-L726

Added lines #L725 - L726 were not covered by tests
currentTime = 10;

InputSystem.Update(InputUpdateType.Editor);

Assert.That(trace, Is.Empty);

runtime.PlayerFocusGained();
ScheduleFocusEvent(true);

Check warning on line 733 in Assets/Tests/InputSystem/CoreTests_Actions.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Assets/Tests/InputSystem/CoreTests_Actions.cs#L733

Added line #L733 was not covered by tests
InputSystem.Update(InputUpdateType.Dynamic);

actions = trace.ToArray();
Expand Down
44 changes: 29 additions & 15 deletions Assets/Tests/InputSystem/CoreTests_Devices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using UnityEngine.Scripting;
using UnityEngine.TestTools;
using UnityEngine.TestTools.Utils;
using UnityEngineInternal.Input;
using Gyroscope = UnityEngine.InputSystem.Gyroscope;
using UnityEngine.TestTools.Constraints;
using Is = NUnit.Framework.Is;
Expand Down Expand Up @@ -1522,8 +1523,9 @@
Assert.That(device, Is.Not.Null);

// Loose focus.
runtime.PlayerFocusLost();
InputSystem.Update();
ScheduleFocusEvent(false);
InputSystem.Update(InputUpdateType.Dynamic);
//InputSystem.Update();

// Disconnect.
var inputEvent = DeviceRemoveEvent.Create(deviceId, runtime.currentTime);
Expand All @@ -1534,8 +1536,9 @@
Assert.That(InputSystem.devices, Is.Empty);

// Regain focus.
runtime.PlayerFocusGained();
InputSystem.Update();
ScheduleFocusEvent(true);
InputSystem.Update(InputUpdateType.Dynamic);
//InputSystem.Update();

var newDeviceId = runtime.ReportNewInputDevice(deviceDesc);
InputSystem.Update();
Expand Down Expand Up @@ -4604,7 +4607,13 @@
InputSystem.onDeviceChange += DeviceChangeCallback;

var eventCount = 0;
InputSystem.onEvent += (eventPtr, _) => ++ eventCount;
InputSystem.onEvent += (eventPtr, _) =>
{
// Focus events will always be processed no matter the state
// Since the test relies on counting events based on state, dont count focus events
if (eventPtr.data->type != (FourCC)FocusConstants.kEventType)
++eventCount;
};

Assert.That(trackedDevice.enabled, Is.True);
Assert.That(mouse.enabled, Is.True);
Expand Down Expand Up @@ -4647,7 +4656,8 @@
}

// Lose focus.
runtime.PlayerFocusLost();
ScheduleFocusEvent(false);
InputSystem.Update(InputUpdateType.Dynamic);

Assert.That(sensor.enabled, Is.False);
Assert.That(disabledDevice.enabled, Is.False);
Expand Down Expand Up @@ -5068,7 +5078,8 @@
commands.Clear();

// Regain focus.
runtime.PlayerFocusGained();
ScheduleFocusEvent(true);
InputSystem.Update(InputUpdateType.Dynamic);

Assert.That(sensor.enabled, Is.False);
Assert.That(disabledDevice.enabled, Is.False);
Expand Down Expand Up @@ -5275,13 +5286,10 @@
"Sync Gamepad", "Sync Joystick",
"Sync TrackedDevice", "Sync TrackedDevice2",
"Sync Mouse", "Sync Mouse2", "Sync Mouse3",
"Sync Keyboard", "Reset Joystick"
}));
// Enabled devices that don't support syncs get reset.
Assert.That(changes, Is.EquivalentTo(new[]
{
"SoftReset Mouse1", "SoftReset Mouse3", "HardReset Joystick", "SoftReset TrackedDevice2"
"Sync Keyboard"
}));
// Enabled devices that don't support syncs dont get reset for Ignore Forcus as we do not want to cancel any actions.
Assert.That(changes, Is.Empty);
break;
}
}
Expand Down Expand Up @@ -5318,7 +5326,13 @@
Assert.That(performedCount, Is.EqualTo(1));

// Lose focus
runtime.PlayerFocusLost();
ScheduleFocusEvent(false);

Check warning on line 5329 in Assets/Tests/InputSystem/CoreTests_Devices.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Assets/Tests/InputSystem/CoreTests_Devices.cs#L5329

Added line #L5329 was not covered by tests
#if UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
// in the new system, we have to process the focus event to update the state of the devices.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Avoid terms like "new" and "old", instead be specific, e.g. "Since Unity 6000.5.a8, we have..." (or whatever is the appropriate version). "New" is relative to something, and it is not clear to what, so this won't age well I am afraid.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively just skip what is before the "comma" since the define should also states the change version.

// In the old system, this wouldn't work and would make the test fal
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of "In the old system" maybe just "Previously"?

InputSystem.Update();

Check warning on line 5333 in Assets/Tests/InputSystem/CoreTests_Devices.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Assets/Tests/InputSystem/CoreTests_Devices.cs#L5333

Added line #L5333 was not covered by tests
#endif

Assert.That(gamepad.enabled, Is.False);

// Queue an event while in the background. We don't want to see this event to be processed once focus
Expand All @@ -5329,7 +5343,7 @@
InputSystem.Update();

// Gain focus
runtime.PlayerFocusGained();
ScheduleFocusEvent(true);

Check warning on line 5346 in Assets/Tests/InputSystem/CoreTests_Devices.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Assets/Tests/InputSystem/CoreTests_Devices.cs#L5346

Added line #L5346 was not covered by tests

// Run update to try process events accordingly once focus is gained
InputSystem.Update();
Expand Down
6 changes: 4 additions & 2 deletions Assets/Tests/InputSystem/CoreTests_Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2717,7 +2717,8 @@ public void Editor_CanForceKeyboardAndMouseInputToGameViewWithoutFocus()
var keyboard = InputSystem.AddDevice<Keyboard>();
var mouse = InputSystem.AddDevice<Mouse>();

runtime.PlayerFocusLost();
ScheduleFocusEvent(false);
InputSystem.Update(InputUpdateType.Dynamic);

Assert.That(keyboard.enabled, Is.True);
Assert.That(mouse.enabled, Is.True);
Expand Down Expand Up @@ -3013,7 +3014,8 @@ public void Editor_LeavingPlayMode_ReenablesAllDevicesTemporarilyDisabledDueToFo
Set(mouse.position, new Vector2(123, 234));
Press(gamepad.buttonSouth);

runtime.PlayerFocusLost();
ScheduleFocusEvent(false);
InputSystem.Update(InputUpdateType.Dynamic);

Assert.That(gamepad.enabled, Is.False);

Expand Down
12 changes: 9 additions & 3 deletions Assets/Tests/InputSystem/CoreTests_State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,9 @@ public void State_CanSetUpMonitorsForStateChanges_InEditor()
InputState.AddChangeMonitor(gamepad.leftStick,
(control, time, eventPtr, monitorIndex) => monitorFired = true);

runtime.PlayerFocusLost();
ScheduleFocusEvent(false);
InputSystem.Update(InputUpdateType.Dynamic);

Set(gamepad.leftStick, new Vector2(0.123f, 0.234f), queueEventOnly: true);
InputSystem.Update(InputUpdateType.Editor);

Expand Down Expand Up @@ -1676,7 +1678,9 @@ public void State_RecordingHistory_ExcludesEditorInputByDefault()
{
history.StartRecording();

runtime.PlayerFocusLost();
ScheduleFocusEvent(false);
InputSystem.Update(InputUpdateType.Dynamic);

Set(gamepad.leftTrigger, 0.123f, queueEventOnly: true);
InputSystem.Update(InputUpdateType.Editor);

Expand All @@ -1696,7 +1700,9 @@ public void State_RecordingHistory_CanCaptureEditorInput()
history.updateMask = InputUpdateType.Editor;
history.StartRecording();

runtime.PlayerFocusLost();
ScheduleFocusEvent(false);
InputSystem.Update(InputUpdateType.Dynamic);

Set(gamepad.leftTrigger, 0.123f, queueEventOnly: true);
InputSystem.Update(InputUpdateType.Editor);

Expand Down
29 changes: 23 additions & 6 deletions Assets/Tests/InputSystem/Plugins/EnhancedTouchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,10 @@
Assert.That(Touch.activeTouches, Has.Count.EqualTo(1));

// And make sure we're not seeing the data in the editor.
runtime.PlayerFocusLost();
ScheduleFocusEvent(false);
InputSystem.Update(InputUpdateType.Editor);

Assert.That(Touch.activeTouches, Is.Empty);

// Feed some data into editor state.
BeginTouch(2, new Vector2(0.234f, 0.345f), queueEventOnly: true);
InputSystem.Update(InputUpdateType.Editor);
Expand All @@ -171,8 +170,25 @@
Assert.That(Touch.activeTouches[0].touchId, Is.EqualTo(2));

// Switch back to player.
runtime.PlayerFocusGained();
InputSystem.Update();
ScheduleFocusEvent(true);

Check warning on line 173 in Assets/Tests/InputSystem/Plugins/EnhancedTouchTests.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Assets/Tests/InputSystem/Plugins/EnhancedTouchTests.cs#L173

Added line #L173 was not covered by tests

// We have to schedule the specificic update type that the player is configured to process events in,
// otherwise we will end up using defaultUpdateType, which due to the fact we do not have focus in pre-update yet,
// will be Editor, which means that we will end up swapping buffers to the editor buffer, and retreiving the wrong active touch
// The only way to properly fix this, is to remove defaultUpdateType, and split the player/editor update loops into separate methods, which would be a breaking change.
// Until then, we have to make sure to schedule the correct update type here.
switch (updateMode)

Check warning on line 180 in Assets/Tests/InputSystem/Plugins/EnhancedTouchTests.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Assets/Tests/InputSystem/Plugins/EnhancedTouchTests.cs#L180

Added line #L180 was not covered by tests
{
case InputSettings.UpdateMode.ProcessEventsInDynamicUpdate:
InputSystem.Update(InputUpdateType.Dynamic);
break;

Check warning on line 184 in Assets/Tests/InputSystem/Plugins/EnhancedTouchTests.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Assets/Tests/InputSystem/Plugins/EnhancedTouchTests.cs#L183-L184

Added lines #L183 - L184 were not covered by tests
case InputSettings.UpdateMode.ProcessEventsInFixedUpdate:
InputSystem.Update(InputUpdateType.Fixed);
break;

Check warning on line 187 in Assets/Tests/InputSystem/Plugins/EnhancedTouchTests.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Assets/Tests/InputSystem/Plugins/EnhancedTouchTests.cs#L186-L187

Added lines #L186 - L187 were not covered by tests
case InputSettings.UpdateMode.ProcessEventsManually:
InputSystem.Update(InputUpdateType.Manual);
break;

Check warning on line 190 in Assets/Tests/InputSystem/Plugins/EnhancedTouchTests.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Assets/Tests/InputSystem/Plugins/EnhancedTouchTests.cs#L189-L190

Added lines #L189 - L190 were not covered by tests
}

Assert.That(Touch.activeTouches, Has.Count.EqualTo(1));
Assert.That(Touch.activeTouches[0].touchId, Is.EqualTo(1));
Expand Down Expand Up @@ -1160,7 +1176,7 @@
Assert.That(Touch.activeTouches, Has.Count.EqualTo(1));
Assert.That(Touch.activeTouches[0].phase, Is.EqualTo(TouchPhase.Began));

runtime.PlayerFocusLost();
ScheduleFocusEvent(false);

if (runInBackground)
{
Expand All @@ -1171,7 +1187,8 @@
else
{
// When not running in the background, the same thing happens but only on focus gain.
runtime.PlayerFocusGained();
//runtime.PlayerFocusGained();
ScheduleFocusEvent(true);

Check warning on line 1191 in Assets/Tests/InputSystem/Plugins/EnhancedTouchTests.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Assets/Tests/InputSystem/Plugins/EnhancedTouchTests.cs#L1191

Added line #L1191 was not covered by tests
InputSystem.Update();
}

Expand Down
6 changes: 3 additions & 3 deletions Assets/Tests/InputSystem/Plugins/InputForUITests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -715,19 +715,19 @@ public void UIActions_DoNotGetTriggeredByOutOfFocusEventInEditor(InputSettings.B
currentTime += 1.0f;
Update();
currentTime += 1.0f;
runtime.PlayerFocusLost();
ScheduleFocusEvent(false);
currentTime += 1.0f;
Set(mouse.position, outOfFocusPosition , queueEventOnly: true);
currentTime += 1.0f;
runtime.PlayerFocusGained();
ScheduleFocusEvent(true);
currentTime += 1.0f;
Set(mouse.position, focusPosition, queueEventOnly: true);
currentTime += 1.0f;

// We call specific updates to simulate editor behavior when regaining focus.
InputSystem.Update(InputUpdateType.Editor);
Assert.AreEqual(0, m_InputForUIEvents.Count);
InputSystem.Update();
InputSystem.Update(InputUpdateType.Dynamic);
// Calling the event provider update after we call InputSystem updates so that we trigger InputForUI events
EventProvider.NotifyUpdate();

Expand Down
14 changes: 10 additions & 4 deletions Assets/Tests/InputSystem/Plugins/UITests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4128,7 +4128,9 @@

scene.leftChildReceiver.events.Clear();

runtime.PlayerFocusLost();
ScheduleFocusEvent(false);
InputSystem.Update(InputUpdateType.Dynamic);

Check warning on line 4132 in Assets/Tests/InputSystem/Plugins/UITests.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Assets/Tests/InputSystem/Plugins/UITests.cs#L4131-L4132

Added lines #L4131 - L4132 were not covered by tests

if (canRunInBackground)
Assert.That(clickCanceled, Is.EqualTo(0));
else
Expand All @@ -4139,7 +4141,9 @@
Assert.That(scene.eventSystem.hasFocus, Is.False);
Assert.That(clicked, Is.False);

runtime.PlayerFocusGained();
ScheduleFocusEvent(true);
InputSystem.Update(InputUpdateType.Dynamic);

Check warning on line 4145 in Assets/Tests/InputSystem/Plugins/UITests.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Assets/Tests/InputSystem/Plugins/UITests.cs#L4144-L4145

Added lines #L4144 - L4145 were not covered by tests

scene.eventSystem.SendMessage("OnApplicationFocus", true);

yield return null;
Expand Down Expand Up @@ -4168,11 +4172,13 @@

// Ensure that losing and regaining focus doesn't cause the next click to be ignored
clicked = false;
runtime.PlayerFocusLost();
ScheduleFocusEvent(false);
InputSystem.Update(InputUpdateType.Dynamic);

Check warning on line 4176 in Assets/Tests/InputSystem/Plugins/UITests.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Assets/Tests/InputSystem/Plugins/UITests.cs#L4175-L4176

Added lines #L4175 - L4176 were not covered by tests
scene.eventSystem.SendMessage("OnApplicationFocus", false);
yield return null;

runtime.PlayerFocusGained();
ScheduleFocusEvent(true);
InputSystem.Update(InputUpdateType.Dynamic);

Check warning on line 4181 in Assets/Tests/InputSystem/Plugins/UITests.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Assets/Tests/InputSystem/Plugins/UITests.cs#L4180-L4181

Added lines #L4180 - L4181 were not covered by tests
scene.eventSystem.SendMessage("OnApplicationFocus", true);
yield return null;

Expand Down
12 changes: 10 additions & 2 deletions Assets/Tests/InputSystem/Plugins/UserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,8 +1268,16 @@ public void Users_DoNotReactToEditorInput()
++InputUser.listenForUnpairedDeviceActivity;
InputUser.onUnpairedDeviceUsed += (control, eventPtr) => Assert.Fail("Should not react!");

runtime.PlayerFocusLost();

// We now have to run a dynamic update before the press, to make sure the focus state is up to date.
// This is due to scheduled focus events are not being processed in pre-update, and not running an update before the press would result in the incorrect state.
// When calling an empty Update(), it will use the default defaultUpdateType to determine which update type to use.
// However in pre-update the focus will not have switched to false yet if not running a dynamic update before, so the focus check in defaultUpdateType is no longer correct.
// which would cause it to schedule a dynamic update instead. We solve this by first processing the focus event before pressing the button,
// which will then make it correctly swap to an editor update type when doing the button press.
// Another way to make this test pass, is to queue the button press and then explicitly call an editor update and process both events
// The way to solve this is to remove defaultUpdateType and split the editor/player loops or to make sure we do not call any Update() without update type, so we do not use defaultUpdateType.
ScheduleFocusEvent(false);
InputSystem.Update(InputUpdateType.Dynamic);
Press(gamepad.buttonSouth);

Assert.That(gamepad.buttonSouth.isPressed, Is.True);
Expand Down
5 changes: 5 additions & 0 deletions Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
"name": "Unity",
"expression": "6000.3.0a6",
"define": "UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY"
},
{
"name": "Unity",
"expression": "6000.5.0a8",
"define": "UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS"
}
],
"noEngineReferences": false
Expand Down
Loading