Skip to content

Commit f78e947

Browse files
committed
Refactor some conditions to improve readability
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.
1 parent 300896c commit f78e947

File tree

1 file changed

+109
-41
lines changed

1 file changed

+109
-41
lines changed

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

Lines changed: 109 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3205,50 +3205,18 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
32053205
var timesliceEvents = (updateType == InputUpdateType.Fixed || updateType == InputUpdateType.BeforeRender) &&
32063206
InputSystem.settings.updateMode == InputSettings.UpdateMode.ProcessEventsInFixedUpdate;
32073207

3208-
// Figure out if we can just flush the buffer and early out.
3209-
var canFlushBuffer =
3210-
false
3211-
#if UNITY_EDITOR
3212-
// If out of focus and runInBackground is off and ExactlyAsInPlayer is on, discard input.
3213-
|| (!gameHasFocus && m_Settings.editorInputBehaviorInPlayMode == InputSettings.EditorInputBehaviorInPlayMode.AllDeviceInputAlwaysGoesToGameView &&
3214-
(!m_Runtime.runInBackground ||
3215-
m_Settings.backgroundBehavior == InputSettings.BackgroundBehavior.ResetAndDisableAllDevices))
3216-
#else
3217-
|| (!gameHasFocus && !m_Runtime.runInBackground)
3218-
#endif
3219-
;
3220-
var canEarlyOut =
3221-
// Early out if there's no events to process.
3222-
eventBuffer.eventCount == 0
3223-
|| canFlushBuffer
3208+
// Determine if we should flush the event buffer which would imply we exit early and do not process
3209+
// any of those events, ever.
3210+
var shouldFlushEventBuffer = ShouldFlushEventBuffer();
3211+
// When we exit early, we may or may not flush the event buffer. It depends if we want to process events
3212+
// later once this method is called.
3213+
var shouldExitEarly = ShouldExitEarlyFromEventProcessing(eventBuffer, shouldFlushEventBuffer, updateType);
32243214

32253215
#if UNITY_EDITOR
3226-
// If we're in the background and not supposed to process events in this update (but somehow
3227-
// still ended up here), we're done.
3228-
|| ((!gameHasFocus || gameShouldGetInputRegardlessOfFocus) &&
3229-
((m_Settings.backgroundBehavior == InputSettings.BackgroundBehavior.ResetAndDisableAllDevices && updateType != InputUpdateType.Editor)
3230-
|| (m_Settings.editorInputBehaviorInPlayMode == InputSettings.EditorInputBehaviorInPlayMode.AllDevicesRespectGameViewFocus && updateType != InputUpdateType.Editor)
3231-
|| (m_Settings.backgroundBehavior == InputSettings.BackgroundBehavior.IgnoreFocus && m_Settings.editorInputBehaviorInPlayMode == InputSettings.EditorInputBehaviorInPlayMode.AllDeviceInputAlwaysGoesToGameView && updateType == InputUpdateType.Editor)
3232-
)
3233-
// When the game is playing and has focus, we never process input in editor updates. All we
3234-
// do is just switch to editor state buffers and then exit.
3235-
|| (gameIsPlaying && gameHasFocus && updateType == InputUpdateType.Editor))
3216+
var dropStatusEvents = ShouldDropStatusEvents(eventBuffer, ref shouldExitEarly);
32363217
#endif
3237-
;
32383218

3239-
3240-
#if UNITY_EDITOR
3241-
var dropStatusEvents = false;
3242-
if (!gameIsPlaying && gameShouldGetInputRegardlessOfFocus && (eventBuffer.sizeInBytes > (100 * 1024)))
3243-
{
3244-
// If the game is not playing but we're sending all input events to the game, the buffer can just grow unbounded.
3245-
// So, in that case, set a flag to say we'd like to drop status events, and do not early out.
3246-
canEarlyOut = false;
3247-
dropStatusEvents = true;
3248-
}
3249-
#endif
3250-
3251-
if (canEarlyOut)
3219+
if (shouldExitEarly)
32523220
{
32533221
// Normally, we process action timeouts after first processing all events. If we have no
32543222
// events, we still need to check timeouts.
@@ -3257,7 +3225,7 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
32573225

32583226
k_InputUpdateProfilerMarker.End();
32593227
InvokeAfterUpdateCallback(updateType);
3260-
if (canFlushBuffer)
3228+
if (shouldFlushEventBuffer)
32613229
eventBuffer.Reset();
32623230
m_CurrentUpdate = default;
32633231
return;
@@ -3699,7 +3667,107 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
36993667
m_CurrentUpdate = default;
37003668
}
37013669

