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.
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.
// Before.
[SerializeField]
private MyControls m_Controls;
// After.
private MyControls m_Controls;
void Awake()
{
m_Controls = new MyControls();
}
thank you, I will test now
I tried this, but the events do not trigger?
private void Awake() {
playerControls = new PlayerControls();
playerControls.Movement.Vertical.performed += Vertical_performed;
playerControls.Movement.Vertical.cancelled += Vertical_cancelled;
playerControls.Movement.Horizontal.performed += Horizontal_performed;
playerControls.Movement.Horizontal.cancelled += Horizontal_cancelled;
}
private void OnEnable() {
playerControls.Enable();
}
OK, NEVERMIND. I restarted Unity and it just somehow works now? Hooray! I guess.
Not working for me. This is my code:
void Awake() {
controls = new InputMaster();
controls.Player.Movement.performed += ctx => Move(ctx.ReadValue<Vector2>());
controls.Player.Movement.cancelled += ctx => Move(Vector2.zero);
}
private void Move(Vector2 _axis)
{
Debug.Log(_axis);
}
void OnEnable() {
controls.Player.Enable();
}
void OnDisable() {
controls.Player.Disable();
}
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
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.
It works !!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.Input;
public class Player : MonoBehaviour
{
private Controls controls;
[SerializeField] int counter;
private void Awake()
{
controls = new Controls();
// one Axis Keys w and s (map = Player)
// unity 2019.1.2
controls.Player.Movement.performed += ctx => Move();
}
void Start()
{
counter = 2;
Debug.Log(controls);
}
void OnEnable()
{
controls.Player.Movement.Enable();
}
void OnDisable()
{
controls.Player.Movement.Disable();
}
private void Move()
{
Debug.Log("Move");
Debug.Log(counter);
}
}
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:
void OnEnable () => controls.Enable();
void OnDisable () => controls.Disable();
Or single map/action individually:
void OnEnable () => controls.map.action.Enable();
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.
Isn’t best practice to unregister the event like this in onDisable?
public class Player : MonoBehaviour
{
private InputMaster _controls;
private void Awake()
{
_controls = new InputMaster();
}
private void OnEnable()
{
_controls.Enable();
_controls.Player.Shoot.performed += _ => Shoot();
_controls.Player.Movement.performed += ctx => Move(ctx.ReadValue<Vector2>());
}
private void OnDisable()
{
_controls.Disable();
_controls.Player.Shoot.performed -= _ => Shoot();
_controls.Player.Movement.performed -= ctx => Move(ctx.ReadValue<Vector2>());
}
private void Shoot()
{
print("Player shot his leg.");
}
private void Move(Vector2 direction)
{
print("Player moved " + direction);
}
}
This answer was a huge time saviour, already had several tabs open about this problem.
Thank you very much!
Cheers