Chain of Responsibility Pattern Example

December 24, 2024 ยท View on GitHub

A Unity example demonstrating the Chain of Responsibility pattern with a game input handling system.

Overview

The Chain of Responsibility pattern manages input handling across different game states (gameplay, UI, cutscenes, dialog). Each handler processes input based on the current game state, ensuring inputs are handled appropriately in each context.

Implementation

Usage

// Setup is automatic - just add to a GameObject
InputManager manager = gameObject.AddComponent<InputManager>();

// Change states as needed
manager.SetGameState(InputState.Dialog);    // When entering dialog
manager.SetGameState(InputState.Gameplay);  // When returning to gameplay

Benefits

  • Clean separation of input handling by game state
  • Easy to add new input states and handlers
  • Prevents input conflicts between states
  • Centralized input management