Advertisement
NovusX

Input System Upgraded

May 6th, 2023 (edited)
952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | Gaming | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.InputSystem;
  4.  
  5. namespace InputReader
  6. {
  7.     [CreateAssetMenu(menuName = "Input Reader")]
  8.     public class InputReader : ScriptableObject, GameInput.IGameplayActions, GameInput.IUIActions, GameInput.IActive_InventoryActions
  9.     {
  10.         public event Action<Vector2> OnMoveEvent;
  11.         public event Action OnNormalAttack;
  12.         public event Action OnAttackStopped;
  13.         public event Action OnDashEvent;
  14.         public event Action OnPauseEvent;
  15.         public event Action OnResumeEvent;
  16.         public event Action<int> OnToggleItemSlot;
  17.        
  18.         #region Exposed_Variables
  19.  
  20.         #endregion
  21.  
  22.         #region Private_Variables
  23.  
  24.         private GameInput _gameInput;
  25.  
  26.         #endregion
  27.  
  28.         #region Public_Variables
  29.  
  30.         #endregion
  31.  
  32.         #region Unity_Calls
  33.  
  34.         private void OnEnable()
  35.         {
  36.             if (_gameInput == null)
  37.             {
  38.                 _gameInput = new GameInput();
  39.                 _gameInput.Gameplay.SetCallbacks(this);
  40.                 _gameInput.UI.SetCallbacks(this);
  41.                 _gameInput.Active_Inventory.SetCallbacks(this);
  42.                
  43.                 SetGameplay();
  44.             }
  45.         }
  46.  
  47.         #endregion
  48.  
  49.         #region Private_Methods
  50.  
  51.         #endregion
  52.  
  53.         #region Public_Methods
  54.  
  55.         public void SetGameplay()
  56.         {
  57.             _gameInput.Gameplay.Enable();
  58.             _gameInput.Active_Inventory.Enable();
  59.             _gameInput.UI.Disable();
  60.         }
  61.        
  62.         public void SetUI()
  63.         {
  64.             _gameInput.Gameplay.Disable();
  65.             _gameInput.UI.Enable();
  66.             _gameInput.Active_Inventory.Disable();
  67.         }
  68.        
  69.         #endregion
  70.  
  71.  
  72.         #region GamePlay
  73.  
  74.         public void OnMove(InputAction.CallbackContext context)
  75.         {
  76.             OnMoveEvent?.Invoke(context.ReadValue<Vector2>());
  77.         }
  78.        
  79.         public void OnNormal_Attack(InputAction.CallbackContext context)
  80.         {
  81.             if (context.performed)
  82.             {
  83.                 OnNormalAttack?.Invoke();
  84.             }
  85.            
  86.             if (context.canceled)
  87.             {
  88.                 OnAttackStopped?.Invoke();
  89.             }
  90.         }
  91.  
  92.         public void OnDash(InputAction.CallbackContext context)
  93.         {
  94.             if (context.performed)
  95.             {
  96.                 OnDashEvent?.Invoke();
  97.             }
  98.         }
  99.  
  100.         public void OnPause(InputAction.CallbackContext context)
  101.         {
  102.             if (context.performed)
  103.             {
  104.                 OnPauseEvent?.Invoke();
  105.                 SetUI();
  106.             }
  107.         }
  108.  
  109.         #endregion
  110.  
  111.         #region UI
  112.        
  113.         public void OnResume(InputAction.CallbackContext context)
  114.         {
  115.             if (context.performed)
  116.             {
  117.                 OnResumeEvent?.Invoke();
  118.                 SetGameplay();
  119.             }
  120.         }
  121.  
  122.         #endregion
  123.  
  124.         #region Active Inventory
  125.        
  126.         public void OnActiveItemSlot(InputAction.CallbackContext context)
  127.         {
  128.             if (context.performed)
  129.             {
  130.                 OnToggleItemSlot?.Invoke((int)context.ReadValue<float>());
  131.             }
  132.         }
  133.        
  134.         #endregion
  135.        
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement