LeCalore
LeCalore

Reputation: 80

Unity New Input System not showing up serialized field

I'm using Unity 2019.1.4f1 and just got the New Unity Input System from the Package Manager. I'm unable to see the serialized field for linking the generated C# class in the monobehaviour that intends to use it.

I've made public and added the [SerializeField] attribute to the instance but it won't show the field in the inspector. Therefore I tried creating the instance programatically inside my Monobehavior but it's not working. My SelectDestination method is not getting called

public class PlayerInputManager : MonoBehaviour
{
    [SerializeField]
    public PlayerInput Input;

    private void Awake()
    {
        Input = new PlayerInput();
        Input.Enable();        
    }

    void OnEnable()
    {
        Input.Player.SelectDestination.performed += ctx => SelectDestination();
        Input.Player.SelectTarget.performed += ctx => SelectDestination();
        Input.Player.W.performed += ctx => SelectDestination();
        Input.Player.Enable();
    }

    void OnDisable()
    {
        Input.Player.SelectDestination.performed -= ctx => SelectDestination();
        Input.Player.SelectTarget.performed -= ctx => SelectDestination();
        Input.Player.W.performed -= ctx => SelectDestination();
        Input.Player.Disable();
    }

    void SelectDestination()
    {
        Debug.Log("BLA");
    }
}

It should log "BLA" when W, Left Click or Right Click is pressed

If this comes to use, the following code is generated by Unity from my Player Input Import Settings:

// GENERATED AUTOMATICALLY FROM 'Assets/Player/Input/PlayerInput.inputactions'

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Utilities;

