Skip to content

Instantly share code, notes, and snippets.

Created July 29, 2021 22:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Unity Input modifier that accepts a modifier + a button but the modifier must be pressed before button
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.Utilities;
using UnityEngine.Scripting;
using UnityEngine;
using UnityEngine.InputSystem;
namespace AshInput {
public class ButtonWithOneModifierOrdered : InputBindingComposite<float> {
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] // BeforeSceneLoad is needed or it will fail in builds initializing too late.
static void Init() {
[InputControl(layout = "Button")] public int modifier;
[InputControl(layout = "Button")] public int button;
enum LockedState : byte { Waiting, LockedPressed, LockedUnpressed };
LockedState lockedState = LockedState.Waiting;
public override float ReadValue(ref InputBindingCompositeContext context) {
bool newModifierState = context.ReadValueAsButton(modifier);
bool newButtonState = context.ReadValueAsButton(button);
if (lockedState == LockedState.Waiting) {
if (newButtonState) lockedState = newModifierState ? LockedState.LockedPressed : LockedState.LockedUnpressed;
else {
if (!newButtonState) lockedState = LockedState.Waiting;
return lockedState == LockedState.LockedPressed ? 1 : 0;
public override float EvaluateMagnitude(ref InputBindingCompositeContext context) {
return ReadValue(ref context);
Copy link

Just drop the file somewhere in the project and it will appear in the Controls Manager

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment