Skip to content

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

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

FIX: ISXB-1319 Input recorder game view focus#2407
MorganHoarau wants to merge 9 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

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).

Please also note that the fix essentially add a new tightly coupled flag for a feature from a sample. It's not the best, but is a small patch to get this feature working.

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.

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
if (!m_ReplayBypassActive)
{
m_ReplayBypassActive = true;
++InputSystem.s_Manager.m_ActiveReplayCount;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

low
Directly modifying InputSystem.s_Manager.m_ActiveReplayCount from ReplayController creates tight coupling. Have you considered adding internal methods to InputManager to encapsulate the increment/decrement of this counter?

🤖 Helpful? 👍/👎


if (m_ClearReplayBypassCallback == null)
m_ClearReplayBypassCallback = EndReplayBypass;
InputSystem.onAfterUpdate += m_ClearReplayBypassCallback;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high
This logic has two issues that can lead to memory leaks and incorrect behavior:

  1. Subscription Leak: ScheduleEndReplayBypass does not check if a callback is already scheduled before calling +=. If Finished() is called multiple times (e.g., if a user triggers PlayAllEvents repeatedly in the same frame), multiple subscriptions are added to InputSystem.onAfterUpdate. Since EndReplayBypass (line 1130) nullifies m_ClearReplayBypassCallback after removing only one subscription, any additional subscriptions will remain in the global onAfterUpdate event forever. This will also keep the ReplayController instance alive in memory indefinitely.

  2. Premature Termination: If a playback is restarted (e.g., calling PlayAllFramesOneByOne) on the same controller after it has finished but before the onAfterUpdate cleanup has fired, BeginReplayBypass will see m_ReplayBypassActive as true and skip the increment. However, the pending EndReplayBypass will then fire, setting m_ReplayBypassActive to false and decrementing the count, leaving the newly started replay without its bypass.

Suggested Fix:

  • In ScheduleEndReplayBypass, return early if m_ClearReplayBypassCallback != null.
  • In BeginReplayBypass, if m_ClearReplayBypassCallback is not null, unsubscribe it from onAfterUpdate and nullify it to cancel the pending cleanup before checking m_ReplayBypassActive.

🤖 Helpful? 👍/👎

Dispose event traces after use, so that they do not leak memory on the unmanaged (C++) memory heap.

> [!NOTE]
> **Keyboard text input is not replayed to UI text fields.** Keyboard state (key presses) is captured and replayed correctly and remains accessible via `Keyboard.current`. However there is a known limitation with character delivery to UI Framework components (uGUI `InputField` or UIToolkit `TextField`). These components receive text through a separate native pipeline that is not fed by event replay. As a result, text typed into UI text fields during recording will not appear during playback.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

low
Minor documentation clarity: 'However there is a known limitation' would be improved with a comma after 'However', and 'UIToolkit' is generally written as 'UI Toolkit' in Unity documentation.

🤖 Helpful? 👍/👎

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.00000% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
....inputsystem/InputSystem/Events/InputEventTrace.cs 83.78% 6 Missing ⚠️
@@           Coverage Diff            @@
##           develop    #2407   +/-   ##
========================================
  Coverage    78.13%   78.13%           
========================================
  Files          483      483           
  Lines        98770    98805   +35     
========================================
+ Hits         77169    77201   +32     
- Misses       21601    21604    +3     
Flag Coverage Δ
inputsystem_MacOS_2022.3 5.33% <5.12%> (-0.01%) ⬇️
inputsystem_MacOS_2022.3_project 75.38% <85.00%> (-0.03%) ⬇️
inputsystem_MacOS_6000.0 5.31% <5.12%> (-0.01%) ⬇️
inputsystem_MacOS_6000.0_project 77.28% <85.00%> (-0.03%) ⬇️
inputsystem_MacOS_6000.3 5.31% <5.12%> (-0.01%) ⬇️
inputsystem_MacOS_6000.3_project 77.27% <85.00%> (+<0.01%) ⬆️
inputsystem_MacOS_6000.4 5.31% <5.12%> (-0.01%) ⬇️
inputsystem_MacOS_6000.4_project 77.28% <85.00%> (+<0.01%) ⬆️
inputsystem_MacOS_6000.5 5.30% <5.12%> (-0.01%) ⬇️
inputsystem_MacOS_6000.5_project 77.32% <85.00%> (+<0.01%) ⬆️
inputsystem_MacOS_6000.6 5.30% <5.12%> (-0.01%) ⬇️
inputsystem_MacOS_6000.6_project 77.32% <85.00%> (+<0.01%) ⬆️
inputsystem_Ubuntu_2022.3_project 75.28% <85.00%> (-0.03%) ⬇️
inputsystem_Ubuntu_6000.0 5.31% <5.12%> (-0.01%) ⬇️
inputsystem_Ubuntu_6000.0_project 77.18% <85.00%> (-0.03%) ⬇️
inputsystem_Ubuntu_6000.3 5.31% <5.12%> (-0.01%) ⬇️
inputsystem_Ubuntu_6000.3_project 77.18% <85.00%> (+<0.01%) ⬆️
inputsystem_Ubuntu_6000.4 5.32% <5.12%> (-0.01%) ⬇️
inputsystem_Ubuntu_6000.4_project 77.19% <85.00%> (+<0.01%) ⬆️
inputsystem_Ubuntu_6000.5 5.30% <5.12%> (-0.01%) ⬇️
inputsystem_Ubuntu_6000.5_project 77.22% <85.00%> (+<0.01%) ⬆️
inputsystem_Ubuntu_6000.6 5.30% <5.12%> (-0.01%) ⬇️
inputsystem_Ubuntu_6000.6_project 77.22% <85.00%> (+<0.01%) ⬆️
inputsystem_Windows_2022.3 5.33% <5.12%> (-0.01%) ⬇️
inputsystem_Windows_2022.3_project 75.51% <85.00%> (-0.03%) ⬇️
inputsystem_Windows_6000.0 5.31% <5.12%> (-0.01%) ⬇️
inputsystem_Windows_6000.0_project 77.41% <85.00%> (-0.02%) ⬇️
inputsystem_Windows_6000.3 5.31% <5.12%> (-0.01%) ⬇️
inputsystem_Windows_6000.3_project 77.40% <85.00%> (+<0.01%) ⬆️
inputsystem_Windows_6000.4 5.31% <5.12%> (-0.01%) ⬇️
inputsystem_Windows_6000.4_project 77.40% <85.00%> (+<0.01%) ⬆️
inputsystem_Windows_6000.5 5.30% <5.12%> (-0.01%) ⬇️
inputsystem_Windows_6000.5_project 77.45% <85.00%> (+<0.01%) ⬆️
inputsystem_Windows_6000.6 5.30% <5.12%> (-0.01%) ⬇️
inputsystem_Windows_6000.6_project 77.45% <85.00%> (+<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.57% <83.78%> (-0.05%) ⬇️

... and 1 file with indirect coverage changes

ℹ️ Need help interpreting these results?

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.

1 participant