Custom input for StandaloneInputModule

I have my own input system and I’m trying to get the StandaloneInputModule to read from it instead from Input.GetAxis etc. I know that in order to do that, I need to inherit from BaseInput: Redirecting to latest version of com.unity.ugui

I’ve already done that but how do I tell the StandaloneInputModule to read from my custom class? Even if I attach my inherited component to the StandaloneInputModule’s gameobject, it still spawns its own BaseInput component and reads from it.

I think you must derive from StandaloneInputModule.
In that custom class you must assign your custom input to the module… probably like this:

void Awake()
{
     base.m_InputOverride = this.gameObject.AddComponent<MyCustomInput>();
}

(I just checked the source code… not sure if it will really work that way)

3 Likes

Perfect! This was exactly what I was looking for!
Thank you!

You can set property StandaloneInputModule.inputOverride for this. I think that should be the preferred way, since, in contrast to m_InputOverride, it is documented in the API.
If, for example, your BaseInput inherited Object is attached to the same GameObject as your StandaloneInputModule you could do the following:

public class CustomBaseInput : BaseInput
{
    protected override void Awake() {
        StandaloneInputModule standaloneInputModule = GetComponent<StandaloneInputModule>();
        if (standaloneInputModule) standaloneInputModule.inputOverride = this;
    }

    public override float GetAxisRaw(string axisName) {
        if (axisName=="Horizontal") {
            // your code here
        } else if (axisName=="Vertical") {
            // your code here
        }
        return 0f;
    }

    public override bool GetButtonDown(string buttonName) {
        if (buttonName=="Submit") {
            // your code here
        } else if (buttonName=="Cancel") {
            // your code here
        }
        return false;
    }

    // more overrides as needed
}
1 Like

This is probably new in Unity. When I wrote my comment, there was no public “inputOverride” property.
Good that Unity exposed it now.