Skip to content

Conversation

@jfreire-unity
Copy link
Collaborator

@jfreire-unity jfreire-unity commented Sep 11, 2025

Description

Proposed solution to fix ISXB-1671.

The bug reports a UITK event that shouldn't be triggered. This ocurrs due to a InputSystemProvider UI Click action being performed once the Editor is in play-mode and we select Game view tab when previously in Scene View.

The action that is triggered provides the wrong mouse position. Essentially, an action is being performed for the mouse click/position that happened while out of focus in the Editor and triggered the focus regain. However, when InputManager.OnUpdate() is called, it assumes the game already is in focus, so there was no way to know if an event was triggered out of focus or not.

The proposed solution is to filter and discard the events that happened immediately before focus was regained, by checking if they were received before the focus regain. In the diagram below, essentially we are discarding the "ofending" event - in red - that triggered an Input Action, which would then dispatch a UITK event through InputSystemProvider.

image

Unrelated changes to bug-fix

I also did a small refactoring to put some conditions in their own functions so that can be easier to read through InputManager.OnUpdate. This is just a personal preference as it has been historically complicated to reason about all the conditions we have. This is still not perfect, but I hope it's a small improvement for others as well.

If you want, you can have a look of the commit which contains the fix changes, 6157498. This doesn't have any refactoring yet and is easier to reason about.

PR template update

Removes a section that we no longer need to take into account.

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.

Overall Product Risks

  • Complexity: 1
  • Halo Effect: 1

Comments to reviewers

Verify that the original bug doesn't happen anymore, both on Win and macOS.

Do some other tests with changing Background settings, to make sure nothing has regressed. We can likely use on of the Mouse Visualisers for it.

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.

During merge:

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

@codecov-github-com
Copy link

codecov-github-com bot commented Sep 11, 2025

Codecov Report

Attention: Patch coverage is 97.34513% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
.../com.unity.inputsystem/InputSystem/InputManager.cs 94.33% 3 Missing ⚠️
@@             Coverage Diff             @@
##           develop    #2234      +/-   ##
===========================================
+ Coverage    76.70%   76.83%   +0.12%     
===========================================
  Files          465      476      +11     
  Lines        87919    88828     +909     
===========================================
+ Hits         67442    68247     +805     
- Misses       20477    20581     +104     
Flag Coverage Δ
inputsystem_MacOS_2022.3 5.44% <69.81%> (+0.07%) ⬆️
inputsystem_MacOS_2022.3_project 74.71% <96.29%> (+0.12%) ⬆️
inputsystem_MacOS_6000.0 5.23% <69.81%> (+0.04%) ⬆️
inputsystem_MacOS_6000.0_project 76.63% <97.34%> (+0.12%) ⬆️
inputsystem_MacOS_6000.2 5.23% <69.81%> (+0.04%) ⬆️
inputsystem_MacOS_6000.2_project 76.62% <97.34%> (+0.12%) ⬆️
inputsystem_MacOS_6000.3 5.23% <69.81%> (+0.04%) ⬆️
inputsystem_MacOS_6000.3_project 76.62% <97.34%> (+0.12%) ⬆️
inputsystem_MacOS_6000.4 5.23% <69.81%> (+0.04%) ⬆️
inputsystem_MacOS_6000.4_project 76.63% <97.34%> (+0.13%) ⬆️
inputsystem_MacOS_6000.5 5.23% <69.81%> (?)
inputsystem_MacOS_6000.5_project 76.64% <97.34%> (?)
inputsystem_Ubuntu_2022.3 5.45% <69.81%> (+0.07%) ⬆️
inputsystem_Ubuntu_2022.3_project 74.51% <96.29%> (+0.11%) ⬆️
inputsystem_Ubuntu_6000.0 5.23% <69.81%> (+0.04%) ⬆️
inputsystem_Ubuntu_6000.0_project 76.43% <97.34%> (+0.11%) ⬆️
inputsystem_Ubuntu_6000.2 5.23% <69.81%> (+0.04%) ⬆️
inputsystem_Ubuntu_6000.2_project 76.43% <97.34%> (+0.11%) ⬆️
inputsystem_Ubuntu_6000.3 5.23% <69.81%> (+0.04%) ⬆️
inputsystem_Ubuntu_6000.3_project 76.42% <97.34%> (+0.11%) ⬆️
inputsystem_Ubuntu_6000.4 5.24% <69.81%> (+0.04%) ⬆️
inputsystem_Ubuntu_6000.4_project 76.44% <97.34%> (+0.11%) ⬆️
inputsystem_Ubuntu_6000.5 5.24% <69.81%> (?)
inputsystem_Ubuntu_6000.5_project 76.44% <97.34%> (?)
inputsystem_Windows_2022.3 5.44% <69.81%> (+0.07%) ⬆️
inputsystem_Windows_2022.3_project 74.84% <96.29%> (+0.11%) ⬆️
inputsystem_Windows_6000.0 5.23% <69.81%> (+0.04%) ⬆️
inputsystem_Windows_6000.0_project 76.75% <97.34%> (+0.10%) ⬆️
inputsystem_Windows_6000.2 5.23% <69.81%> (+0.04%) ⬆️
inputsystem_Windows_6000.2_project 76.75% <97.34%> (+0.10%) ⬆️
inputsystem_Windows_6000.3 5.23% <69.81%> (+0.04%) ⬆️
inputsystem_Windows_6000.3_project 76.75% <97.34%> (+0.10%) ⬆️
inputsystem_Windows_6000.4 5.23% <69.81%> (+0.04%) ⬆️
inputsystem_Windows_6000.4_project 76.75% <97.34%> (+0.11%) ⬆️
inputsystem_Windows_6000.5 5.23% <69.81%> (?)
inputsystem_Windows_6000.5_project 76.75% <97.34%> (?)

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

