Skip to content

Commit 3aeaf38

Browse files
authored
NEW: Support StepCounter on iOS (#1253).
1 parent b398919 commit 3aeaf38

23 files changed

+673
-7
lines changed

Assets/Tests/InputSystem/APIVerificationTests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using UnityEngine.InputSystem.DualShock;
1616
using UnityEngine.InputSystem.Editor;
1717
using UnityEngine;
18+
using UnityEngine.InputSystem.iOS.LowLevel;
1819
using UnityEngine.InputSystem.Utilities;
1920
using Object = System.Object;
2021
using TypeAttributes = Mono.Cecil.TypeAttributes;
@@ -424,7 +425,12 @@ private bool IgnoreTypeWithoutPreserveAttribute(Type type)
424425
if (type == typeof(FastKeyboard)
425426
|| type == typeof(FastMouse)
426427
|| type == typeof(FastTouchscreen)
427-
|| type == typeof(FastDualShock4GamepadHID))
428+
|| type == typeof(FastDualShock4GamepadHID)
429+
#if UNITY_EDITOR || UNITY_IOS || UNITY_TVOS
430+
// iOS Step Counter is created from C# code
431+
|| type == typeof(iOSStepCounter)
432+
#endif
433+
)
428434
return true;
429435

430436
return false;
@@ -447,7 +453,7 @@ public void API_DoesNotHaveUndocumentedPublicTypes()
447453
{
448454
var docsFolder = GenerateDocsDirectory();
449455
var undocumentedTypes = GetInputSystemPublicTypes().Where(type => !IgnoreTypeForDocs(type) && string.IsNullOrEmpty(TypeSummary(type, docsFolder)));
450-
Assert.That(undocumentedTypes, Is.Empty, $"Got {undocumentedTypes.Count()} undocumented types.");
456+
Assert.That(undocumentedTypes, Is.Empty, $"Got {undocumentedTypes.Count()} undocumented types, the docs are generated in {docsFolder}");
451457
}
452458

453459
[Test]

Assets/Tests/InputSystem/Plugins/iOSTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
using UnityEngine.InputSystem.Layouts;
88
using UnityEngine.InputSystem.iOS;
99
using UnityEngine.InputSystem.iOS.LowLevel;
10+
using UnityEngine.InputSystem.LowLevel;
1011
using UnityEngine.InputSystem.Processors;
1112
using UnityEngine.InputSystem.XInput;
13+
using UnityEngine.TestTools;
14+
using UnityEngine.TestTools.Utils;
1215

