Unity New Input System未显示序列化字段

时间:2019-06-02 01:44:50

标签: c# unity3d input

我正在使用Unity 2019.1.4f1,刚刚从软件包管理器中获得了新的Unity输入系统。我无法在要使用它的单行为中看到用于链接生成的C#类的序列化字段。

我已经公开并将[SerializeField]属性添加到实例,但是它不会在检查器中显示该字段。 因此,我尝试在Monobehavior内部以编程方式创建实例,但无法正常工作。我的SelectDestination方法没有被调用

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");
    }
}

在按下W,左键单击或右键单击时,应记录“ BLA”

如果要使用此功能,Unity会从我的播放器输入导入设置中生成以下代码:

// 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);
    }
}

1 个答案:

答案 0 :(得分:0)

以编程方式创建实例的方式起作用。

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

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

唯一的问题是未激活输入后端。重新启动Unity后,会弹出一条消息以解决此问题,此后它仍然起作用。导入新的输入系统时,也会更改此后端选项,但我设法以某种方式单击了未更改