Skip to content

FIX: ISXB-1319 Input recorder game view focus#2407

Open
MorganHoarau wants to merge 11 commits intodevelopfrom
fix/ISXB-1319/input-recorder-game-view-focus
Open

FIX: ISXB-1319 Input recorder game view focus#2407
MorganHoarau wants to merge 11 commits intodevelopfrom
fix/ISXB-1319/input-recorder-game-view-focus

Conversation

@MorganHoarau
Copy link
Copy Markdown
Collaborator

@MorganHoarau MorganHoarau commented Apr 14, 2026

Description

PR addresses https://jira.unity3d.com/browse/ISXB-1319

  • The first issue is fixed with a deemed acceptable trade-off.
  • The second issue is documented as known limitation in Events.md

Tech Roundtable Slide-deck: https://docs.google.com/presentation/d/18zL10TRcnVfBiSSBe_YT5EYVkJLyt7gFvD3s7EL0JKY/edit?usp=drive_link

Note: the fix adds a tight coupling between InputManager and InputEventTrace.ReplayController.

Issue 1: replay requires Game View focus

Root cause: three independent focus-gating layers that block replayed events when Game View is unfocused (event deferral, device-disabled discard, UI module skip).
The fix implemented uses an InputManager-level isReplayActive flag that bypasses all three (Editor-only).

Issues 1 Details

When Game View loses focus -> applicationHasFocus=false -> gameHasFocus=false. Replay events are blocked at three independent layers:

sequenceDiagram
    participant IM as InputManager
    participant RC as ReplayController
    participant Dev as Mouse/Keyboard
    participant UIIM as InputSystemUIInputModule
    participant ES as EventSystem

    Note over IM,ES: Game View loses focus
    IM->>IM: gameHasFocus = false
    IM->>Dev: DisableDevice(DisabledWhileInBackground)
    ES->>ES: OnApplicationFocus(false) → m_HasFocus = false

    Note over IM,ES: Replay starts
    RC->>IM: QueueEvent(mouseStateEvent)
    IM->>IM: ❌ A — event deferred (ShouldDefer)
    IM->>IM: ❌ B — event discarded (device disabled)
    ES->>UIIM: Process()
    UIIM->>UIIM: ❌ C — !isFocused → skip ProcessPointer/ProcessNavigation
Loading

Known trade-off: The bypass doesn't distinguish replayed events from real hardware input, so real keyboard/mouse input also reaches the game during replay even with Game View unfocused. This is acceptable for typical usage (replay in isolation in Editor).

Issue 2: TextField text not replayed

Architectural limitation. UIToolkit TextField receives text through the native IMGUI event queue (Event.PopEvent), which is a separate pipeline from InputSystem. Replayed TextEvents fire correctly through Keyboard.OnTextInput but have zero subscribers. The IMGUI queue is only populated by native OS input. Keyboard state (key presses) does replay correctly; only character delivery to TextFields is broken. Fixing this would require engine-side changes (InputForUI / IMGUI bridge), outside scope of this ticket.

This is a consistent pattern across Unity's UI framework: IMGUI, uGUI, and UIToolkit all use Event.PopEvent() for text processing.

JIRA user story: https://jira.unity3d.com/browse/ISX-2562

Testing status & QA

Please describe the testing already done by you and what testing you request/recommend QA to execute. If you used or created any testing project please link them here too for QA.

  • Tested on 6.0 using InputRecorderSample.unity scene in WinEditor.
  • Need to test on 6.5 without the changes to see if latest changes from Vera affect the behavior.
input-recorder-half-fix.mp4

Overall Product Risks

Please rate the potential complexity and halo effect from low to high for the reviewers. Note down potential risks to specific Editor branches if any.

  • Complexity: Low
  • Halo Effect: Low

Comments to reviewers

Please describe any additional information such as what to focus on, or historical info for the reviewers.

  • Paulius: Could you please evaluate the known trade-off for Issue 1?
    • To Test: Record a long enough input sequence, then during playback, type on the keyboard or move the mouse while the Game View is unfocused and observe that the real inputs reaches the game.
    • Context: The fix works by bypassing the focus gate for the duration of replay — a side effect is that real hardware input (keyboard, mouse) can also pass through during that window, even though the Game View is not focused. This is the trade-off. Rita would like your assessment of how impactful this is in practice, and whether it is acceptable for shipping Issue 1's fix as-is.

Checklist

