@@ -35,98 +35,103 @@ internal void OnFocusChanged(bool focus)
3535 m_IsHandlingFocusChange = true ;
3636 m_ApplicationHadFocus = ! focus ;
3737
38+ try
39+ {
3840#if UNITY_EDITOR
39- SyncAllDevicesWhenEditorIsActivated ( ) ;
41+ SyncAllDevicesWhenEditorIsActivated ( ) ;
4042
41- if ( ! m_Runtime . isInPlayMode )
42- {
43- m_IsHandlingFocusChange = false ;
44- return ;
45- }
43+ if ( ! m_Runtime . isInPlayMode )
44+ return ;
4645
47- var gameViewFocus = m_Settings . editorInputBehaviorInPlayMode ;
46+ var gameViewFocus = m_Settings . editorInputBehaviorInPlayMode ;
4847#endif
4948
50- var runInBackground =
49+ var runInBackground =
5150#if UNITY_EDITOR
52- // In the editor, the player loop will always be run even if the Game View does not have focus. This
53- // amounts to runInBackground being always true in the editor, regardless of what the setting in
54- // the Player Settings window is.
55- //
56- // If, however, "Game View Focus" is set to "Exactly As In Player", we force code here down the same
57- // path as in the player.
58- gameViewFocus != InputSettings . EditorInputBehaviorInPlayMode . AllDeviceInputAlwaysGoesToGameView || m_Runtime . runInBackground ;
51+
52+ // In the editor, the player loop will always be run even if the Game View does not have focus. This
53+ // amounts to runInBackground being always true in the editor, regardless of what the setting in
54+ // the Player Settings window is.
55+ //
56+ // If, however, "Game View Focus" is set to "Exactly As In Player", we force code here down the same
57+ // path as in the player.
58+ gameViewFocus != InputSettings . EditorInputBehaviorInPlayMode . AllDeviceInputAlwaysGoesToGameView || m_Runtime . runInBackground ;
5959#else
60- m_Runtime . runInBackground ;
60+ m_Runtime . runInBackground ;
6161#endif
6262
63- if ( m_Settings . backgroundBehavior == InputSettings . BackgroundBehavior . IgnoreFocus && runInBackground )
64- {
65- // If runInBackground is true, no device changes should happen, even when focus is gained. So early out.
66- // If runInBackground is false, we still want to sync devices when focus is gained. So we need to continue further.
67- m_IsHandlingFocusChange = false ;
68- return ;
69- }
63+ if ( m_Settings . backgroundBehavior == InputSettings . BackgroundBehavior . IgnoreFocus && runInBackground )
64+ {
65+ // If runInBackground is true, no device changes should happen, even when focus is gained. So early out.
66+ // If runInBackground is false, we still want to sync devices when focus is gained. So we need to continue further.
67+ return ;
68+ }
7069
7170#if UNITY_EDITOR
72- // Set the current update type while we process the focus changes to make sure we
73- // feed into the right buffer. No need to do this in the player as it doesn't have
74- // the editor/player confusion.
75- m_CurrentUpdate = m_UpdateMask . GetUpdateTypeForPlayer ( ) ;
71+
72+ // Set the current update type while we process the focus changes to make sure we
73+ // feed into the right buffer. No need to do this in the player as it doesn't have
74+ // the editor/player confusion.
75+ m_CurrentUpdate = m_UpdateMask . GetUpdateTypeForPlayer ( ) ;
7676#endif
7777
78- if ( ! focus )
79- {
80- // We only react to loss of focus when we will keep running in the background. If not,
81- // we'll do nothing and just wait for focus to come back (where we then try to sync all devices).
82- if ( runInBackground )
78+ if ( ! focus )
8379 {
84- for ( var i = 0 ; i < m_DevicesCount ; ++ i )
80+ // We only react to loss of focus when we will keep running in the background. If not,
81+ // we'll do nothing and just wait for focus to come back (where we then try to sync all devices).
82+ if ( runInBackground )
8583 {
86- // Determine whether to run this device in the background.
87- var device = m_Devices [ i ] ;
88- if ( ! device . enabled || ShouldRunDeviceInBackground ( device ) )
89- continue ;
84+ for ( var i = 0 ; i < m_DevicesCount ; ++ i )
85+ {
86+ // Determine whether to run this device in the background.
87+ var device = m_Devices [ i ] ;
88+ if ( ! device . enabled || ShouldRunDeviceInBackground ( device ) )
89+ continue ;
9090
91- // Disable the device. This will also soft-reset it.
92- EnableOrDisableDevice ( device , false , DeviceDisableScope . TemporaryWhilePlayerIsInBackground ) ;
91+ // Disable the device. This will also soft-reset it.
92+ EnableOrDisableDevice ( device , false , DeviceDisableScope . TemporaryWhilePlayerIsInBackground ) ;
9393
94- // In case we invoked a callback that messed with our device array, adjust our index.
95- var index = m_Devices . IndexOfReference ( device , m_DevicesCount ) ;
96- if ( index == - 1 )
97- -- i ;
98- else
99- i = index ;
94+ // In case we invoked a callback that messed with our device array, adjust our index.
95+ var index = m_Devices . IndexOfReference ( device , m_DevicesCount ) ;
96+ if ( index == - 1 )
97+ -- i ;
98+ else
99+ i = index ;
100+ }
100101 }
101102 }
102- }
103- else
104- {
105- m_DiscardOutOfFocusEvents = true ;
106- m_FocusRegainedTime = m_Runtime . currentTime ;
107- // On focus gain, reenable and sync devices.
108- for ( var i = 0 ; i < m_DevicesCount ; ++ i )
103+ else
109104 {
110- var device = m_Devices [ i ] ;
105+ m_DiscardOutOfFocusEvents = true ;
106+ m_FocusRegainedTime = m_Runtime . currentTime ;
111107
112- // Re-enable the device if we disabled it on focus loss. This will also issue a sync.
113- if ( device . disabledWhileInBackground )
114- EnableOrDisableDevice ( device , true , DeviceDisableScope . TemporaryWhilePlayerIsInBackground ) ;
115- // Try to sync. If it fails and we didn't run in the background, perform
116- // a reset instead. This is to cope with backends that are unable to sync but
117- // may still retain state which now may be outdated because the input device may
118- // have changed state while we weren't running. So at least make the backend flush
119- // its state (if any).
120- else if ( device . enabled && ! runInBackground && ! device . RequestSync ( ) && m_Settings . backgroundBehavior != InputSettings . BackgroundBehavior . IgnoreFocus )
121- ResetDevice ( device ) ;
108+ // On focus gain, reenable and sync devices.
109+ for ( var i = 0 ; i < m_DevicesCount ; ++ i )
110+ {
111+ var device = m_Devices [ i ] ;
112+
113+ // Re-enable the device if we disabled it on focus loss. This will also issue a sync.
114+ if ( device . disabledWhileInBackground )
115+ EnableOrDisableDevice ( device , true , DeviceDisableScope . TemporaryWhilePlayerIsInBackground ) ;
116+
117+ // Try to sync. If it fails and we didn't run in the background, perform
118+ // a reset instead. This is to cope with backends that are unable to sync but
119+ // may still retain state which now may be outdated because the input device may
120+ // have changed state while we weren't running. So at least make the backend flush
121+ // its state (if any).
122+ else if ( device . enabled && ! runInBackground && ! device . RequestSync ( ) && m_Settings . backgroundBehavior != InputSettings . BackgroundBehavior . IgnoreFocus )
123+ ResetDevice ( device ) ;
124+ }
122125 }
123- }
124126
125127#if UNITY_EDITOR
126- m_CurrentUpdate = InputUpdateType . None ;
128+ m_CurrentUpdate = InputUpdateType . None ;
127129#endif
128-
129- m_IsHandlingFocusChange = false ;
130+ }
131+ finally
132+ {
133+ m_IsHandlingFocusChange = false ;
134+ }
130135 }
131136
132137 /// <summary>
0 commit comments