public class PlayerInput : IInputActionCollection
{
    private InputActionAsset asset;
    public PlayerInput()
    {
        asset = InputActionAsset.FromJson(@"{
    ""name"": ""PlayerInput"",
    ""maps"": [
        {
            ""name"": ""Player"",
            ""id"": ""5bcdf4ef-1e0a-4aa8-b012-545024ebcf4b"",
            ""actions"": [
                {
                    ""name"": ""Select Destination"",
                    ""id"": ""1d0574dd-8a36-47cf-9987-1ee778cc187b"",
                    ""expectedControlLayout"": """",
                    ""continuous"": false,
                    ""passThrough"": false,
                    ""initialStateCheck"": false,
                    ""processors"": """",
                    ""interactions"": """",
                    ""bindings"": []
                },
                {
                    ""name"": ""Select Target"",
                    ""id"": ""a1a64f6a-ab46-42f5-854c-2bcf2c2a462b"",
                    ""expectedControlLayout"": """",
                    ""continuous"": false,
                    ""passThrough"": false,
                    ""initialStateCheck"": false,
                    ""processors"": """",
                    ""interactions"": """",
                    ""bindings"": []
                },
                {
                    ""name"": ""W"",
                    ""id"": ""c0d92aea-cab2-4277-b6a7-89dfa0944f83"",
                    ""expectedControlLayout"": """",
                    ""continuous"": false,
                    ""passThrough"": false,
                    ""initialStateCheck"": false,
                    ""processors"": """",
                    ""interactions"": """",
                    ""bindings"": []
                }
            ],
            ""bindings"": [
                {
                    ""name"": """",
                    ""id"": ""539fd2ab-5081-4f91-b1c6-ed9db7833b61"",
                    ""path"": ""<Mouse>/rightButton"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": ""PC"",
                    ""action"": ""Select Destination"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": false,
                    ""modifiers"": """"
                },
                {
                    ""name"": """",
                    ""id"": ""61673484-a2cd-49e3-8352-aa0e5f791bc5"",
                    ""path"": ""<Mouse>/leftButton"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": ""PC"",
                    ""action"": ""Select Target"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": false,
                    ""modifiers"": """"
                },
                {
                    ""name"": """",
                    ""id"": ""8c4c3c98-65b7-4c0b-80cc-0e25d2a59567"",
                    ""path"": ""<Keyboard>/w"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": "";PC"",
                    ""action"": ""W"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": false,
                    ""modifiers"": """"
                }
            ]
        }
    ],
    ""controlSchemes"": [
        {
            ""name"": ""PC"",
            ""basedOn"": """",
            ""bindingGroup"": ""PC"",
            ""devices"": [
                {
                    ""devicePath"": ""<Keyboard>"",
                    ""isOptional"": false,
                    ""isOR"": false
                },
                {
                    ""devicePath"": ""<Mouse>"",
                    ""isOptional"": false,
                    ""isOR"": false
                }
            ]
        }
    ]
}");
        // Player
        m_Player = asset.GetActionMap("Player");
        m_Player_SelectDestination = m_Player.GetAction("Select Destination");
        m_Player_SelectTarget = m_Player.GetAction("Select Target");
        m_Player_W = m_Player.GetAction("W");
    }

    ~PlayerInput()
    {
        UnityEngine.Object.Destroy(asset);
    }

    public InputBinding? bindingMask
    {
        get => asset.bindingMask;
        set => asset.bindingMask = value;
    }

    public ReadOnlyArray<InputDevice>? devices
    {
        get => asset.devices;
        set => asset.devices = value;
    }

    public ReadOnlyArray<InputControlScheme> controlSchemes
    {
        get => asset.controlSchemes;
    }

    public bool Contains(InputAction action)
    {
        return asset.Contains(action);
    }

    public IEnumerator<InputAction> GetEnumerator()
    {
        return asset.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public void Enable()
    {
        asset.Enable();
    }

    public void Disable()
    {
        asset.Disable();
    }

    // Player
    private InputActionMap m_Player;
    private IPlayerActions m_PlayerActionsCallbackInterface;
    private InputAction m_Player_SelectDestination;
    private InputAction m_Player_SelectTarget;
    private InputAction m_Player_W;
    public struct PlayerActions
    {
        private PlayerInput m_Wrapper;
        public PlayerActions(PlayerInput wrapper) { m_Wrapper = wrapper; }
        public InputAction @SelectDestination { get { return m_Wrapper.m_Player_SelectDestination; } }
        public InputAction @SelectTarget { get { return m_Wrapper.m_Player_SelectTarget; } }
        public InputAction @W { get { return m_Wrapper.m_Player_W; } }
        public InputActionMap Get() { return m_Wrapper.m_Player; }
        public void Enable() { Get().Enable(); }
        public void Disable() { Get().Disable(); }
        public bool enabled { get { return Get().enabled; } }
        public InputActionMap Clone() { return Get().Clone(); }
        public static implicit operator InputActionMap(PlayerActions set) { return set.Get(); }
        public void SetCallbacks(IPlayerActions instance)
        {
            if (m_Wrapper.m_PlayerActionsCallbackInterface != null)
            {
                SelectDestination.started -= m_Wrapper.m_PlayerActionsCallbackInterface.OnSelectDestination;
                SelectDestination.performed -= m_Wrapper.m_PlayerActionsCallbackInterface.OnSelectDestination;
                SelectDestination.canceled -= m_Wrapper.m_PlayerActionsCallbackInterface.OnSelectDestination;
                SelectTarget.started -= m_Wrapper.m_PlayerActionsCallbackInterface.OnSelectTarget;
                SelectTarget.performed -= m_Wrapper.m_PlayerActionsCallbackInterface.OnSelectTarget;
                SelectTarget.canceled -= m_Wrapper.m_PlayerActionsCallbackInterface.OnSelectTarget;
                W.started -= m_Wrapper.m_PlayerActionsCallbackInterface.OnW;
                W.performed -= m_Wrapper.m_PlayerActionsCallbackInterface.OnW;
                W.canceled -= m_Wrapper.m_PlayerActionsCallbackInterface.OnW;
            }
            m_Wrapper.m_PlayerActionsCallbackInterface = instance;
            if (instance != null)
            {
                SelectDestination.started += instance.OnSelectDestination;
                SelectDestination.performed += instance.OnSelectDestination;
                SelectDestination.canceled += instance.OnSelectDestination;
                SelectTarget.started += instance.OnSelectTarget;
                SelectTarget.performed += instance.OnSelectTarget;
                SelectTarget.canceled += instance.OnSelectTarget;
                W.started += instance.OnW;
                W.performed += instance.OnW;
                W.canceled += instance.OnW;
            }
        }
    }
    public PlayerActions @Player
    {
        get
        {
            return new PlayerActions(this);
        }
    }
    private int m_PCSchemeIndex = -1;
    public InputControlScheme PCScheme
    {
        get
        {
            if (m_PCSchemeIndex == -1) m_PCSchemeIndex = asset.GetControlSchemeIndex("PC");
            return asset.controlSchemes[m_PCSchemeIndex];
        }
    }
    public interface IPlayerActions
    {
        void OnSelectDestination(InputAction.CallbackContext context);
        void OnSelectTarget(InputAction.CallbackContext context);
        void OnW(InputAction.CallbackContext context);
    }
}

Upvotes: 1

Views: 3862

Answers (1)

LeCalore
LeCalore

Reputation: 80

The way of creating the instance programatically works.

public class PlayerInputManager : MonoBehaviour
{
    [SerializeField]
    public PlayerInput Input;

    private void Awake()
    {
        Input = new PlayerInput(); <<<<<<<<<<<
        Input.Enable();        
    }
    ...
}

The only problem was that the input backend wasn't activated. Once restarted Unity a message popped addressing this issue, and it worked afterwards. This changing backend option came also when importing the new Input System, but somehow I managed to click not changing it

Upvotes: 1

Related Questions