Files with missing lines Coverage Δ
Assets/Tests/InputSystem/CoreTests_Actions.cs 97.78% <100.00%> (+<0.01%) ⬆️
...ssets/Tests/InputSystem/Plugins/InputForUITests.cs 96.70% <100.00%> (+0.34%) ⬆️
...utSystem/Plugins/InputForUI/InputSystemProvider.cs 86.60% <ø> (-0.37%) ⬇️
.../com.unity.inputsystem/InputSystem/InputManager.cs 89.26% <94.33%> (+0.48%) ⬆️

... and 24 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jfreire-unity jfreire-unity force-pushed the isxb-1671-outoffocus-events-trigger-actions branch 2 times, most recently from dc01440 to f21ad55 Compare September 11, 2025 13:08
@jfreire-unity jfreire-unity force-pushed the isxb-1671-outoffocus-events-trigger-actions branch from f21ad55 to 6157498 Compare September 11, 2025 13:11
It's tested against all background behavior settings.
The goal is here is to make those conditions self contained in "chunks" so that we can slightly reduce the cognitive load when reading through the OnUpdate() function.

This is a personal preference and others in the team might disagree with these changes.
@jfreire-unity jfreire-unity marked this pull request as ready for review September 12, 2025 11:36
Copy link
Collaborator

@ritamerkl ritamerkl left a comment

Choose a reason for hiding this comment

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

I am hesitant introducing such a big change in the InputManager, also looking at it I wonder:
Isn't the wrong event the mousePosition sent from DeviceSync? Since the event before should not cause the MouseDown, but the second event (caused by the DeviceSync). Or did I get that wrong?

@jfreire-unity
Copy link
Collaborator Author

I am hesitant introducing such a big change in the InputManager, also looking at it I wonder: Isn't the wrong event the mousePosition sent from DeviceSync?

The wrong event is the one before the DeviceSync. That's why we should discard it and not process it.
With this PR we make sure that we only process events received AFTER focus was regained.

I am hesitant introducing such a big change in the InputManager

I don't see it as a big change but maybe the small refactoring is "polluting" the PR in that sense? If you look at 6157498 there are a bit smaller changes and I just factored out some methods.

Copy link
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.

It's a bit difficult at the moment to judge what is refactoring and what is bug fixing / change in behavior on this PR. I will come back with additional comments once I have realised this.

@jfreire-unity
Copy link
Collaborator Author

jfreire-unity commented Oct 8, 2025

It's a bit difficult at the moment to judge what is refactoring and what is bug fixing / change in behavior on this PR. I will come back with additional comments once I have realised this.

@ekcoh could you try looking at 6157498 first, since it has the least amount of changes? Let me know if it's better.
The refactor is just encapsulating the previously existing conditions into single functions.
But if it's still confusing, I can redo this without any of these changes.

Copy link
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.

Commenting for now since the PR states it will add more tests and I added some suggestions. Generally I believe this is an improvement and happy to see some refactoring happening on InputManager. Regarding the refactor I would just like to make sure we have test coverage on the conditions before and after to avoid regressing something. It looked quite similar though and I couldn't spot any mistake there.
My other concern is regarding whether the focus change callback is direct or indirect. If direct it may lead to out-of-order processing of focus change and events which is why I have mentioned previously that we should have our focus changes in the event queue to make it trivial to reason about. If indirect we have a problem since we do not have the focus events true timestamp. Getting a timestamp when the callback is invoked doesn't need to imply that its the true timestamp of the event on the event timeline.

var mousePointAction = new InputAction(binding: "<Mouse>/position", type: InputActionType.PassThrough);
mousePointAction.Enable();

using (var trace = new InputActionTrace(mousePointAction))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would recommend letting the time step parameter also be an input parameter to the test case. It should still be valid for timeStep = 0.0f, since our logic need to handle event order as the determining factor when time isn't sufficient to tell them apart.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So I would recommend adding that test as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In that case, this would not work. Time needs to move; otherwise, we can't know what events have occurred before and after the focus. This solution relies on event timestamps. Similar to what we do for transitions between Edit Mode and Entering Play Mode, see the conditions in ShouldDiscardEditModeTransitionEvent(). Let me know if I'm missing something.

In theory, if we had focus events in the queues, we wouldn't need to know about timestamps. However, as we discussed, there is a higher risk in introducing such a change. We should still conduct an exploration of the implementation, but I would prefer to land first a less risky solution for this particular bug.

I'm pappy to prototype a solution together if you like, after this fix. We can sync on Slack.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it's better to use timestamps than what we had before and in practise it's likely not a problem. Still safe to land as is though and guess we need it in the queue to have a strictly accurate implementation.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we can also add an inline comment to this test case that with a strictly defined event queue containing all relevant inputs without downsampling the test case should be expanded to be time-agnostic for a fixed timestamp?

private bool m_NativeBeforeUpdateHooked;
private bool m_HaveDevicesWithStateCallbackReceivers;
private bool m_HasFocus;
private bool m_DiscardOutOfFocusEvents;
Copy link
Collaborator

Choose a reason for hiding this comment

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

A bit of a corner case, but what happens with these if there is a domain reload while in playmode? Will they be reinitialised to the same values they had before the domain reload was initiated?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hm, good question. How can I trigger a domain reload in play mode so I can test that?

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can either:

  • Change a script manually, that will trigger a domain reload
  • Make a script hat calls into EditorUtility.RequestScriptReload

Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you conclude this @jfreire-unity ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

They are initialized to their default values (false for the boolean, 0.0 to the timestamp).
I'm not seeing a reason why they should have their same values as before the domain reload. The timestamp will always be "old". But maybe I'm missing some detail or a pattern.

@ekcoh
Copy link
Collaborator

ekcoh commented Oct 21, 2025

@jfreire-unity Please update this branch with changes from develop to get critical CI fix from #2260. I do not dare updating it for you since the branch has conflicts.

@jfreire-unity jfreire-unity force-pushed the isxb-1671-outoffocus-events-trigger-actions branch from 05cf85f to 6fb0bb3 Compare October 22, 2025 15:42
Copy link
Collaborator

@Pauliusd01 Pauliusd01 left a comment

Choose a reason for hiding this comment

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

Issue still occurs for me if I switch the resolution from free aspect to one of the predefined ones.
https://github.com/user-attachments/assets/9a530010-e9cb-432c-9c9a-b047a3a0b4e6

@jfreire-unity
Copy link
Collaborator Author

Issue still occurs for me if I switch the resolution from free aspect to one of the predefined ones. https://github.com/user-attachments/assets/9a530010-e9cb-432c-9c9a-b047a3a0b4e6

I can't access that image

@benoitalain
Copy link
Collaborator

Just chiming in to bring back a point that was mentioned in our early discussions about this issue. In an ideal world, we'd have to be able to click inside the GameView directly and have the first click that happened before we changed focus be processed as though it had been a normal click after focus. Most software allow that, where you change your window focus and at the same time you click a button from the other window. In that case, if the coordinates of the click (as expressed in the new focused window) are valid, we should accept the click. I don't know how computing new coordinates after the fact is feasible in general though.

