InputManager - jimdroberts/FishMMO GitHub Wiki

Description

A static utility class for managing game input in FishMMO, providing an abstraction layer over Unity's built-in Input and EventSystem. It allows for customizable virtual key and axis mappings, controls mouse visibility and lock state, and supports configuration overrides for flexible input management.


API Access

Fields

  • private static readonly Dictionary<string, KeyMap> virtualKeyMaps

    Maps custom virtual key names to their corresponding KeyMap objects for flexible key rebinding.

  • private static readonly Dictionary<string, string> axisMaps

    Maps custom virtual axis names to Unity Input Manager axis names for flexible axis rebinding.

Properties

  • public static bool ForcedMouseMode { get; private set; }

    Indicates whether the mouse cursor's visibility is currently being forced by an explicit call to ToggleMouseMode.

  • public static bool MouseMode { get; set; }

    Gets or sets the current mouse mode. When true, the cursor is visible and unlocked; when false, it is hidden and locked.

Events

  • public static event System.Action OnToggleMouseMode

    Invoked whenever the MouseMode property is toggled. The boolean parameter indicates the new state.

Methods

  • public static void ResetForcedMouseMode()

    Resets the ForcedMouseMode flag to false, allowing the mouse mode to be toggled again.

  • public static void ToggleMouseMode(bool forceMouseMode = false)

    Toggles the MouseMode between visible/unlocked and hidden/locked. Optionally forces the mode.

  • public static void AddKey(string virtualKey, KeyCode keyCode)

    Adds or updates a virtual key mapping.

  • public static KeyCode GetKeyCode(string virtualKey)

    Retrieves the KeyCode currently mapped to a given virtual key.

  • public static bool GetKey(string virtualKey)

    Checks if a virtual key is currently being held down.

  • public static bool GetKeyDown(string virtualKey)

    Checks if a virtual key was pressed down in the current frame.

  • public static bool GetKeyUp(string virtualKey)

    Checks if a virtual key was released in the current frame.

  • public static void AddAxis(string virtualAxis, string unityAxis)

    Adds or updates a virtual axis mapping.

  • public static float GetAxis(string virtualAxis)

    Retrieves the value of a virtual axis (smoothed input).

  • public static float GetAxisRaw(string virtualAxis)

    Retrieves the raw value of a virtual axis (no smoothing).

  • public static IEnumerable GetVirtualKeyNames()

    Returns all registered virtual key names.

  • public static IEnumerable GetVirtualAxisNames()

    Returns all registered virtual axis names.

  • public static void ForceClickMouseButtonInCenterOfGameWindow()

    In the Unity Editor, sends a simulated left mouse button click event to the center of the Game window (editor only).


Basic Usage

Setup

  1. InputManager is static and initializes itself on first access.
  2. Use AddKey and AddAxis to customize key and axis mappings as needed.
  3. Use MouseMode and ToggleMouseMode to control mouse visibility and lock state.
  4. Use GetKey, GetKeyDown, GetKeyUp, GetAxis, and GetAxisRaw for input checks throughout your game code.

Example

// Toggle mouse mode (e.g., on key press)
InputManager.ToggleMouseMode();

// Check if the "Jump" virtual key is pressed
if (InputManager.GetKeyDown("Jump"))
{
    // Handle jump action
}

// Get movement axis
float vertical = InputManager.GetAxis("Vertical");
float horizontal = InputManager.GetAxis("Horizontal");

Best Practices

  • Use virtual key and axis names throughout your code for flexibility and easy rebinding.
  • Subscribe to OnToggleMouseMode to react to mouse mode changes in your UI or gameplay systems.
  • Always check for null EventSystem references when interacting with UI.
  • Use configuration overrides to support user-customizable input schemes.
  • In the Unity Editor, use ForceClickMouseButtonInCenterOfGameWindow to ensure the Game window captures input focus after locking the cursor.
⚠️ **GitHub.com Fallback** ⚠️