【问题标题】:Unity Input System automatically generated class gives errorsUnity Input System 自动生成的类给出错误
【发布时间】:2021-06-01 19:29:03
【问题描述】:

我正在制作游戏,并尝试添加游戏手柄控件。 Unity 自动生成一个类供使用,它返回 3 但同样的错误:

Assets\Scripts\Player2\PlayerControls.cs(78,38):错误 CS1061:“InputActionMap”不包含“GetAction”的定义,并且没有可访问的扩展方法“GetAction”接受类型的第一个参数可以找到“InputActionMap”(您是否缺少 using 指令或程序集引用?)

生成类


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

public class Player2ControlsScript : IInputActionCollection
{
    private InputActionAsset asset;
    public Player2ControlsScript()
    {
        asset = InputActionAsset.FromJson(@"{
    ""name"": ""PlayerControls"",
    ""maps"": [
        {
            ""name"": ""Gameplay"",
            ""id"": ""bb9a690a-3e84-4125-97d0-abc9eadb168c"",
            ""actions"": [
                {
                    ""name"": ""Jump"",
                    ""id"": ""8e38deca-52f6-4adc-ae48-bf600aca39a1"",
                    ""expectedControlLayout"": """",
                    ""continuous"": false,
                    ""passThrough"": false,
                    ""initialStateCheck"": false,
                    ""processors"": """",
                    ""interactions"": """",
                    ""bindings"": []
                },
                {
                    ""name"": ""Move"",
                    ""id"": ""3abf2144-1e34-4204-a7d7-7c42eafa79f3"",
                    ""expectedControlLayout"": """",
                    ""continuous"": false,
                    ""passThrough"": false,
                    ""initialStateCheck"": false,
                    ""processors"": """",
                    ""interactions"": """",
                    ""bindings"": []
                }
            ],
            ""bindings"": [
                {
                    ""name"": """",
                    ""id"": ""8947f9d2-2588-4297-a2f1-e7b4b7efa212"",
                    ""path"": ""<Gamepad>/buttonSouth"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Jump"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": false,
                    ""modifiers"": """"
                },
                {
                    ""name"": """",
                    ""id"": ""2d52334c-dbb5-4a14-acce-3a42c0f5208a"",
                    ""path"": ""<Gamepad>/leftStick"",
                    ""interactions"": """",
                    ""processors"": """",
                    ""groups"": """",
                    ""action"": ""Move"",
                    ""isComposite"": false,
                    ""isPartOfComposite"": false,
                    ""modifiers"": """"
                }
            ]
        }
    ],
    ""controlSchemes"": []
}");
        // Gameplay
        m_Gameplay = asset.GetActionMap("Gameplay");
        m_Gameplay_Jump = m_Gameplay.GetAction("Jump");
        m_Gameplay_Move = m_Gameplay.GetAction("Move");
    }

    ~Player2ControlsScript()
    {
        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();
    }

    // Gameplay
    private InputActionMap m_Gameplay;
    private IGameplayActions m_GameplayActionsCallbackInterface;
    private InputAction m_Gameplay_Jump;
    private InputAction m_Gameplay_Move;
    public struct GameplayActions
    {
        private Player2ControlsScript m_Wrapper;
        public GameplayActions(Player2ControlsScript wrapper) { m_Wrapper = wrapper; }
        public InputAction @Jump { get { return m_Wrapper.m_Gameplay_Jump; } }
        public InputAction @Move { get { return m_Wrapper.m_Gameplay_Move; } }
        public InputActionMap Get() { return m_Wrapper.m_Gameplay; }
        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(GameplayActions set) { return set.Get(); }
        public void SetCallbacks(IGameplayActions instance)
        {
            if (m_Wrapper.m_GameplayActionsCallbackInterface != null)
            {
                Jump.started -= m_Wrapper.m_GameplayActionsCallbackInterface.OnJump;
                Jump.performed -= m_Wrapper.m_GameplayActionsCallbackInterface.OnJump;
                Jump.canceled -= m_Wrapper.m_GameplayActionsCallbackInterface.OnJump;
                Move.started -= m_Wrapper.m_GameplayActionsCallbackInterface.OnMove;
                Move.performed -= m_Wrapper.m_GameplayActionsCallbackInterface.OnMove;
                Move.canceled -= m_Wrapper.m_GameplayActionsCallbackInterface.OnMove;
            }
            m_Wrapper.m_GameplayActionsCallbackInterface = instance;
            if (instance != null)
            {
                Jump.started += instance.OnJump;
                Jump.performed += instance.OnJump;
                Jump.canceled += instance.OnJump;
                Move.started += instance.OnMove;
                Move.performed += instance.OnMove;
                Move.canceled += instance.OnMove;
            }
        }
    }
    public GameplayActions @Gameplay
    {
        get
        {
            return new GameplayActions(this);
        }
    }
    public interface IGameplayActions
    {
        void OnJump(InputAction.CallbackContext context);
        void OnMove(InputAction.CallbackContext context);
    }
}

我的“播放器”脚本

using UnityEngine;
using UnityEngine.InputSystem;

public class Player2_Controller: MonoBehaviour {

    Vector2 move;
    Player2ControlsScript controls;

void Awake () { 
        controls = new Player2ControlsScript();
        controls.Gameplay.Move.performed += ctx => move = ctx.ReadValue<Vector2>();
        controls.Gameplay.Move.canceled += ctx => move = Vector2.zero;
    }

    void OnEnable() {
            controls.Gameplay.Enable();
    }

    void OnDisable() {
        controls.Gameplay.Disable();
    }

}

【问题讨论】:

  • 我没有仔细查看代码,但错误非常简单。这是说您用于GetAction 的重载不存在。从docs 中,只有两个GetAction 重载,它们采用stringGUID,没有采用InputActionMap编辑:刚刚注意到您说这是从 Unity 生成的。后面可以看一下代码,不过sn-p比较大。

标签: c# unity3d


【解决方案1】:

不确定这是否有帮助,但尝试转到 CS1061 行,将鼠标放在红色波浪线(InputActionMap)上方,CTRL + ENTER 并点击再次输入。这能解决问题吗?

【讨论】:

    【解决方案2】:

    好的,我解决这个问题。我不知道是什么导致了这个问题,但它修复了错误。只需将软件包版本更改为 0.2.10-preview 即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 2020-05-16
      • 2016-12-27
      • 2014-05-06
      • 2014-03-13
      • 2017-08-11
      • 1970-01-01
      相关资源
      最近更新 更多