Maze / Library /PackageCache /com.unity.inputsystem@1.6.1 /InputSystem /Plugins /iOS /iOSStepCounter.cs
| using System.Runtime.InteropServices; | |
| using AOT; | |
| using Unity.Collections.LowLevel.Unsafe; | |
| using UnityEngine.InputSystem.Layouts; | |
| using UnityEngine.InputSystem.LowLevel; | |
| using UnityEngine.InputSystem.Utilities; | |
| namespace UnityEngine.InputSystem.iOS.LowLevel | |
| { | |
| /// <summary> | |
| /// Describes the access for motion related features. | |
| /// </summary> | |
| /// <remarks>Enum values map values from CoreMotion.framework/Headers/CMAuthorization.h</remarks> | |
| public enum MotionAuthorizationStatus | |
| { | |
| /// <summary> | |
| /// The access status was not yet determined. | |
| /// </summary> | |
| NotDetermined = 0, | |
| /// <summary> | |
| /// Access was denied due system settings. | |
| /// </summary> | |
| Restricted, | |
| /// <summary> | |
| /// Access was denied by the user. | |
| /// </summary> | |
| Denied, | |
| /// <summary> | |
| /// Access was allowed by the user. | |
| /// </summary> | |
| Authorized | |
| } | |
| [] | |
| internal struct iOSStepCounterState : IInputStateTypeInfo | |
| { | |
| public static FourCC kFormat = new FourCC('I', 'S', 'C', 'S'); | |
| public FourCC format => kFormat; | |
| [] | |
| public int stepCounter; | |
| } | |
| /// <summary> | |
| /// Step Counter (also known as pedometer) sensor for iOS. | |
| /// </summary> | |
| /// <remarks> | |
| /// You need to enable Motion Usage in Input System settings (see <see cref="InputSettings.iOSSettings.motionUsage"/>), to be allowed | |
| /// to access the sensor on the user's device. | |
| /// <example> | |
| /// <code> | |
| /// void Start() | |
| /// { | |
| /// InputSystem.EnableDevice(StepCounter.current); | |
| /// } | |
| /// | |
| /// void OnGUI() | |
| /// { | |
| /// GUILayout.Label(StepCounter.current.stepCounter.ReadValue().ToString()); | |
| /// } | |
| /// </code> | |
| /// </example> | |
| /// </remarks> | |
| /// <seealso cref="InputSettings.iOSSettings.motionUsage"/> | |
| [] | |
| public class iOSStepCounter : StepCounter | |
| { | |
| private const int kCommandFailure = -1; | |
| private const int kCommandSuccess = 1; | |
| internal delegate void OnDataReceivedDelegate(int deviceId, int numberOfSteps); | |
| [] | |
| private struct iOSStepCounterCallbacks | |
| { | |
| internal OnDataReceivedDelegate onData; | |
| } | |
| [] | |
| private static extern int _iOSStepCounterEnable(int deviceId, ref iOSStepCounterCallbacks callbacks, int sizeOfCallbacks); | |
| [] | |
| private static extern int _iOSStepCounterDisable(int deviceId); | |
| [] | |
| private static extern int _iOSStepCounterIsEnabled(int deviceId); | |
| [] | |
| private static extern int _iOSStepCounterIsAvailable(); | |
| [] | |
| private static extern int _iOSStepCounterGetAuthorizationStatus(); | |
| [] | |
| private static void OnDataReceived(int deviceId, int numberOfSteps) | |
| { | |
| var stepCounter = (iOSStepCounter)InputSystem.GetDeviceById(deviceId); | |
| InputSystem.QueueStateEvent(stepCounter, new iOSStepCounterState {stepCounter = numberOfSteps}); | |
| } | |
| private bool m_Enabled = false; | |
| protected override unsafe long ExecuteCommand(InputDeviceCommand* commandPtr) | |
| { | |
| var t = commandPtr->type; | |
| if (t == QueryEnabledStateCommand.Type) | |
| { | |
| ((QueryEnabledStateCommand*)commandPtr)->isEnabled = m_Enabled; | |
| ((QueryEnabledStateCommand*)commandPtr)->isEnabled = _iOSStepCounterIsEnabled(deviceId) != 0; | |
| return kCommandSuccess; | |
| } | |
| if (t == EnableDeviceCommand.Type) | |
| { | |
| if (InputSystem.settings.iOS.motionUsage.enabled == false) | |
| { | |
| Debug.LogError("Please enable Motion Usage in Input Settings before using Step Counter."); | |
| return kCommandFailure; | |
| } | |
| m_Enabled = true; | |
| return kCommandSuccess; | |
| var callbacks = new iOSStepCounterCallbacks(); | |
| callbacks.onData = OnDataReceived; | |
| return _iOSStepCounterEnable(deviceId, ref callbacks, Marshal.SizeOf(callbacks)); | |
| } | |
| if (t == DisableDeviceCommand.Type) | |
| { | |
| m_Enabled = false; | |
| return kCommandSuccess; | |
| return _iOSStepCounterDisable(deviceId); | |
| } | |
| if (t == QueryCanRunInBackground.Type) | |
| { | |
| ((QueryCanRunInBackground*)commandPtr)->canRunInBackground = true; | |
| return kCommandSuccess; | |
| } | |
| if (t == RequestResetCommand.Type) | |
| { | |
| m_Enabled = false; | |
| _iOSStepCounterDisable(deviceId); | |
| return kCommandSuccess; | |
| } | |
| return kCommandFailure; | |
| } | |
| /// <summary> | |
| /// Does the phone support the pedometer? | |
| /// </summary> | |
| public static bool IsAvailable() | |
| { | |
| return false; | |
| return _iOSStepCounterIsAvailable() != 0; | |
| } | |
| /// <summary> | |
| /// Indicates whether the app is authorized to gather data for step counter sensor. | |
| /// </summary> | |
| public static MotionAuthorizationStatus AuthorizationStatus | |
| { | |
| get | |
| { | |
| return MotionAuthorizationStatus.NotDetermined; | |
| return (MotionAuthorizationStatus)_iOSStepCounterGetAuthorizationStatus(); | |
| } | |
| } | |
| } | |
| } | |