1316
internal class iOSTests : InputTestFixture
1417
{
@@ -79,5 +82,20 @@ public void Devices_SupportsiOSSensors(string deviceClass, Type sensorType)
7982
Assert.That(InputSystem.devices[0].description.interfaceName, Is.EqualTo("iOS"));
8083
Assert.That(InputSystem.devices[0].description.deviceClass, Is.EqualTo(deviceClass));
8184
}
85+
86+
[Test]
87+
[Category("Devices")]
88+
public void Devices_SupportsiOSStepCounter()
89+
{
90+
var device = InputSystem.AddDevice<iOSStepCounter>();
91+
LogAssert.Expect(LogType.Error, "Please enable Motion Usage in Input Settings before using Step Counter.");
92+
InputSystem.EnableDevice(device);
93+
94+
InputSystem.settings.iOS.motionUsage.enabled = true;
95+
InputSystem.EnableDevice(device);
96+
InputSystem.QueueStateEvent(device, new iOSStepCounterState(){stepCounter = 5});
97+
InputSystem.Update();
98+
Assert.That(device.stepCounter.ReadValue(), Is.EqualTo(5));
99+
}
82100
}
83101
#endif // UNITY_EDITOR || UNITY_ANDROID

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ however, it has to be formatted properly to pass verification tests.
7676
Debug.Log($"Button {button} was pressed");
7777
}
7878
```
79+
- Added support for Step Counter sensors for iOS.
80+
* You need to enable **Motion Usage** under Input System settings before using the sensor. You can also manually add **Privacy - Motion Usage Description** to your application's Info.plist file.
7981

8082
## [1.1.0-preview.2] - 2020-10-23
8183

-125 KB
Loading
6.12 KB
Loading

Packages/com.unity.inputsystem/Documentation~/Sensors.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Sensor support
22

3+
* [Sampling Frequency](#sampling-frequency)
4+
* [Accelerometer](#accelerometer)
5+
* [Gyroscope](#gyroscope)
6+
* [GravitySensor](#gravitysensor)
7+
* [AttitudeSensor](#attitudesensor)
8+
* [LinearAccelerationSensor](#linearaccelerationsensor)
9+
* [MagneticFieldSensor](#magneticfieldsensor)
10+
* [LightSensor](#lightsensor)
11+
* [PressureSensor](#pressuresensor)
12+
* [ProximitySensor](#proximitysensor)
13+
* [HumiditySensor](#humiditysensor)
14+
* [AmbientTemperatureSensor](#ambienttemperaturesensor)
15+
* [StepCounter](#stepcounter)
16+
317
Sensors are [`InputDevices`](Devices.md) that measure environmental characteristics of the device that the content is running on. Unity currently supports sensors on iOS and Android. Android supports a wider range of sensors than iOS.
418

519
Unlike other devices, sensors are disabled by default. To enable a sensor, call [`InputSystem.EnableDevice()`](../api/UnityEngine.InputSystem.InputSystem.html#UnityEngine_InputSystem_InputSystem_EnableDevice_UnityEngine_InputSystem_InputDevice_)).
@@ -36,7 +50,7 @@ Each sensor Device implements a single Control which represents the data read by
3650
|[`ProximitySensor`](#proximitysensor)|Yes|No|[`distance`](../api/UnityEngine.InputSystem.ProximitySensor.html#UnityEngine_InputSystem_ProximitySensor_distance)|[`AxisControl`](../api/UnityEngine.InputSystem.Controls.AxisControl.html)|
3751
|[`HumiditySensor`](#humiditysensor)|Yes|No|[`relativeHumidity`](../api/UnityEngine.InputSystem.HumiditySensor.html#UnityEngine_InputSystem_HumiditySensor_relativeHumidity)|[`AxisControl`](../api/UnityEngine.InputSystem.Controls.AxisControl.html)|
3852
|[`AmbientTemperatureSensor`](#ambienttemperaturesensor)|Yes|No|[`ambientTemperature`](../api/UnityEngine.InputSystem.AmbientTemperatureSensor.html#UnityEngine_InputSystem_AmbientTemperatureSensor_ambientTemperature)|[`AxisControl`](../api/UnityEngine.InputSystem.Controls.AxisControl.html)|
39-
|[`StepCounter`](#stepcounter)|Yes|No|[`stepCounter`](../api/UnityEngine.InputSystem.StepCounter.html#UnityEngine_InputSystem_StepCounter_stepCounter)|[`IntegerControl`](../api/UnityEngine.InputSystem.Controls.IntegerControl.html)|
53+
|[`StepCounter`](#stepcounter)|Yes|Yes|[`stepCounter`](../api/UnityEngine.InputSystem.StepCounter.html#UnityEngine_InputSystem_StepCounter_stepCounter)|[`IntegerControl`](../api/UnityEngine.InputSystem.Controls.IntegerControl.html)|
4054

4155
## Sampling frequency
4256

@@ -97,3 +111,5 @@ This Input Device represents the ambient air temperature measured by the device
97111
## <a name="stepcounter"></a>[`StepCounter`](../api/UnityEngine.InputSystem.StepCounter.html)
98112

99113
This Input Device represents the user's footstep count as measured by the device which is running the content.
114+
115+
>NOTE: To access the pedometer on iOS/tvOS devices, you need to enable the [__Motion Usage__ setting](Settings.md#iostvos) in the [Input Settings](Settings.md).

Packages/com.unity.inputsystem/Documentation~/Settings.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,12 @@ To force the Editor to add all locally available Devices, even if they're not in
9898
![Add Devices Not Listed In Supported Devices](Images/AddDevicesNotListedInSupportedDevices.png)
9999

100100
This setting is stored as a user setting (that is, other users who open the same Project can't see the setting).
101+
102+
## Platform-specific settings
103+
104+
### iOS/tvOS
105+
106+
![iOSSettings](Images/iOSSettings.png)
107+
108+
* __Motion Usage__<br>
109+
Governs access to the [pedometer](Sensors.md# on the device. If enabled, the __Description__ string supplied in the settings will be added to the application's Info.plist

Packages/com.unity.inputsystem/InputSystem/Devices/InputDevice.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,17 +522,23 @@ protected virtual void OnRemoved()
522522
public unsafe long ExecuteCommand<TCommand>(ref TCommand command)
523523
where TCommand : struct, IInputDeviceCommandInfo
524524
{
525+
var commandPtr = (InputDeviceCommand*)UnsafeUtility.AddressOf(ref command);
525526
// Give callbacks first shot.
526527
var manager = InputSystem.s_Manager;
527528
var callbacks = manager.m_DeviceCommandCallbacks;
528529
for (var i = 0; i < callbacks.length; ++i)
529530
{
530-
var result = callbacks[i](this, (InputDeviceCommand*)UnsafeUtility.AddressOf(ref command));
531+
var result = callbacks[i](this, commandPtr);
531532
if (result.HasValue)
532533
return result.Value;
533534
}
534535

535-
return InputRuntime.s_Instance.DeviceCommand(deviceId, ref command);
536+
return ExecuteCommand((InputDeviceCommand*)UnsafeUtility.AddressOf(ref command));
537+
}
538+
539+
protected virtual unsafe long ExecuteCommand(InputDeviceCommand* commandPtr)
540+
{
541+
return InputRuntime.s_Instance.DeviceCommand(deviceId, commandPtr);
536542
}
537543

538544
[Flags]

Packages/com.unity.inputsystem/InputSystem/Devices/Sensor.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,9 @@ protected override void FinishSetup()
623623
/// <summary>
624624
/// Input device representing the foot steps taken by the user as measured by the device playing the content.
625625
/// </summary>
626+
/// <remarks>
627+
/// On iOS, access to the step counter must be enabled via <see cref="InputSettings.iOSSettings.motionUsage"/>.
628+
/// </remarks>
626629
[InputControlLayout(displayName = "Step Counter")]
627630
[Scripting.Preserve]
628631
public class StepCounter : Sensor

Packages/com.unity.inputsystem/InputSystem/Editor/Settings/InputSettingsProvider.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,20 @@ public override void OnGUI(string searchContext)
126126

127127
m_SupportedDevices.DoLayoutList();
128128

129+
EditorGUILayout.LabelField("iOS", EditorStyles.boldLabel);
130+
EditorGUILayout.Space();
131+
m_iOSProvider.OnGUI();
132+
129133
if (EditorGUI.EndChangeCheck())
130134
Apply();
131135
}
132136
}
133137

138+
private static void ShowPlatformSettings()
139+
{
140+
// Would be nice to get BuildTargetDiscovery.GetBuildTargetInfoList since that contains information about icons etc
141+
}
142+
134143
private static void CreateNewSettingsAsset(string relativePath)
135144
{
136145
// Create settings file.
@@ -301,6 +310,8 @@ private void InitializeWithCurrentSettings()
301310
EditorGUI.LabelField(rect, m_Settings.supportedDevices[index]);
302311
}
303312
};
313+
314+
m_iOSProvider = new InputSettingsiOSProvider(m_SettingsObject);
304315
}
305316

306317
private void Apply()
@@ -368,6 +379,8 @@ private static string[] FindInputSettingsInProject()
368379
GUIContent m_TapRadiusContent;
369380
GUIContent m_MultiTapDelayTimeContent;
370381

382+
[NonSerialized] private InputSettingsiOSProvider m_iOSProvider;
383+
371384
private static InputSettingsProvider s_Instance;
372385

373386
internal static void ForceReload()

0 commit comments

Comments
 (0)