Skip to content

Commit ea8728c

Browse files
committed
Fixed regression with initial focus causing added input devices to be disabled
- Fixed regression caused by #2365 where the focus state of the application was not being synced to the input runtime correctly, causing all input devices to be initially disabled until the game window was clicked away and then back. - Fixed some incorrect setting of the `focusState` by just setting or clearing the ApplicationFocus flag instead of setting the entire value. In effect this has no current difference because the flags enum only has two values, but makes it future-proof.
1 parent e492059 commit ea8728c

3 files changed

Lines changed: 27 additions & 27 deletions

File tree

Packages/com.unity.inputsystem/InputSystem/Runtime/IInputRuntime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ internal unsafe interface IInputRuntime
113113
/// <summary>
114114
/// Set delegate to call when the application changes focus.
115115
/// </summary>
116-
/// <seealso cref="Application.onFocusChanged"/>
116+
/// <seealso cref="Application.focusChanged"/>
117117
Action<bool> onPlayerFocusChanged { get; set; }
118118
#endif
119119

Packages/com.unity.inputsystem/InputSystem/Runtime/InputManager.LegacyFocusHandling.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal enum FocusFlags : ushort
2222
/// <summary>
2323
/// In editor this means the GameView has focus. In a built player this means the player has focus.
2424
/// </summary>
25-
ApplicationFocus = (1 << 0)
25+
ApplicationFocus = (1 << 0),
2626
};
2727

2828
internal partial class InputManager
@@ -34,7 +34,7 @@ internal void OnFocusChanged(bool focus)
3434

3535
if (!m_Runtime.isInPlayMode)
3636
{
37-
focusState = focus ? FocusFlags.ApplicationFocus : FocusFlags.None;
37+
SetRuntimeFocusState(focus);
3838
return;
3939
}
4040

@@ -59,7 +59,7 @@ internal void OnFocusChanged(bool focus)
5959
{
6060
// If runInBackground is true, no device changes should happen, even when focus is gained. So early out.
6161
// If runInBackground is false, we still want to sync devices when focus is gained. So we need to continue further.
62-
focusState = focus ? FocusFlags.ApplicationFocus : FocusFlags.None;
62+
SetRuntimeFocusState(focus);
6363
return;
6464
}
6565

@@ -122,7 +122,7 @@ internal void OnFocusChanged(bool focus)
122122
#endif
123123

124124
// We set this *after* the block above as defaultUpdateType is influenced by the setting.
125-
focusState = focus ? FocusFlags.ApplicationFocus : FocusFlags.None;
125+
SetRuntimeFocusState(focus);
126126
}
127127

128128
/// <summary>
@@ -150,8 +150,6 @@ private bool ShouldFlushEventBuffer()
150150
/// <summary>
151151
/// Determines if we should exit early from event processing without handling events.
152152
/// </summary>
153-
/// <param name="eventBuffer">The current event buffer</param>
154-
/// <param name="canFlushBuffer">Whether the buffer can be flushed</param>
155153
/// <param name="updateType">The current update type</param>
156154
/// <returns>True if we should exit early, false otherwise.</returns>
157155
[MethodImpl(MethodImplOptions.AggressiveInlining)]

Packages/com.unity.inputsystem/InputSystem/Runtime/InputManager.cs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -268,20 +268,15 @@ public FocusFlags focusState
268268
{
269269
get
270270
{
271-
#if UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
272-
return m_Runtime.focusState;
273-
#else
274-
return m_FocusState;
275-
#endif
271+
if (m_Runtime != null)
272+
return m_Runtime.focusState;
273+
274+
return Application.isFocused ? FocusFlags.ApplicationFocus : FocusFlags.None;
276275
}
277276
set
278277
{
279-
#if UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
280278
if (m_Runtime != null)
281279
m_Runtime.focusState = value;
282-
#else
283-
m_FocusState = value;
284-
#endif
285280
}
286281
}
287282

@@ -557,11 +552,11 @@ internal static void StopEditorEventPassthrough()
557552
#else
558553
true;
559554
#endif
560-
private bool applicationHasFocus => (focusState & FocusFlags.ApplicationFocus) != FocusFlags.None;
555+
private bool applicationHasFocus => m_Runtime != null ? m_Runtime.isPlayerFocused : Application.isFocused;
561556

562557
private bool gameHasFocus =>
563558
#if UNITY_EDITOR
564-
m_RunPlayerUpdatesInEditMode || applicationHasFocus || gameShouldGetInputRegardlessOfFocus || isEditorEventPassthroughActive;
559+
m_RunPlayerUpdatesInEditMode || applicationHasFocus || gameShouldGetInputRegardlessOfFocus || isEditorEventPassthroughActive;
565560
#else
566561
applicationHasFocus || gameShouldGetInputRegardlessOfFocus;
567562
#endif
@@ -2027,11 +2022,6 @@ internal void InitializeData()
20272022
// we don't know which one the user is going to use. The user
20282023
// can manually turn off one of them to optimize operation.
20292024
m_UpdateMask = InputUpdateType.Dynamic | InputUpdateType.Fixed;
2030-
#if !UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
2031-
m_FocusState = Application.isFocused
2032-
? m_FocusState | FocusFlags.ApplicationFocus
2033-
: m_FocusState & ~FocusFlags.ApplicationFocus;
2034-
#endif
20352025
#if UNITY_EDITOR
20362026
m_EditorIsActive = true;
20372027
m_UpdateMask |= InputUpdateType.Editor;
@@ -2277,9 +2267,7 @@ internal void InstallRuntime(IInputRuntime runtime)
22772267
#endif
22782268
m_Runtime.pollingFrequency = pollingFrequency;
22792269

2280-
focusState = m_Runtime.isPlayerFocused
2281-
? focusState | FocusFlags.ApplicationFocus
2282-
: focusState & ~FocusFlags.ApplicationFocus;
2270+
SetRuntimeFocusState(Application.isFocused);
22832271

22842272
// We only hook NativeInputSystem.onBeforeUpdate if necessary.
22852273
if (m_BeforeUpdateListeners.length > 0 || m_HaveDevicesWithStateCallbackReceivers)
@@ -2451,7 +2439,6 @@ internal struct AvailableDevice
24512439
private bool m_NativeBeforeUpdateHooked;
24522440
private bool m_HaveDevicesWithStateCallbackReceivers;
24532441
#if !UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
2454-
private FocusFlags m_FocusState = FocusFlags.ApplicationFocus;
24552442
private bool m_DiscardOutOfFocusEvents;
24562443
private double m_FocusRegainedTime;
24572444
#endif
@@ -4059,6 +4046,21 @@ private bool ShouldDropStatusEvents(InputEventBuffer eventBuffer)
40594046

40604047
#endif // UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
40614048

4049+
/// <summary>
4050+
/// Set the focus state of the runtime, converting from the given boolean to either setting or clearing the focus flag.
4051+
/// </summary>
4052+
/// <param name="applicationFocus">The application focus state.</param>
4053+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4054+
private void SetRuntimeFocusState(bool applicationFocus)
4055+
{
4056+
if (m_Runtime != null)
4057+
{
4058+
m_Runtime.focusState = applicationFocus
4059+
? m_Runtime.focusState | FocusFlags.ApplicationFocus
4060+
: m_Runtime.focusState & ~FocusFlags.ApplicationFocus;
4061+
}
4062+
}
4063+
40624064
[MethodImpl(MethodImplOptions.AggressiveInlining)]
40634065
private void FinalizeUpdate(InputUpdateType updateType)
40644066
{

0 commit comments

Comments
 (0)