private static void OnActionTriggered(object obj, InputActionChange change) { if (change != InputActionChange.ActionTriggered) { return; } ////REVIEW: filter for noise here? // Grab control that triggered the action. var action = (InputAction)obj; var control = action.lastTriggerControl; if (control == null) { return; } // See if it's coming from a device not belonging to any user. var device = control.device; if (ArrayHelpers.ContainsReferenceTo(s_AllDevices, s_AllDeviceCount, device)) { // No, it's a device already assigned to a player so do nothing. return; } // Ok, we know it's a device not assigned to a user and it triggered action. The only // thing that remains is to determine whether it was from an action assigned to a user. var userIndex = -1; for (var i = 0; i < s_AllUserCount; ++i) { var actions = s_AllUserData[i].actions; if (actions != null && actions.Contains(action)) { userIndex = i; break; } } if (userIndex != -1) { var user = s_AllUsers[userIndex]; for (var i = 0; i < s_OnUnassignedDeviceUsed.length; ++i) { s_OnUnassignedDeviceUsed[i](user, action, control); } } }
/// <summary> /// Return a list of all currently added devices that are not assigned to any user. /// </summary> /// <returns>A (possibly empty) list of devices that are currently not assigned to a user.</returns> /// <seealso cref="InputSystem.devices"/> /// <seealso cref="InputUser.AssignInputDevice"/> /// <remarks> /// The resulting list uses <see cref="Allocator.Temp"> temporary, unmanaged memory</see>. If not disposed of /// explicitly, the list will automatically be deallocated at the end of the frame and will become unusable. /// </remarks> public static InputControlList <InputDevice> GetUnassignedInputDevices() { var unusedDevices = new InputControlList <InputDevice>(Allocator.Temp); foreach (var device in InputSystem.devices) { // If it's in s_AllDevices, there is *some* user that is using the device. // We don't care which one it is here. if (ArrayHelpers.ContainsReferenceTo(s_AllDevices, s_AllDeviceCount, device)) { continue; } unusedDevices.Add(device); } return(unusedDevices); }