[text] simple input manager

Viewer

copydownloadembedprintName: simple input manager
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class InputManager : MonoBehaviour
  6. {
  7.     public Dictionary<KeyCode, EventType> keybindings = new Dictionary<KeyCode, EventType>();
  8.     public Dictionary<KeyCode, EventType> keybindingsUp = new Dictionary<KeyCode, EventType>();
  9.  
  10.     private void OnEnable()
  11.     {
  12.         EventSystem.AddListener(EventType.INPUT_FS, ToggleFullscreen);
  13.     }
  14.  
  15.     private void OnDisable()
  16.     {
  17.         EventSystem.RemoveListener(EventType.INPUT_FS, ToggleFullscreen);
  18.     }
  19.  
  20.     private void Start()
  21.     {
  22.         keybindings.Add(KeyCode.A,           EventType.INPUT_LEFT);
  23.         keybindings.Add(KeyCode.D,           EventType.INPUT_RIGHT);
  24.         keybindings.Add(KeyCode.W,           EventType.INPUT_FORWARD);
  25.         keybindings.Add(KeyCode.S,           EventType.INPUT_BACK);
  26.         keybindings.Add(KeyCode.Space,       EventType.INPUT_UP);
  27.         keybindings.Add(KeyCode.LeftControl, EventType.INPUT_DOWN);
  28.         keybindings.Add(KeyCode.RightControl, EventType.INPUT_DOWN);
  29.         keybindings.Add(KeyCode.LeftShift,   EventType.INPUT_SPEED);
  30.         keybindings.Add(KeyCode.RightShift,  EventType.INPUT_SPEED);
  31.     }
  32.  
  33.     private void Update()
  34.     {
  35.         // check if any key is pressed and fire event related to it
  36.         foreach (KeyValuePair<KeyCode, EventType> keybinding in keybindings)
  37.         {
  38.             if (Input.GetKey(keybinding.Key))
  39.             {
  40.                 EventSystem.InvokeEvent(keybinding.Value);
  41.             }
  42.         }
  43.  
  44.         // check if any key is released and fire event related to it
  45.         foreach (KeyValuePair<KeyCode, EventType> keybinding in keybindingsUp)
  46.         {
  47.             if (Input.GetKeyUp(keybinding.Key))
  48.             {
  49.                 EventSystem.InvokeEvent(keybinding.Value);
  50.             }
  51.         }
  52.  
  53.         if (Input.GetAxis("Mouse ScrollWheel") > 0f) // zoom in 
  54.         {
  55.             EventSystem.InvokeEvent(EventType.INPUT_SCROLL_DOWN);
  56.         }
  57.         if (Input.GetAxis("Mouse ScrollWheel") < 0f) // zoom out
  58.         {
  59.             EventSystem.InvokeEvent(EventType.INPUT_SCROLL_UP);
  60.         }
  61.     }
  62. }
  63.     

Editor

You can edit this paste and save as new:


File Description
  • simple input manager
  • Paste Code
  • 29 Mar-2022
  • 2.16 Kb
You can Share it: