Search Issue Tracker
Fixed in 2021.1.X
Votes
0
Found in
2018.4
2020.2.0b5
2021.1
Issue ID
1283510
Regression
No
OnApplicationFocus sets the hasFocus variable to true and instantly to false when focusing on the Editor after it was unfocused
How to reproduce:
1. Open the attached "OnApplicationFocus" project
2. Open the "SampleScene" Scene
3. Enter the Play Mode
4. Click on the desktop to unfocus the Editor
5. Click on the Inspector in the Editor
Expected results: No new messages are printed in the Console
Actual results: A "Focus = true" message is printed in the Console instantly followed by a "Focus = false" message
Reproducible with: 2018.4.28f1, 2019.4.12f1, 2020.1.9f1, 2020.2.0b7, 2021.1.0a1
Notes:
- This issue has been tested and reproduced on Windows 10
- This is reproducible only after the "hasFocus" variable been set to true
- Pressing on the Game view to focus doesn't produce the issue
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- Articulation Body with 'Revolute' Joint Type has erratic behavior when Upper Limit is set to above 360
- WebGL Player fails to render Scene when Terrain with Detail Mesh is added and WebGPU Graphics API is used
- Inconsistent errors are logged when different types are passed into the Query "Q<>" method in UIToolkit and the ancestor VisualElement is null
- Crash on GetMaterialPropertyByIndex when opening a specific Scene
- Discrepancies in the styling are present when using a TSS file instead of a USS file in custom EditorWindow
Resolution Note (fix version 2021.1):
This is by design. Application.isFocused and MonoBehaviour.OnApplicationFocus(bool) are true when the Game View is focused. https://docs.unity3d.com/ScriptReference/Application-isFocused.html
When clicking from the Game View to the desktop, The Game View loses focus, but it is still the primary Unity Editor tab in that window. If you then click away from the Editor and then return to the Editor by clicking on a different tab, the application will activate the primary tab and then switch to the tab you clicked on.
So in in this case, the Game View is the focused tab.
| Action | Response
|------------------------------|----------------------------------------------------------
| Click Unity Editor Game View | Game View gains focus -> OnApplicationFocus(true) is called.
| Click the desktop | Game View loses focus -> OnApplicationFocus(false) is called.
| Click the Hierarchy tab | Game View regains focus -> OnApplicationFocus(true) is called.
| | Game View loses focus -> OnApplicationFocus(false) is called.
| | Hierarchy tab gains focus
| Click Unity Editor Game View | Game View gains focus -> OnApplicationFocus(true) is called.
If you click to and from the Game View repeatedly, it does not have this extra set of calls, and just calls OnApplicationFocus(true) and OnApplicationFocus(false) as expected.
The documentation has been updated in Unity 2021.1.0a9