3670+
/// <summary>
3671+
/// Determines if the event buffer should be flushed without processing events.
3672+
/// </summary>
3673+
/// <returns>True if the buffer should be flushed, false otherwise.</returns>
3674+
private bool ShouldFlushEventBuffer()
3675+
{
37023676
#if UNITY_EDITOR
3677+
// If out of focus and runInBackground is off and ExactlyAsInPlayer is on, discard input.
3678+
if (!gameHasFocus &&
3679+
m_Settings.editorInputBehaviorInPlayMode == InputSettings.EditorInputBehaviorInPlayMode.AllDeviceInputAlwaysGoesToGameView
3680+
&&
3681+
(!m_Runtime.runInBackground || m_Settings.backgroundBehavior == InputSettings.BackgroundBehavior.ResetAndDisableAllDevices))
3682+
return true;
3683+
#else
3684+
// In player builds, flush if out of focus and not running in background
3685+
if (!gameHasFocus && !m_Runtime.runInBackground)
3686+
return true;
3687+
#endif
3688+
return false;
3689+
}
3690+
3691+
/// <summary>
3692+
/// Determines if we should exit early from event processing without handling events.
3693+
/// </summary>
3694+
/// <param name="eventBuffer">The current event buffer</param>
3695+
/// <param name="canFlushBuffer">Whether the buffer can be flushed</param>
3696+
/// <param name="updateType">The current update type</param>
3697+
/// <returns>True if we should exit early, false otherwise.</returns>
3698+
private bool ShouldExitEarlyFromEventProcessing(InputEventBuffer eventBuffer, bool canFlushBuffer, InputUpdateType updateType)
3699+
{
3700+
// Early out if there are no events to process
3701+
if (eventBuffer.eventCount == 0)
3702+
return true;
3703+
3704+
// Early out if we can flush the buffer
3705+
if (canFlushBuffer)
3706+
return true;
3707+
3708+
#if UNITY_EDITOR
3709+
// Check various PlayMode specific early exit conditions
3710+
if (ShouldExitEarlyInEditor(updateType))
3711+
return true;
3712+
3713+
// When the game is playing and has focus, we never process input in editor updates.
3714+
// All we do is just switch to editor state buffers and then exit.
3715+
if ((gameIsPlaying && gameHasFocus && updateType == InputUpdateType.Editor))
3716+
return true;
3717+
#endif
3718+
3719+
return false;
3720+
}
3721+
3722+
#if UNITY_EDITOR
3723+
/// <summary>
3724+
/// Checks editor-specific conditions for early exit from event processing.
3725+
/// </summary>
3726+
/// <param name="updateType">The current update type</param>
3727+
/// <returns>True if we should exit early in editor context, false otherwise.</returns>
3728+
/// <remarks>
3729+
/// Whenever this method returns true, it usually means that events are left in the buffer and should be
3730+
/// processed in a next update call.
3731+
/// </remarks>
3732+
private bool ShouldExitEarlyInEditor(InputUpdateType updateType)
3733+
{
3734+
// In Play Mode, if we're in the background and not supposed to process events in this update
3735+
if ((!gameHasFocus || gameShouldGetInputRegardlessOfFocus) && updateType != InputUpdateType.Editor)
3736+
{
3737+
if (m_Settings.backgroundBehavior == InputSettings.BackgroundBehavior.ResetAndDisableAllDevices ||
3738+
m_Settings.editorInputBehaviorInPlayMode == InputSettings.EditorInputBehaviorInPlayMode.AllDevicesRespectGameViewFocus)
3739+
return true;
3740+
}
3741+
3742+
// Special case for IgnoreFocus behavior with AllDeviceInputAlwaysGoesToGameView in editor updates
3743+
if ((!gameHasFocus || gameShouldGetInputRegardlessOfFocus) &&
3744+
m_Settings.backgroundBehavior == InputSettings.BackgroundBehavior.IgnoreFocus &&
3745+
m_Settings.editorInputBehaviorInPlayMode == InputSettings.EditorInputBehaviorInPlayMode.AllDeviceInputAlwaysGoesToGameView &&
3746+
updateType == InputUpdateType.Editor)
3747+
return true;
3748+
3749+
return false;
3750+
}
3751+
3752+
/// <summary>
3753+
/// Determines if status events should be dropped and modifies early exit behavior accordingly.
3754+
/// </summary>
3755+
/// <param name="eventBuffer">The current event buffer</param>
3756+
/// <param name="canEarlyOut">Reference to the early exit flag that may be modified</param>
3757+
/// <returns>True if status events should be dropped, false otherwise.</returns>
3758+
private bool ShouldDropStatusEvents(InputEventBuffer eventBuffer, ref bool canEarlyOut)
3759+
{
3760+
// If the game is not playing but we're sending all input events to the game,
3761+
// the buffer can just grow unbounded. So, in that case, set a flag to say we'd
3762+
// like to drop status events, and do not early out.
3763+
if (!gameIsPlaying && gameShouldGetInputRegardlessOfFocus && (eventBuffer.sizeInBytes > (100 * 1024)))
3764+
{
3765+
canEarlyOut = false;
3766+
return true;
3767+
}
3768+
return false;
3769+
}
3770+
37033771
/// <summary>
37043772
/// Determines if an event should be discarded based on timing or focus state.
37053773
/// </summary>

0 commit comments

Comments
 (0)