@jfreire-unity
Copy link
Collaborator Author

Issue still occurs for me if I switch the resolution from free aspect to one of the predefined ones. https://github.com/user-attachments/assets/9a530010-e9cb-432c-9c9a-b047a3a0b4e6

Could you test the same on develop but by adding a EventSystem game object which will also add InputSystemUIInputModule? Feel free to ping me on Slack.

I believe the issue you are reporting happens regardless of using InputForUI, so it wouldn't be a regression.

@Pauliusd01 Pauliusd01 self-requested a review October 31, 2025 08:43
@Pauliusd01
Copy link
Collaborator

Issue still occurs for me if I switch the resolution from free aspect to one of the predefined ones. https://github.com/user-attachments/assets/9a530010-e9cb-432c-9c9a-b047a3a0b4e6

Could you test the same on develop but by adding a EventSystem game object which will also add InputSystemUIInputModule? Feel free to ping me on Slack.

I believe the issue you are reporting happens regardless of using InputForUI, so it wouldn't be a regression.

Here's what it's looking to me:

With the event system object:

  • Your PR: doesn't repro with free aspect option, reproes with custom ones
  • Develop: doesn't repro with free aspect option, reproes with custom ones

Without the event system object (default user project)

  • Your PR: doesn't repro with free aspect option, reproes with custom ones
  • Develop: reproes with free aspect option, reproes with custom ones

So It's still an improvement and the custom resolution thing is not a new regression, if you want it to be a separate bug that sounds fine to me. Only thing though is I'm seeing a lot of CI failures

@jfreire-unity
Copy link
Collaborator Author

jfreire-unity commented Nov 4, 2025

Issue still occurs for me if I switch the resolution from free aspect to one of the predefined ones. https://github.com/user-attachments/assets/9a530010-e9cb-432c-9c9a-b047a3a0b4e6

Could you test the same on develop but by adding a EventSystem game object which will also add InputSystemUIInputModule? Feel free to ping me on Slack.
I believe the issue you are reporting happens regardless of using InputForUI, so it wouldn't be a regression.

Here's what it's looking to me:

With the event system object:

  • Your PR: doesn't repro with free aspect option, reproes with custom ones
  • Develop: doesn't repro with free aspect option, reproes with custom ones

Without the event system object (default user project)

  • Your PR: doesn't repro with free aspect option, reproes with custom ones
  • Develop: reproes with free aspect option, reproes with custom ones

So It's still an improvement and the custom resolution thing is not a new regression, if you want it to be a separate bug that sounds fine to me. Only thing though is I'm seeing a lot of CI failures

Thanks for testing!

Yes, I would raise it as different bug since it is something already happenin with Event System + UIInputModule and will likely require trunk changes. It is not a focus change issue.
I prefer to land this small improvements to at least fix the regression reported.

I'm looking into the CI issues and will ping reviewers once those are fixed

Doesn't make sense to run them in the Player as they are editor specific.
@jfreire-unity jfreire-unity requested a review from ekcoh November 5, 2025 08:36
Copy link
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.

Thanks for doing this improvement/fix. I will approve now since I believe this makes it more correct than existing solution.

var mousePointAction = new InputAction(binding: "<Mouse>/position", type: InputActionType.PassThrough);
mousePointAction.Enable();

using (var trace = new InputActionTrace(mousePointAction))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it's better to use timestamps than what we had before and in practise it's likely not a problem. Still safe to land as is though and guess we need it in the queue to have a strictly accurate implementation.

var mousePointAction = new InputAction(binding: "<Mouse>/position", type: InputActionType.PassThrough);
mousePointAction.Enable();

using (var trace = new InputActionTrace(mousePointAction))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we can also add an inline comment to this test case that with a strictly defined event queue containing all relevant inputs without downsampling the test case should be expanded to be time-agnostic for a fixed timestamp?

private bool m_NativeBeforeUpdateHooked;
private bool m_HaveDevicesWithStateCallbackReceivers;
private bool m_HasFocus;
private bool m_DiscardOutOfFocusEvents;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you conclude this @jfreire-unity ?

@ritamerkl ritamerkl requested review from ritamerkl and removed request for ritamerkl November 5, 2025 15:27
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.

7 participants