Before review:

  • Changelog entry added.
    • Explains the change in Changed, Fixed, Added sections.
    • For API change contains an example snippet and/or migration example.
    • JIRA ticket linked, example (case %%). If it is a private issue, just add the case ID without a link.
    • Jira port for the next release set as "Resolved".
  • Tests added/changed, if applicable.
    • Functional tests Area_CanDoX, Area_CanDoX_EvenIfYIsTheCase, Area_WhenIDoX_AndYHappens_ThisIsTheResult.
    • Performance tests.
    • Integration tests.
  • Docs for new/changed API's.
    • Xmldoc cross references are set correctly.
    • Added explanation how the API works.
    • Usage code examples added.
    • The manual is updated, if needed.

During merge:

  • Commit message for squash-merge is prefixed with one of the list:
    • NEW: ___.
    • FIX: ___.
    • DOCS: ___.
    • CHANGE: ___.
    • RELEASE: 1.1.0-preview.3.

Bypasses several game focused related logic gates to let replay input event reach playmode session.
Bit of logics to track replay controllers and bridge with InputManager.
Uses InputManager.isReplayActive flag
@MorganHoarau MorganHoarau changed the title Fix/isxb 1319/input recorder game view focus FIX: ISXB-1319 Input recorder game view focus Apr 15, 2026
@MorganHoarau MorganHoarau marked this pull request as ready for review April 15, 2026 14:07
Copy link
Copy Markdown
Contributor

@u-pr u-pr Bot left a comment

Choose a reason for hiding this comment

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

May require changes
The review identified several critical issues, including potential compilation failures in non-Editor builds and logic bugs related to event subscription leaks. Additionally, there are minor suggestions for documentation and architectural improvements.

🤖 Helpful? 👍/👎

Comment thread Packages/com.unity.inputsystem/InputSystem/InputManager.cs Outdated
Comment thread Packages/com.unity.inputsystem/Documentation~/Events.md Outdated
WinPlayer build made to ensure compilation
@codecov-github-com
Copy link
Copy Markdown

codecov-github-com Bot commented Apr 15, 2026

Codecov Report

Attention: Patch coverage is 85.36585% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
....inputsystem/InputSystem/Events/InputEventTrace.cs 84.21% 6 Missing ⚠️
@@           Coverage Diff            @@
##           develop    #2407   +/-   ##
========================================
  Coverage    78.13%   78.13%           
========================================
  Files          483      483           
  Lines        98770    98806   +36     
========================================
+ Hits         77169    77202   +33     
- Misses       21601    21604    +3     
Flag Coverage Δ
inputsystem_MacOS_2022.3 5.33% <5.00%> (-0.01%) ⬇️
inputsystem_MacOS_2022.3_project 75.38% <85.36%> (-0.03%) ⬇️
inputsystem_MacOS_6000.0 5.31% <5.00%> (-0.01%) ⬇️
inputsystem_MacOS_6000.0_project 77.28% <85.36%> (-0.03%) ⬇️
inputsystem_MacOS_6000.3 5.31% <5.00%> (-0.01%) ⬇️
inputsystem_MacOS_6000.3_project 77.27% <85.36%> (+<0.01%) ⬆️
inputsystem_MacOS_6000.4 5.31% <5.00%> (-0.01%) ⬇️
inputsystem_MacOS_6000.4_project 77.28% <85.36%> (+<0.01%) ⬆️
inputsystem_MacOS_6000.5 5.30% <5.00%> (-0.01%) ⬇️
inputsystem_MacOS_6000.5_project 77.32% <85.36%> (+<0.01%) ⬆️
inputsystem_MacOS_6000.6 5.30% <5.00%> (-0.01%) ⬇️
inputsystem_MacOS_6000.6_project 77.32% <85.36%> (+<0.01%) ⬆️
inputsystem_Ubuntu_2022.3_project 75.28% <85.36%> (-0.03%) ⬇️
inputsystem_Ubuntu_6000.0 5.31% <5.00%> (-0.01%) ⬇️
inputsystem_Ubuntu_6000.0_project 77.18% <85.36%> (-0.03%) ⬇️
inputsystem_Ubuntu_6000.3 5.31% <5.00%> (-0.01%) ⬇️
inputsystem_Ubuntu_6000.3_project 77.18% <85.36%> (+<0.01%) ⬆️
inputsystem_Ubuntu_6000.4 5.32% <5.00%> (-0.01%) ⬇️
inputsystem_Ubuntu_6000.4_project 77.19% <85.36%> (+<0.01%) ⬆️
inputsystem_Ubuntu_6000.5 5.30% <5.00%> (-0.01%) ⬇️
inputsystem_Ubuntu_6000.5_project 77.22% <85.36%> (+<0.01%) ⬆️
inputsystem_Ubuntu_6000.6 5.30% <5.00%> (-0.01%) ⬇️
inputsystem_Ubuntu_6000.6_project 77.22% <85.36%> (+<0.01%) ⬆️
inputsystem_Windows_2022.3 5.33% <5.00%> (-0.01%) ⬇️
inputsystem_Windows_2022.3_project 75.51% <85.36%> (-0.03%) ⬇️
inputsystem_Windows_6000.0 5.31% <5.00%> (-0.01%) ⬇️
inputsystem_Windows_6000.0_project 77.41% <85.36%> (-0.02%) ⬇️
inputsystem_Windows_6000.3 5.31% <5.00%> (-0.01%) ⬇️
inputsystem_Windows_6000.3_project 77.40% <85.36%> (+<0.01%) ⬆️
inputsystem_Windows_6000.4 5.31% <5.00%> (-0.01%) ⬇️
inputsystem_Windows_6000.4_project 77.40% <85.36%> (+<0.01%) ⬆️
inputsystem_Windows_6000.5 5.30% <5.00%> (-0.01%) ⬇️
inputsystem_Windows_6000.5_project 77.45% <85.36%> (+<0.01%) ⬆️
inputsystem_Windows_6000.6 5.30% <5.00%> (-0.01%) ⬇️
inputsystem_Windows_6000.6_project 77.45% <85.36%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
.../com.unity.inputsystem/InputSystem/InputManager.cs 94.15% <100.00%> (-0.09%) ⬇️
...InputSystem/Plugins/UI/InputSystemUIInputModule.cs 93.65% <100.00%> (ø)
....inputsystem/InputSystem/Events/InputEventTrace.cs 84.59% <84.21%> (-0.03%) ⬇️

