// Update is called once per frame void Update() { if (Input.GetButtonUp(ButtonName)) { OnButtonPress.SafeInvoke(); } if (Input.GetButtonDown(ButtonName)) { OnButtonDown.SafeInvoke(); } if (Input.GetButton(ButtonName)) { OnButtonHeld.SafeInvoke(); } }
protected override void UpdateState() { if (timeSinceStateChange == 0 && state == State.ButtonPressing) { buttonHeld = true; } switch (state) { case State.ButtonUnpressed: break; case State.ButtonPressing: if (timeSinceStateChange > timeToPressButton) { state = State.ButtonPressed; } break; case State.ButtonPressed: if (unpressAfterPress && timeSinceStateChange > timeBetweenPressEndDepressStart || !buttonHeld) { state = State.ButtonUnpressing; } break; case State.ButtonUnpressing: if (timeSinceStateChange > timeToUnpressButton) { state = State.ButtonUnpressed; } break; default: throw new ArgumentOutOfRangeException(); } if (buttonHeld) { OnButtonHeld?.Invoke(this); } }
public static void RegisterButtonHeldCallback(InputKey button, OnButtonHeld callback) { var key = GetKeyString(button); instance.ButtonHeldCallbacks[key] += callback; }