Still no luck, “onAnyButtonPress” is not recognized. I’m on Unity 2021.1.15f1 (Windows) with Input System 1.0.2.
Was added sometime in >=1.1. Recommend upgrading to 1.3.
I am using 1.2.0 and I cannot use your posted code for InputSystem.onAnyButtonPress.
private void Start()
{
InputSystem.onAnyButtonPress.Call(
ctrl =>
{
if (ctrl.device is Gamepad gamepad)
Debug.Log($"Button {ctrl} on {ctrl.device} was pressed");
});
}
I get a bunch of errors:
ArgumentException while executing ‘InputSystem.onEvent’ callbacks
UnityEngine.InputSystem.LowLevel.NativeInputRuntime/<>c__DisplayClass7_0:<set_onUpdate>b__0 (UnityEngineInternal.Input.NativeInputUpdateType,UnityEngineInternal.Input.NativeInputEventBuffer*)
UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate (UnityEngineInternal.Input.NativeInputUpdateType,intptr) (at /Users/bokken/buildslave/unity/build/Modules/Input/Private/Input.cs:120)
ArgumentException: Event must be a StateEvent or DeltaStateEvent but is a IMES instead
Parameter name: eventPtr
UnityEngine.InputSystem.InputControlExtensions.EnumerateControls (UnityEngine.InputSystem.LowLevel.InputEventPtr eventPtr, UnityEngine.InputSystem.InputControlExtensions+Enumerate flags, UnityEngine.InputSystem.InputDevice device, System.Single magnitudeThreshold) (at Library/PackageCache/com.unity.inputsystem@1.2.0/InputSystem/Controls/InputControlExtensions.cs:1014)
UnityEngine.InputSystem.InputControlExtensions.GetFirstButtonPressOrNull (UnityEngine.InputSystem.LowLevel.InputEventPtr eventPtr, System.Single magnitude, System.Boolean buttonControlsOnly) (at Library/PackageCache/com.unity.inputsystem@1.2.0/InputSystem/Controls/InputControlExtensions.cs:1096)
UnityEngine.InputSystem.InputSystem+<>c.<get_onAnyButtonPress>b__79_0 (UnityEngine.InputSystem.LowLevel.InputEventPtr e) (at Library/PackageCache/com.unity.inputsystem@1.2.0/InputSystem/InputSystem.cs:2404)
UnityEngine.InputSystem.LowLevel.SelectObservable2+Select[TSource,TResult].OnNext (TSource evt) (at Library/PackageCache/com.unity.inputsystem@1.2.0/InputSystem/Utilities/Observables/SelectObservable.cs:43) UnityEngine.InputSystem.LowLevel.InputEventListener+ObserverState.<.ctor>b__2_0 (UnityEngine.InputSystem.LowLevel.InputEventPtr eventPtr, UnityEngine.InputSystem.InputDevice device) (at Library/PackageCache/com.unity.inputsystem@1.2.0/InputSystem/Events/InputEventListener.cs:127) UnityEngine.InputSystem.Utilities.DelegateHelpers.InvokeCallbacksSafe[TValue1,TValue2] (UnityEngine.InputSystem.Utilities.CallbackArray1[System.Action`2[TValue1,TValue2]]& callbacks, TValue1 argument1, TValue2 argument2, System.String callbackName, System.Object context) (at Library/PackageCache/com.unity.inputsystem@1.2.0/InputSystem/Utilities/DelegateHelpers.cs:71)
UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr) (at /Users/bokken/buildslave/unity/build/Modules/Input/Private/Input.cs:120)
- This code is also executing when I quit playmode. Am I doing something wrong? Why is it executing in edit mode when I use this on Start()? I don’t have any [ExecuteInEditoMode]
- Am I suppose to subscribe to it then unsubscribe? If so, how?
EDIT: I found this documentation that helps with the code executing in edit mode. However, I still get the errors:
I’m all for trying the cool new stuff, and I am enjoying some of the benefits of the new input system, but I cannot get this to work : all I need is a simple “Press a key to continue” once my main game scene is loaded and I can’t seem to make it work.
Could you provide more context in the code ? The example provided by the user above from the docs just fails because IObservable doesn’t implement the Call method.
Can you put me on the right tracks ?
Sorry, the answer is actually properly provided here : Input System 1.3.0 released: more fixes (post #15) and it cannot be more clear.
It’s also a good and robust way of implementing the “Wait for any key” through code with events, so you don’t clutter your project needlessly. Even though it’s recommended to segregate functionality (so input should really be in its own manager), this is the perfect exception to the rule : you just need to register that one key press event, wait for it, then de-register it.
Clean and efficient.