InputSystem icon indicating copy to clipboard operation
InputSystem copied to clipboard

Interfaces created by Generate Script Usage

Open hridpath opened this issue 7 years ago • 1 comments

I am trying to implement this Input system on a new project and I see that there are interfaceses created for each of the Action Maps I have defined in the editor.

Are these to be implemented by the handler code or only for the interal usage of the generated script?

I have tried with and without the implementations. By entering Using Static <InputActionClassName> and extending the current class with the Interfaces, I got nothing.

So instead I create new <InputActionClassName> and wiring the performed events, I still do not get the events firing.

Here is the Generated Code and the Monobehavior class (SimpleCameraController.cs from LWRP examples, to use new InputSystem) that is using the <InputActionClassName> Generated code: `// GENERATED AUTOMATICALLY FROM "Assets/WF/InputScripts/CameraSimpleControl.inputactions"

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

namespace DesertSapphire.Input { public class CameraSimpleControl : IInputActionCollection, IDisposable { private InputActionAsset asset; public CameraSimpleControl() { asset = InputActionAsset.FromJson(@"{ ""name"": ""CameraSimpleControl"", ""maps"": [ { ""name"": ""CameraMovement"", ""id"": ""2c65c175-e67f-43d1-9690-2317aca06511"", ""actions"": [ { ""name"": ""Move"", ""type"": ""Button"", ""id"": ""e453e7a1-7962-4293-ab9f-89984ea7d037"", ""expectedControlType"": """", ""processors"": """", ""interactions"": """" }, { ""name"": ""Tilt"", ""type"": ""Button"", ""id"": ""43c218e4-aac1-4e95-83fe-f38a04f20d62"", ""expectedControlType"": """", ""processors"": """", ""interactions"": """" } ], ""bindings"": [ { ""name"": ""WASD"", ""id"": ""94d1ddc4-8793-4601-81f4-993915a0d103"", ""path"": ""2DVector"", ""interactions"": """", ""processors"": """", ""groups"": """", ""action"": ""Move"", ""isComposite"": true, ""isPartOfComposite"": false }, { ""name"": ""up"", ""id"": ""0f3ee247-22ec-4afa-885b-15a1ba0a0b4b"", ""path"": ""/w"", ""interactions"": """", ""processors"": """", ""groups"": ""KeyboardMouse"", ""action"": ""Move"", ""isComposite"": false, ""isPartOfComposite"": true }, { ""name"": ""down"", ""id"": ""83d9d50c-1f86-42dd-b99e-4f1aa2bc5ccc"", ""path"": ""/s"", ""interactions"": """", ""processors"": """", ""groups"": ""KeyboardMouse"", ""action"": ""Move"", ""isComposite"": false, ""isPartOfComposite"": true }, { ""name"": ""left"", ""id"": ""8ab52d5f-610d-49ef-bbc8-cf13bf5406c8"", ""path"": ""/a"", ""interactions"": """", ""processors"": """", ""groups"": ""KeyboardMouse"", ""action"": ""Move"", ""isComposite"": false, ""isPartOfComposite"": true }, { ""name"": ""right"", ""id"": ""a868acd2-4fbc-4078-b453-b5ef362d5b1b"", ""path"": ""/d"", ""interactions"": """", ""processors"": """", ""groups"": ""KeyboardMouse"", ""action"": ""Move"", ""isComposite"": false, ""isPartOfComposite"": true }, { ""name"": ""QE"", ""id"": ""65b4e239-877a-4f18-b899-fcf6526360dd"", ""path"": ""1DAxis"", ""interactions"": """", ""processors"": """", ""groups"": """", ""action"": ""Move"", ""isComposite"": true, ""isPartOfComposite"": false }, { ""name"": """", ""id"": ""0c19994d-870b-4b1b-8324-0b359e0995d3"", ""path"": ""/q"", ""interactions"": ""Press"", ""processors"": """", ""groups"": ""KeyboardMouse"", ""action"": ""Tilt"", ""isComposite"": false, ""isPartOfComposite"": false }, { ""name"": """", ""id"": ""a2728092-6caa-4222-afbb-a12d3e880652"", ""path"": ""/e"", ""interactions"": ""Press"", ""processors"": """", ""groups"": ""KeyboardMouse"", ""action"": ""Tilt"", ""isComposite"": false, ""isPartOfComposite"": false } ] }, { ""name"": ""MouseInput"", ""id"": ""dd3e222c-3992-4ef8-8849-410b54836c30"", ""actions"": [ { ""name"": ""RightButton"", ""type"": ""Button"", ""id"": ""b2d3c3be-d274-40e6-bca6-39bb08d09c3e"", ""expectedControlType"": """", ""processors"": """", ""interactions"": """" }, { ""name"": ""ScrollWheel"", ""type"": ""Value"", ""id"": ""2eec6070-88e9-447c-aeb8-48e55b6a2cfa"", ""expectedControlType"": ""Vector2"", ""processors"": """", ""interactions"": """" } ], ""bindings"": [ { ""name"": """", ""id"": ""40cfd0d5-76b9-4e5e-bde3-780d9ff02429"", ""path"": ""/rightButton"", ""interactions"": ""Press(behavior=2)"", ""processors"": """", ""groups"": ""KeyboardMouse"", ""action"": ""RightButton"", ""isComposite"": false, ""isPartOfComposite"": false }, { ""name"": """", ""id"": ""b13c8220-b68e-49dc-be99-91b1c60a0b8d"", ""path"": ""/scroll"", ""interactions"": """", ""processors"": """", ""groups"": ""KeyboardMouse"", ""action"": ""ScrollWheel"", ""isComposite"": false, ""isPartOfComposite"": false } ] }, { ""name"": ""Misc"", ""id"": ""80b046ca-3f0b-4c68-a05b-702566f3a1d3"", ""actions"": [ { ""name"": ""Interaction"", ""type"": ""Button"", ""id"": ""53dcd6c0-18c2-4008-804d-aafc6d48d0b8"", ""expectedControlType"": """", ""processors"": """", ""interactions"": """" } ], ""bindings"": [ { ""name"": """", ""id"": ""59b74ae9-6d81-418d-987f-66e98f33686b"", ""path"": ""/escape"", ""interactions"": ""Press"", ""processors"": """", ""groups"": ""KeyboardMouse"", ""action"": ""Interaction"", ""isComposite"": false, ""isPartOfComposite"": false }, { ""name"": """", ""id"": ""c5460be8-5091-42df-952d-7cd8a8d5d4a8"", ""path"": ""/leftShift"", ""interactions"": ""Press"", ""processors"": """", ""groups"": ""KeyboardMouse"", ""action"": ""Interaction"", ""isComposite"": false, ""isPartOfComposite"": false } ] } ], ""controlSchemes"": [ { ""name"": ""KeyboardMouse"", ""bindingGroup"": ""KeyboardMouse"", ""devices"": [ { ""devicePath"": """", ""isOptional"": false, ""isOR"": false }, { ""devicePath"": """", ""isOptional"": false, ""isOR"": false } ] } ] }"); // CameraMovement m_CameraMovement = asset.FindActionMap("CameraMovement", throwIfNotFound: true); m_CameraMovement_Move = m_CameraMovement.FindAction("Move", throwIfNotFound: true); m_CameraMovement_Tilt = m_CameraMovement.FindAction("Tilt", throwIfNotFound: true); // MouseInput m_MouseInput = asset.FindActionMap("MouseInput", throwIfNotFound: true); m_MouseInput_RightButton = m_MouseInput.FindAction("RightButton", throwIfNotFound: true); m_MouseInput_ScrollWheel = m_MouseInput.FindAction("ScrollWheel", throwIfNotFound: true); // Misc m_Misc = asset.FindActionMap("Misc", throwIfNotFound: true); m_Misc_Interaction = m_Misc.FindAction("Interaction", throwIfNotFound: true); }

    public void Dispose()
    {
        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 => 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();
    }

    // CameraMovement
    private readonly InputActionMap m_CameraMovement;
    private ICameraMovementActions m_CameraMovementActionsCallbackInterface;
    private readonly InputAction m_CameraMovement_Move;
    private readonly InputAction m_CameraMovement_Tilt;
    public struct CameraMovementActions
    {
        private CameraSimpleControl m_Wrapper;
        public CameraMovementActions(CameraSimpleControl wrapper) { m_Wrapper = wrapper; }
        public InputAction @Move => m_Wrapper.m_CameraMovement_Move;
        public InputAction @Tilt => m_Wrapper.m_CameraMovement_Tilt;
        public InputActionMap Get() { return m_Wrapper.m_CameraMovement; }
        public void Enable() { Get().Enable(); }
        public void Disable() { Get().Disable(); }
        public bool enabled => Get().enabled;
        public static implicit operator InputActionMap(CameraMovementActions set) { return set.Get(); }
        public void SetCallbacks(ICameraMovementActions instance)
        {
            if (m_Wrapper.m_CameraMovementActionsCallbackInterface != null)
            {
                Move.started -= m_Wrapper.m_CameraMovementActionsCallbackInterface.OnMove;
                Move.performed -= m_Wrapper.m_CameraMovementActionsCallbackInterface.OnMove;
                Move.canceled -= m_Wrapper.m_CameraMovementActionsCallbackInterface.OnMove;
                Tilt.started -= m_Wrapper.m_CameraMovementActionsCallbackInterface.OnTilt;
                Tilt.performed -= m_Wrapper.m_CameraMovementActionsCallbackInterface.OnTilt;
                Tilt.canceled -= m_Wrapper.m_CameraMovementActionsCallbackInterface.OnTilt;
            }
            m_Wrapper.m_CameraMovementActionsCallbackInterface = instance;
            if (instance != null)
            {
                Move.started += instance.OnMove;
                Move.performed += instance.OnMove;
                Move.canceled += instance.OnMove;
                Tilt.started += instance.OnTilt;
                Tilt.performed += instance.OnTilt;
                Tilt.canceled += instance.OnTilt;
            }
        }
    }
    public CameraMovementActions @CameraMovement => new CameraMovementActions(this);

    // MouseInput
    private readonly InputActionMap m_MouseInput;
    private IMouseInputActions m_MouseInputActionsCallbackInterface;
    private readonly InputAction m_MouseInput_RightButton;
    private readonly InputAction m_MouseInput_ScrollWheel;
    public struct MouseInputActions
    {
        private CameraSimpleControl m_Wrapper;
        public MouseInputActions(CameraSimpleControl wrapper) { m_Wrapper = wrapper; }
        public InputAction @RightButton => m_Wrapper.m_MouseInput_RightButton;
        public InputAction @ScrollWheel => m_Wrapper.m_MouseInput_ScrollWheel;
        public InputActionMap Get() { return m_Wrapper.m_MouseInput; }
        public void Enable() { Get().Enable(); }
        public void Disable() { Get().Disable(); }
        public bool enabled => Get().enabled;
        public static implicit operator InputActionMap(MouseInputActions set) { return set.Get(); }
        public void SetCallbacks(IMouseInputActions instance)
        {
            if (m_Wrapper.m_MouseInputActionsCallbackInterface != null)
            {
                RightButton.started -= m_Wrapper.m_MouseInputActionsCallbackInterface.OnRightButton;
                RightButton.performed -= m_Wrapper.m_MouseInputActionsCallbackInterface.OnRightButton;
                RightButton.canceled -= m_Wrapper.m_MouseInputActionsCallbackInterface.OnRightButton;
                ScrollWheel.started -= m_Wrapper.m_MouseInputActionsCallbackInterface.OnScrollWheel;
                ScrollWheel.performed -= m_Wrapper.m_MouseInputActionsCallbackInterface.OnScrollWheel;
                ScrollWheel.canceled -= m_Wrapper.m_MouseInputActionsCallbackInterface.OnScrollWheel;
            }
            m_Wrapper.m_MouseInputActionsCallbackInterface = instance;
            if (instance != null)
            {
                RightButton.started += instance.OnRightButton;
                RightButton.performed += instance.OnRightButton;
                RightButton.canceled += instance.OnRightButton;
                ScrollWheel.started += instance.OnScrollWheel;
                ScrollWheel.performed += instance.OnScrollWheel;
                ScrollWheel.canceled += instance.OnScrollWheel;
            }
        }
    }
    public MouseInputActions @MouseInput => new MouseInputActions(this);

    // Misc
    private readonly InputActionMap m_Misc;
    private IMiscActions m_MiscActionsCallbackInterface;
    private readonly InputAction m_Misc_Interaction;
    public struct MiscActions
    {
        private CameraSimpleControl m_Wrapper;
        public MiscActions(CameraSimpleControl wrapper) { m_Wrapper = wrapper; }
        public InputAction @Interaction => m_Wrapper.m_Misc_Interaction;
        public InputActionMap Get() { return m_Wrapper.m_Misc; }
        public void Enable() { Get().Enable(); }
        public void Disable() { Get().Disable(); }
        public bool enabled => Get().enabled;
        public static implicit operator InputActionMap(MiscActions set) { return set.Get(); }
        public void SetCallbacks(IMiscActions instance)
        {
            if (m_Wrapper.m_MiscActionsCallbackInterface != null)
            {
                Interaction.started -= m_Wrapper.m_MiscActionsCallbackInterface.OnInteraction;
                Interaction.performed -= m_Wrapper.m_MiscActionsCallbackInterface.OnInteraction;
                Interaction.canceled -= m_Wrapper.m_MiscActionsCallbackInterface.OnInteraction;
            }
            m_Wrapper.m_MiscActionsCallbackInterface = instance;
            if (instance != null)
            {
                Interaction.started += instance.OnInteraction;
                Interaction.performed += instance.OnInteraction;
                Interaction.canceled += instance.OnInteraction;
            }
        }
    }
    public MiscActions @Misc => new MiscActions(this);
    private int m_KeyboardMouseSchemeIndex = -1;
    public InputControlScheme KeyboardMouseScheme
    {
        get
        {
            if (m_KeyboardMouseSchemeIndex == -1) m_KeyboardMouseSchemeIndex = asset.FindControlSchemeIndex("KeyboardMouse");
            return asset.controlSchemes[m_KeyboardMouseSchemeIndex];
        }
    }
    public interface ICameraMovementActions
    {
        void OnMove(InputAction.CallbackContext context);
        void OnTilt(InputAction.CallbackContext context);
    }
    public interface IMouseInputActions
    {
        void OnRightButton(InputAction.CallbackContext context);
        void OnScrollWheel(InputAction.CallbackContext context);
    }
    public interface IMiscActions
    {
        void OnInteraction(InputAction.CallbackContext context);
    }
}

} and the Monoehavior: using UnityEngine; using UnityEngine.InputSystem; using DesertSapphire.Input;

namespace UnityTemplateProjects { public class SimpleCameraController : MonoBehaviour { static CameraSimpleControl inputActions; class CameraState { public float yaw; public float pitch; public float roll; public float x; public float y; public float z;

        public void SetFromTransform(Transform t)
        {
            pitch = t.eulerAngles.x;
            yaw = t.eulerAngles.y;
            roll = t.eulerAngles.z;
            x = t.position.x;
            y = t.position.y;
            z = t.position.z;
        }

        public void Translate(Vector3 translation)
        {
            Vector3 rotatedTranslation = Quaternion.Euler(pitch, yaw, roll) * translation;

            x += rotatedTranslation.x;
            y += rotatedTranslation.y;
            z += rotatedTranslation.z;
        }

        public void LerpTowards(CameraState target, float positionLerpPct, float rotationLerpPct)
        {
            yaw = Mathf.Lerp(yaw, target.yaw, rotationLerpPct);
            pitch = Mathf.Lerp(pitch, target.pitch, rotationLerpPct);
            roll = Mathf.Lerp(roll, target.roll, rotationLerpPct);
            
            x = Mathf.Lerp(x, target.x, positionLerpPct);
            y = Mathf.Lerp(y, target.y, positionLerpPct);
            z = Mathf.Lerp(z, target.z, positionLerpPct);
        }

        public void UpdateTransform(Transform t)
        {
            t.eulerAngles = new Vector3(pitch, yaw, roll);
            t.position = new Vector3(x, y, z);
        }
    }
    
    CameraState m_TargetCameraState = new CameraState();
    CameraState m_InterpolatingCameraState = new CameraState();

    [Header("Movement Settings")]
    [Tooltip("Exponential boost factor on translation, controllable by mouse wheel.")]
    public float boost = 3.5f;

    [Tooltip("Time it takes to interpolate camera position 99% of the way to the target."), Range(0.001f, 1f)]
    public float positionLerpTime = 0.2f;

    [Header("Rotation Settings")]
    [Tooltip("X = Change in mouse position.\nY = Multiplicative factor for camera rotation.")]
    public AnimationCurve mouseSensitivityCurve = new AnimationCurve(new Keyframe(0f, 0.5f, 0f, 5f), new Keyframe(1f, 2.5f, 0f, 0f));

    [Tooltip("Time it takes to interpolate camera rotation 99% of the way to the target."), Range(0.001f, 1f)]
    public float rotationLerpTime = 0.01f;

    [Tooltip("Whether or not to invert our Y axis for mouse input to rotation.")]
    public bool invertY = false;

    private Vector3 _direction = new Vector3();
    private KeyCode _interaction = KeyCode.None;
    private bool _rightButtonDown = false;
    private bool _quit = false;
    private float _scrollUp = 0f;
    private float _scrollDown = 0f;
    void OnEnable()
    {
        inputActions = new CameraSimpleControl();
        m_TargetCameraState.SetFromTransform(transform);
        m_InterpolatingCameraState.SetFromTransform(transform);
        inputActions.CameraMovement.Move.performed += OnMove;
        inputActions.CameraMovement.Tilt.performed += OnTilt;
        inputActions.Misc.Interaction.performed += OnInteraction;
        inputActions.MouseInput.ScrollWheel.performed += OnScrollWheel;
        inputActions.MouseInput.RightButton.performed += OnRightButton;

    }

    void SetInputTranslationDirection(KeyCode key)
    {
        _direction = new Vector3();
        switch (key)
        {
            case KeyCode.W:
                _direction += Vector3.forward;
                break;
            case KeyCode.S:
                _direction += Vector3.back;
                break;
            case KeyCode.A:
                _direction += Vector3.left;
                break;
            case KeyCode.D:
                _direction += Vector3.right;
                break;
            case KeyCode.Q:
                _direction += Vector3.down;
                break;
            case KeyCode.E:
                _direction += Vector3.up;
                break;
        }
    }
    
    void Update()
    {
        var mouse = Mouse.current;
        // Exit Sample  
        if (_interaction == KeyCode.Escape)
        {
            Application.Quit();
			#if UNITY_EDITOR
			UnityEditor.EditorApplication.isPlaying = false; 
			#endif
        }

        // Hide and lock cursor when right mouse button pressed
        if (_rightButtonDown)
        {
            Cursor.lockState = CursorLockMode.Locked;
        }

        // Unlock and show cursor when right mouse button released
        if (!_rightButtonDown)
        {
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
        }

        // Rotation
        if (_rightButtonDown)
        {
           
            var mPos = mouse.position;
            //var mouseMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y") * (invertY ? 1 : -1));
            var mouseMovement = new Vector2(mPos.x.ReadValue(), mPos.y.ReadValue()   * (invertY ? 1 : -1));
            var mouseSensitivityFactor = mouseSensitivityCurve.Evaluate(mouseMovement.magnitude);

            m_TargetCameraState.yaw += mouseMovement.x * mouseSensitivityFactor;
            m_TargetCameraState.pitch += mouseMovement.y * mouseSensitivityFactor;
        }
        
        // Translation
        var translation = _direction * Time.deltaTime;

        // Speed up movement when shift key held
        if (_interaction == KeyCode.LeftShift)
        {
            translation *= 10.0f;
        }
        
        // Modify movement by a boost factor (defined in Inspector and modified in play mode through the mouse scroll wheel)
        
        boost += mouse.scroll.ReadValue().y * 0.2f;
        translation *= Mathf.Pow(2.0f, boost);

        m_TargetCameraState.Translate(translation);

        // Framerate-independent interpolation
        // Calculate the lerp amount, such that we get 99% of the way to our target in the specified time
        var positionLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / positionLerpTime) * Time.deltaTime);
        var rotationLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / rotationLerpTime) * Time.deltaTime);
        m_InterpolatingCameraState.LerpTowards(m_TargetCameraState, positionLerpPct, rotationLerpPct);

        m_InterpolatingCameraState.UpdateTransform(transform);
    }

    public void OnMove(InputAction.CallbackContext context)
    {
        var key = context.ReadValue<KeyCode>();
        SetInputTranslationDirection(key);
    }

    public void OnTilt(InputAction.CallbackContext context)
    {
        var key = context.ReadValue<KeyCode>();
        SetInputTranslationDirection(key);
    }

    public void OnRightButton(InputAction.CallbackContext context)
    {
        _rightButtonDown = context.phase == InputActionPhase.Performed;
    }

    public void OnScrollWheel(InputAction.CallbackContext context)
    {

    }

    public void OnInteraction(InputAction.CallbackContext context)
    {
        _interaction = context.ReadValue<KeyCode>();
        
    }
}

}`

hridpath avatar Oct 17 '19 00:10 hridpath

Are these to be implemented by the handler code

It's meant to be used with the generated SetCallbacks method.

I still do not get the events firing.

You need to enable the actions using Enable on the asset, individual maps, or individual actions.

Rene-Damm avatar Oct 17 '19 07:10 Rene-Damm