/// <summary> /// Adds a set of callbacks for a specified input action. These callbacks also exist on <see cref="InputAction"/>, however using this API allows other input users to block calls by using <see cref="ConsumeControl"/>. /// </summary> /// <param name="user">The functionality user.</param> /// <param name="action">The input action to listen for changes on.</param> /// <param name="onStarted">A call when the input action leaves it's default state. See <see cref="InputAction.started"/></param> /// <param name="onPerformed">A call when the input action passes a specified threshold and is 'triggered'. See <see cref="InputAction.performed"/></param> /// <param name="onCanceled">A call when the input action returns to it's default state. See <see cref="InputAction.canceled"/></param> public static void AddActionListeners(this IUsesInputActions user, InputAction action, ActionEventCallback onStarted = null, ActionEventCallback onPerformed = null, ActionEventCallback onCanceled = null) { user.provider.AddActionListeners(user, action, onStarted, onPerformed, onCanceled); }
void IProvidesInputActions.AddActionListeners(IUsesInputActions user, InputAction action, ActionEventCallback onStart, ActionEventCallback onPerformed, ActionEventCallback onCancel) { if (!m_ActionListeners.TryGetValue(action, out var listeners)) { listeners = new ActionListenerCollection(); listeners.user = user; m_ActionListeners.Add(action, listeners); action.started += OnActionStartedEvent; action.performed += OnActionPerformedEvent; action.canceled += OnActionCanceledEvent; } if (user != listeners.user) { Debug.LogError("Cannot add action listeners because another user is already listening to this action."); return; } if (onStart != null) { listeners.startedCallbacks.Add(onStart); } if (onPerformed != null) { listeners.performedCallbacks.Add(onPerformed); } if (onCancel != null) { listeners.cancelledCallbacks.Add(onCancel); } }