Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

New Input system not serialized

Discussion in 'Input System' started by alea360, Apr 24, 2019.

  1. alea360

    alea360

    Joined:
    Mar 26, 2018
    Posts:
    6
    hello, I start to migrate to unity 2019.1 with new input system 0.2.8 but I can't use New Input System because I can't see it in my script when I use public or serialized... Something change since 2018.3 and new input system 0.2.1? Thank you for your answer.
     
    Haxel0rd, foshe_5 and davidsweeneyms like this.
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    I assume you're generating a wrapper for an .inputactions asset using the "Generate C# Wrapper" checkbox and use the generated type on the serialized field, right?

    In the latest package, there was a change where the generated wrappers no longer require the asset to be dragged separately onto the field. Instead, the generated wrapper is self-contained and does not need to be serialized.

    Code (CSharp):
    1. // Before.
    2. [SerializeField]
    3. private MyControls m_Controls;
    4.  
    5. // After.
    6. private MyControls m_Controls;
    7. void Awake()
    8. {
    9.     m_Controls = new MyControls();
    10. }
     
  3. alea360

    alea360

    Joined:
    Mar 26, 2018
    Posts:
    6
    thank you, I will test now ;)
     
  4. galactichyperstar

    galactichyperstar

    Joined:
    Jun 4, 2018
    Posts:
    11
    I tried this, but the events do not trigger?
    Code (CSharp):
    1. private void Awake() {
    2.         playerControls = new PlayerControls();
    3.         playerControls.Movement.Vertical.performed += Vertical_performed;
    4.         playerControls.Movement.Vertical.cancelled += Vertical_cancelled;
    5.         playerControls.Movement.Horizontal.performed += Horizontal_performed;
    6.         playerControls.Movement.Horizontal.cancelled += Horizontal_cancelled;
    7.     }
    8.     private void OnEnable() {
    9.         playerControls.Enable();
    10.     }
     
  5. galactichyperstar

    galactichyperstar

    Joined:
    Jun 4, 2018
    Posts:
    11
    OK, NEVERMIND. I restarted Unity and it just somehow works now? Hooray! I guess.
     
  6. kartikey0303

    kartikey0303

    Joined:
    Jul 16, 2017
    Posts:
    2
    Not working for me. This is my code:
    Code (CSharp):
    1. void Awake() {
    2.         controls = new InputMaster();
    3.         controls.Player.Movement.performed += ctx => Move(ctx.ReadValue<Vector2>());
    4.         controls.Player.Movement.cancelled += ctx => Move(Vector2.zero);
    5.     }
    6.  
    7.     private void Move(Vector2 _axis)
    8.     {
    9.         Debug.Log(_axis);
    10.     }
    11.     void OnEnable() {
    12.         controls.Player.Enable();
    13.     }
    14.     void OnDisable() {
    15.         controls.Player.Disable();
    16.     }
    17.  
    Using Input System 0.2.8 with Unity 2019.1.0f2

    UPDATE: Also after updating to the new Input System On Screen Buttons are also not working
     
    Last edited: May 2, 2019
  7. kartikey0303

    kartikey0303

    Joined:
    Jul 16, 2017
    Posts:
    2
    I selected Both in the Input Player Settings. The UI seems to work now. I also added OnScreenButton Script to one of the button to trigger keyboard left arrow key. That surprisingly worked, but hardware keyboard is still not working.
     
  8. winninger

    winninger

    Joined:
    Mar 6, 2017
    Posts:
    1
    It works !!


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Experimental.Input;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     private Controls controls;
    9.     [SerializeField] int counter;
    10.  
    11.     private void Awake()
    12.     {
    13.         controls = new Controls();
    14.         // one Axis Keys w and s (map = Player)
    15.         // unity 2019.1.2
    16.         controls.Player.Movement.performed += ctx => Move();
    17.     }
    18.  
    19.     void Start()
    20.     {
    21.         counter = 2;
    22.         Debug.Log(controls);
    23.     }
    24.  
    25.     void OnEnable()
    26.     {
    27.         controls.Player.Movement.Enable();
    28.     }
    29.     void OnDisable()
    30.     {
    31.         controls.Player.Movement.Disable();
    32.     }
    33.  
    34.     private void Move()
    35.     {
    36.         Debug.Log("Move");
    37.         Debug.Log(counter);
    38.     }
    39. }
    40.  
     
  9. Beto-Caldas

    Beto-Caldas

    Joined:
    May 27, 2013
    Posts:
    8
    This is how it worked for me

    upload_2019-6-9_3-29-38.png

    upload_2019-6-9_3-30-18.png

    upload_2019-6-9_3-30-59.png

    upload_2019-6-9_3-31-31.png
     
    andrew-lukasik likes this.
  10. andrew-lukasik

    andrew-lukasik

    Joined:
    Jan 31, 2013
    Posts:
    249
    Thanks to @Beto-Caldas' comment I noticed that if you debug controls.map.enabled or any controls.map.action.enabled you will discover that it's disabled on start. No wonder it didn't work. So this seems to be the core issue and whole controls must be enabled/disabled:
    Code (CSharp):
    1. void OnEnable () => controls.Enable();
    2. void OnDisable () => controls.Disable();
    Or single map/action individually:
    Code (CSharp):
    1. void OnEnable () => controls.map.action.Enable();
    2. void OnDisable () => controls.map.action.Disable();
    This is very useful when you think about it. Definitely a great feature and not a bug. Just needs to be communicated more clearly.

    Also to disable legacy system completely - EVERY StandaloneInputModule component (usually next to EventSystem) needs to be replaced with equivalent new InputSystemUIInputModule. Otherwise UI events wont work.
     
    Last edited: Jun 29, 2019
  11. kubajs

    kubajs

    Joined:
    Apr 18, 2015
    Posts:
    58
    Isn't best practice to unregister the event like this in onDisable?

    Code (CSharp):
    1. public class Player : MonoBehaviour
    2. {
    3.     private InputMaster _controls;
    4.  
    5.     private void Awake()
    6.     {
    7.         _controls = new InputMaster();
    8.     }
    9.  
    10.     private void OnEnable()
    11.     {
    12.         _controls.Enable();
    13.         _controls.Player.Shoot.performed += _ => Shoot();
    14.         _controls.Player.Movement.performed += ctx => Move(ctx.ReadValue<Vector2>());
    15.     }
    16.  
    17.     private void OnDisable()
    18.     {
    19.         _controls.Disable();
    20.         _controls.Player.Shoot.performed -= _ => Shoot();
    21.         _controls.Player.Movement.performed -= ctx => Move(ctx.ReadValue<Vector2>());
    22.     }
    23.  
    24.     private void Shoot()
    25.     {
    26.         print("Player shot his leg.");
    27.     }
    28.  
    29.     private void Move(Vector2 direction)
    30.     {
    31.         print("Player moved " + direction);
    32.     }
    33. }
     
  12. Haxel0rd

    Haxel0rd

    Joined:
    May 20, 2021
    Posts:
    53
    This answer was a huge time saviour, already had several tabs open about this problem.
    Thank you very much!

    Cheers