From 5d7a4816ee4b6c1d37b8dc604b5d8bd979895e19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=CC=8Akan=20Sidenvall?= Date: Thu, 3 Jul 2025 14:15:22 +0200 Subject: [PATCH 1/4] CHANGE: Rely to module to manage polling frequency. --- Packages/com.unity.inputsystem/CHANGELOG.md | 1 + .../InputSystem/InputManager.cs | 22 ++++++++++++++++++- .../InputSystem/InputSystem.cs | 2 +- .../InputSystem/NativeInputRuntime.cs | 11 ++++++++++ .../InputSystem/Unity.InputSystem.asmdef | 5 +++++ 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/Packages/com.unity.inputsystem/CHANGELOG.md b/Packages/com.unity.inputsystem/CHANGELOG.md index 280718a4f7..02db5a9f28 100644 --- a/Packages/com.unity.inputsystem/CHANGELOG.md +++ b/Packages/com.unity.inputsystem/CHANGELOG.md @@ -61,6 +61,7 @@ however, it has to be formatted properly to pass verification tests. ### Changed - Changed default input action asset name from New Controls to New Actions. - Added disabling of action maps in rebinding UI sample. +- `InputSystem.pollingFrequency` is now initialized based on recommended polling frequency by the underlying platform on Unity versions newer than 6000.3.0a1. It can still be customized (set) during run-time as usual. Increasing polling frequency beyond the default 60Hz leads to less probability of input loss and reduces input latency. ### Added - An alternative way to access if an action state reached a certain phase during this rendering frame (Update()). This can be utilized even if the InputSystem update mode is set to manual or FixedUpdate. It can be used to access the action phase during rendering, eg for perform updates to the UI. diff --git a/Packages/com.unity.inputsystem/InputSystem/InputManager.cs b/Packages/com.unity.inputsystem/InputSystem/InputManager.cs index cb75d7d6f6..d33dbb30a9 100644 --- a/Packages/com.unity.inputsystem/InputSystem/InputManager.cs +++ b/Packages/com.unity.inputsystem/InputSystem/InputManager.cs @@ -197,16 +197,28 @@ public InputSettings.ScrollDeltaBehavior scrollDeltaBehavior public float pollingFrequency { - get => m_PollingFrequency; + get + { + #if UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY + return m_Runtime.pollingFrequency; + #else + return m_PollingFrequency; + #endif + } + set { ////REVIEW: allow setting to zero to turn off polling altogether? if (value <= 0) throw new ArgumentException("Polling frequency must be greater than zero", "value"); + #if UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY + m_Runtime.pollingFrequency = value; + #else m_PollingFrequency = value; if (m_Runtime != null) m_Runtime.pollingFrequency = value; + #endif } } @@ -1888,8 +1900,10 @@ internal void InitializeData() m_ScrollDeltaBehavior = InputSettings.ScrollDeltaBehavior.UniformAcrossAllPlatforms; + #if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY // Default polling frequency is 60 Hz. m_PollingFrequency = 60; + #endif // Register layouts. // NOTE: Base layouts must be registered before their derived layouts @@ -2141,7 +2155,9 @@ internal struct AvailableDevice // Used by EditorInputControlLayoutCache to determine whether its state is outdated. internal int m_LayoutRegistrationVersion; + #if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY private float m_PollingFrequency; + #endif internal InputControlLayout.Collection m_Layouts; private TypeTable m_Processors; @@ -4092,7 +4108,9 @@ internal SerializedState SaveState() return new SerializedState { layoutRegistrationVersion = m_LayoutRegistrationVersion, + #if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY pollingFrequency = m_PollingFrequency, + #endif devices = deviceArray, availableDevices = m_AvailableDevices?.Take(m_AvailableDeviceCount).ToArray(), buffers = m_StateBuffers, @@ -4118,7 +4136,9 @@ internal void RestoreStateWithoutDevices(SerializedState state) updateMask = state.updateMask; scrollDeltaBehavior = state.scrollDeltaBehavior; m_Metrics = state.metrics; + #if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY m_PollingFrequency = state.pollingFrequency; + #endif if (m_Settings != null) Object.DestroyImmediate(m_Settings); diff --git a/Packages/com.unity.inputsystem/InputSystem/InputSystem.cs b/Packages/com.unity.inputsystem/InputSystem/InputSystem.cs index 10b5dfa98e..8a645156ff 100644 --- a/Packages/com.unity.inputsystem/InputSystem/InputSystem.cs +++ b/Packages/com.unity.inputsystem/InputSystem/InputSystem.cs @@ -1364,7 +1364,7 @@ public static event InputDeviceFindControlLayoutDelegate onFindLayoutForDevice /// The unit is Hertz. A value of 120, for example, means that devices are sampled 120 times /// per second. /// - /// The default polling frequency is 60 Hz. + /// The default polling frequency is at least 60 Hz or what is suitable for the target device. /// /// For devices that are polled, the frequency setting will directly translate to changes in the /// patterns. At 60 Hz, for example, timestamps for a specific, diff --git a/Packages/com.unity.inputsystem/InputSystem/NativeInputRuntime.cs b/Packages/com.unity.inputsystem/InputSystem/NativeInputRuntime.cs index f82d5fa8b3..bece519094 100644 --- a/Packages/com.unity.inputsystem/InputSystem/NativeInputRuntime.cs +++ b/Packages/com.unity.inputsystem/InputSystem/NativeInputRuntime.cs @@ -211,10 +211,16 @@ public Action onPlayerFocusChanged public float pollingFrequency { + #if UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY + get => NativeInputSystem.GetPollingFrequency(); + #else get => m_PollingFrequency; + #endif set { + #if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY m_PollingFrequency = value; + #endif NativeInputSystem.SetPollingFrequency(value); } } @@ -246,7 +252,12 @@ public bool runInBackground #if UNITY_EDITOR private Action m_PlayerLoopInitialization; #endif + #if !UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY + // From Unity 6000.3.0a2 (TODO Update comment and manifest before landing PR) this is handled by module + // and initial value is suggested by the platform based on its supported device set. + // In older version this is stored here and package override module/platform. private float m_PollingFrequency = 60.0f; + #endif private bool m_DidCallOnShutdown = false; private void OnShutdown() { diff --git a/Packages/com.unity.inputsystem/InputSystem/Unity.InputSystem.asmdef b/Packages/com.unity.inputsystem/InputSystem/Unity.InputSystem.asmdef index cbf773a654..d7cecb6144 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Unity.InputSystem.asmdef +++ b/Packages/com.unity.inputsystem/InputSystem/Unity.InputSystem.asmdef @@ -111,6 +111,11 @@ "name": "com.unity.modules.unityanalytics", "expression": "1", "define": "UNITY_INPUT_SYSTEM_ENABLE_ANALYTICS" + }, + { + "name": "Unity", + "expression": "6000.3.0a1", + "define": "UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY" } ], "noEngineReferences": false From 2473d865576083cd64e815cde103cd62480c81c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=CC=8Akan=20Sidenvall?= Date: Thu, 3 Jul 2025 14:29:01 +0200 Subject: [PATCH 2/4] Fixed test that is slightly different with this change. --- Assets/Tests/InputSystem/CoreTests_Devices.cs | 11 +++++++++++ .../Tests/InputSystem/Unity.InputSystem.Tests.asmdef | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/Assets/Tests/InputSystem/CoreTests_Devices.cs b/Assets/Tests/InputSystem/CoreTests_Devices.cs index 03fe00c679..070b1c7ce9 100644 --- a/Assets/Tests/InputSystem/CoreTests_Devices.cs +++ b/Assets/Tests/InputSystem/CoreTests_Devices.cs @@ -4394,6 +4394,15 @@ public void Devices_CanSetPollingFrequency() Assert.That(InputSystem.pollingFrequency, Is.EqualTo(120).Within(0.000001)); } + #if UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY + [Test] + [Category("Devices")] + public void Devices_PollingFrequencyIsAtLeast60HzByDefault() + { + Assert.That(InputSystem.pollingFrequency, Is.GreaterThanOrEqualTo(60)); + } + + #else [Test] [Category("Devices")] public void Devices_PollingFrequencyIs60HzByDefault() @@ -4403,6 +4412,8 @@ public void Devices_PollingFrequencyIs60HzByDefault() Assert.That(runtime.pollingFrequency, Is.EqualTo(60).Within(0.000001)); } + #endif + [Test] [Category("Devices")] public unsafe void Devices_CanInterceptAndHandleDeviceCommands() diff --git a/Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef b/Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef index 37981b1ea9..1914d5f214 100644 --- a/Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef +++ b/Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef @@ -65,6 +65,11 @@ "name": "Unity", "expression": "6000.0.15", "define": "UNITY_INPUT_SYSTEM_SENDPOINTERHOVERTOPARENT" + }, + { + "name": "Unity", + "expression": "6000.3.0a1", + "define": "UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY" } ], "noEngineReferences": false From 0676363621b50b5b1a211d59d18e66582cb1cde4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=CC=8Akan=20Sidenvall?= Date: Thu, 3 Jul 2025 17:59:30 +0200 Subject: [PATCH 3/4] Updated asmdef to match next version since otherwise CI would fail. --- Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef | 2 +- .../com.unity.inputsystem/InputSystem/Unity.InputSystem.asmdef | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef b/Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef index 1914d5f214..17d8925f68 100644 --- a/Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef +++ b/Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef @@ -68,7 +68,7 @@ }, { "name": "Unity", - "expression": "6000.3.0a1", + "expression": "6000.3.0a2", "define": "UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY" } ], diff --git a/Packages/com.unity.inputsystem/InputSystem/Unity.InputSystem.asmdef b/Packages/com.unity.inputsystem/InputSystem/Unity.InputSystem.asmdef index d7cecb6144..ac01f0e6e6 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Unity.InputSystem.asmdef +++ b/Packages/com.unity.inputsystem/InputSystem/Unity.InputSystem.asmdef @@ -114,7 +114,7 @@ }, { "name": "Unity", - "expression": "6000.3.0a1", + "expression": "6000.3.0a2", "define": "UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY" } ], From b8e17c9171ba624ca7ce5f1274892f251533cbf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=CC=8Akan=20Sidenvall?= Date: Fri, 4 Jul 2025 11:42:13 +0200 Subject: [PATCH 4/4] Trunk already at a2 so bumped version in asmdef to a3 since a2 do not contain required changes. Note that to test one has to weak this asmdef. --- Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef | 2 +- .../com.unity.inputsystem/InputSystem/Unity.InputSystem.asmdef | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef b/Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef index 17d8925f68..01abd0ac01 100644 --- a/Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef +++ b/Assets/Tests/InputSystem/Unity.InputSystem.Tests.asmdef @@ -68,7 +68,7 @@ }, { "name": "Unity", - "expression": "6000.3.0a2", + "expression": "6000.3.0a3", "define": "UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY" } ], diff --git a/Packages/com.unity.inputsystem/InputSystem/Unity.InputSystem.asmdef b/Packages/com.unity.inputsystem/InputSystem/Unity.InputSystem.asmdef index ac01f0e6e6..f57a96d465 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Unity.InputSystem.asmdef +++ b/Packages/com.unity.inputsystem/InputSystem/Unity.InputSystem.asmdef @@ -114,7 +114,7 @@ }, { "name": "Unity", - "expression": "6000.3.0a2", + "expression": "6000.3.0a3", "define": "UNITY_INPUT_SYSTEM_PLATFORM_POLLING_FREQUENCY" } ],