... and 1 file with indirect coverage changes

ℹ️ Need help interpreting these results?

@MorganHoarau
Copy link
Copy Markdown
Collaborator Author

/crc

Copy link
Copy Markdown
Contributor

@u-pr u-pr Bot left a comment

Choose a reason for hiding this comment

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

Great
The Pull Request quality is high, but there are a few remaining logic and safety issues to address. Specifically, the replay bypass in InputManager is currently too broad and may affect unintended devices, and a potential null reference should be guarded against in InputEventTrace.

🤖 Helpful? 👍/👎

Comment thread Packages/com.unity.inputsystem/InputSystem/InputManager.cs
Comment thread Packages/com.unity.inputsystem/InputSystem/Events/InputEventTrace.cs Outdated
Co-authored-by: u-pr[bot] <205906871+u-pr[bot]@users.noreply.github.com>
@MorganHoarau
Copy link
Copy Markdown
Collaborator Author

waiting for CI to be green before adding final reviewers

@ekcoh
Copy link
Copy Markdown
Collaborator

ekcoh commented Apr 28, 2026

Thanks for a very good and descriptive PR description. For issue #2 I would recommend filing a backlog ticket referencing the findings from this investigation and possibly this PR as well so this can be looked into in the future.

Copy link
Copy Markdown
Collaborator

@ekcoh ekcoh left a comment

Choose a reason for hiding this comment

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

Generally very nice and clean PR with a great description. I have no reason to not approve but I would urge considering the questions and design aspects raised in comments since I believe this can be further improved by handling the ignoreFocusFiltering as a clear internal feature (that might become relevant for more things) and consider if editor/source splitting of the replay logic is potentially possible to make cleaner by adding (or relying on existing) events tied to the replay mechanism to avoid as much #if UNITY_EDITOR checks. The only thing I am missing here is a test case since this adds a "new internal feature" which doesn't seem to be covered for regressions which indicates it needs to be rested manually which is costly in the long run.

public ReplayController PlayAllFramesOneByOne()
{
finished = false;
#if UNITY_EDITOR
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Generally, when this kind of behaviour is needed (with these editor checks all over the place), I wonder if we shouldn't add a proper optional callback for these replay events, e.g. ReplayBegun, there already seems to be an m_OnFinished that could be used to handle the call to editor code ScheduleEndReplayBypass()?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

e.g. this could be e.g. (not sure about naming - conceptually) m_OnReplayStart?();

#if UNITY_EDITOR
// Schedule bypass removal for after the next OnUpdate, so any events already
// queued into the native buffer this frame are still processed with the bypass active.
ScheduleEndReplayBypass();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Couldn't editor code hook to m_OnFinished in this case handle it that way?

// focus loss — the replay intentionally re-injects events for those devices.
if (device != null && !device.enabled &&
#if UNITY_EDITOR
!isReplayActive &&
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Minor, but I don't specifically see why this is "tightly replay", it looks like an internal feature "ignoreGameViewFocus". That might age/scale better and better indicates what this is all about. It's a specific mode where game-view focus is ignored in event filtering, or what do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants