Search Unity

Custom input for StandaloneInputModule

Discussion in 'UGUI & TextMesh Pro' started by Dreamteck, Apr 19, 2018.

  1. Dreamteck

    Dreamteck

    Joined:
    Feb 12, 2015
    Posts:
    336
    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: https://docs.unity3d.com/ScriptReference/EventSystems.BaseInput.html

    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.
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    I think you must derive from StandaloneInputModule.
    In that custom class you must assign your custom input to the module... probably like this:
    Code (CSharp):
    1. void Awake()
    2. {
    3.      base.m_InputOverride = this.gameObject.AddComponent<MyCustomInput>();
    4. }
    (I just checked the source code... not sure if it will really work that way)
     
    plmx, metroidsnes and Dreamteck like this.
  3. Dreamteck

    Dreamteck

    Joined:
    Feb 12, 2015
    Posts:
    336
    Perfect! This was exactly what I was looking for!
    Thank you!
     
  4. Hnglftz

    Hnglftz

    Joined:
    May 11, 2019
    Posts:
    2
    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:

    Code (CSharp):
    1. public class CustomBaseInput : BaseInput
    2. {
    3.     protected override void Awake() {
    4.         StandaloneInputModule standaloneInputModule = GetComponent<StandaloneInputModule>();
    5.         if (standaloneInputModule) standaloneInputModule.inputOverride = this;
    6.     }
    7.  
    8.     public override float GetAxisRaw(string axisName) {
    9.         if (axisName=="Horizontal") {
    10.             // your code here
    11.         } else if (axisName=="Vertical") {
    12.             // your code here
    13.         }
    14.         return 0f;
    15.     }
    16.  
    17.     public override bool GetButtonDown(string buttonName) {
    18.         if (buttonName=="Submit") {
    19.             // your code here
    20.         } else if (buttonName=="Cancel") {
    21.             // your code here
    22.         }
    23.         return false;
    24.     }
    25.  
    26.     // more overrides as needed
    27. }
     
  5. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    This is probably new in Unity. When I wrote my comment, there was no public "inputOverride" property.
    Good that Unity exposed it now.