←All patterns
Structural

Adapter

Wrap an incompatible API so it looks exactly like the interface your game code already expects.

What Is It?

An Adapter sits between two pieces of code that disagree about shape. Your game talks to a target interface it owns, while the adaptee (usually a third-party SDK or a legacy system) exposes a different set of methods. The adapter implements the target and translates every call into the adaptee’s dialect.

In Unity this comes up constantly with input. A PlayerController only wants to ask input.JumpPressed. The legacy Input Manager answers with Input.GetButtonDown("Jump"), the Input System with action.WasPressedThisFrame(), and a vendor gamepad SDK might hand back a bitmask from PollButtons(). Three adapters hide all three behind one IInputProvider.

The key property is that neither side changes. You never edit the vendor SDK, and you never sprinkle #if blocks or SDK-specific calls through gameplay code. The translation lives in one small class per source, and switching sources means handing the controller a different adapter.

When Is It Used?

Reach for it when integrating code you do not control: platform SDKs, analytics providers, ad networks, store APIs, or an Asset Store package whose naming clashes with your conventions.

It is also the cleanest way to migrate gradually. Wrap the old system in an adapter today, write new code against your interface, and later drop in an adapter for the replacement without touching any callers.

Skip it when you own both sides. An adapter around your own code usually means the real fix is a rename or a refactor, not another layer.

Interactive Demo

Click the controls and watch the objects collaborate. The console mirrors what the C# code below would log.

Adapter Pattern
adapterLegacyjumps0translated0
PlayerController
client
«interface» IInputProvider
LegacyAdapter
plugged in
InputSysAdapter
idle
GamepadAdapter
idle
Input Mgr
Input System
Gamepad SDK
Console
Press a button to see what happens…

Code

Assets / Scripts / Structural/ Adapter ›PlayerController.cs
using UnityEngine;

namespace Patterns.Structural.Adapter
{
    /// <summary>
    /// Client. Only knows about IInputProvider, so it works with any
    /// input backend that has an adapter.
    /// </summary>
    [RequireComponent(typeof(Rigidbody))]
    public class PlayerController : MonoBehaviour
    {
        [SerializeField] private float moveSpeed = 6f;
        [SerializeField] private float jumpForce = 7f;
        [SerializeField] private bool useGamepadSdk;

        private IInputProvider input;
        private Rigidbody body;

        private void Awake()
        {
            body = GetComponent<Rigidbody>();

            // Pick a backend once; nothing below this line cares which.
            input = useGamepadSdk
                ? new GamepadAdapter(new ThirdPartyGamepad(0))
                : new LegacyInputAdapter();
        }

        public void SetInput(IInputProvider provider) => input = provider;

        private void Update()
        {
            input.Tick();

            Vector2 move = input.Move * moveSpeed;
            body.velocity = new Vector3(move.x, body.velocity.y, move.y);

            if (input.JumpPressed)
                body.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
        }
    }
}
4 files · namespace Patterns.Structural.AdapterC# · UTF-8 · LF

Before & After

Real-world smell: a player controller that talks to two input APIs directly. Here is the code without the pattern, the refactored version, and the exact diff between them.

! What’s wrong with it?

PlayerController holds a ThirdPartyGamepad and its button state, and Update branches on useGamepadSdk: one path normalises raw stick values and detects button edges, the other calls Input.GetAxis and Input.GetButtonDown.

Movement code is now mixed with SDK details. A third backend (the new Input System, a touch stick, a replay for tests) adds another branch here and in every other script that reads input, and an SDK update can break gameplay classes that have nothing to do with input.

✓ What changed

  • The pad and previousButtons fields and the whole if (useGamepadSdk) branch in Update are gone.
  • The player now holds one IInputProvider and reads input.Move and input.JumpPressed after input.Tick().
  • Stick normalisation and button-edge detection moved into GamepadAdapter; the legacy axis calls moved into LegacyInputAdapter.
  • The backend is chosen once in Awake, so a new input source is a new adapter class and the movement code stays the same.
PlayerController.Before.cs → PlayerController.After.cs+9/−26
11 using UnityEngine;
22
33 namespace Patterns.Structural.Adapter
44 {
55 /// <summary>
6− /// Without Adapter: the player talks to both input APIs directly and
7− /// branches on which one is active, every frame.
6+ /// With Adapter: the player only reads IInputProvider. Each input API
7+ /// is translated in its own adapter class.
88 /// </summary>
99 [RequireComponent(typeof(Rigidbody))]
1010 public class PlayerController : MonoBehaviour
1111 {
1212 [SerializeField] private float moveSpeed = 6f;
1313 [SerializeField] private float jumpForce = 7f;
1414 [SerializeField] private bool useGamepadSdk;
1515
16− private ThirdPartyGamepad pad;
17− private uint previousButtons;
16+ private IInputProvider input;
1817 private Rigidbody body;
1918
2019 private void Awake()
2120 {
2221 body = GetComponent<Rigidbody>();
2322
24− if (useGamepadSdk)
25− pad = new ThirdPartyGamepad(0);
23+ input = useGamepadSdk
24+ ? new GamepadAdapter(new ThirdPartyGamepad(0))
25+ : new LegacyInputAdapter();
2626 }
2727
2828 private void Update()
2929 {
30− Vector2 raw;
31− bool jumpPressed;
30+ input.Tick();
3231
33− if (useGamepadSdk)
34− {
35− raw = new Vector2(pad.ReadStick(0) / 32767f, pad.ReadStick(1) / 32767f);
36−
37− uint buttons = pad.PollButtons();
38− bool down = (buttons & ThirdPartyGamepad.ButtonA) != 0;
39− bool wasDown = (previousButtons & ThirdPartyGamepad.ButtonA) != 0;
40− jumpPressed = down && !wasDown;
41− previousButtons = buttons;
42− }
43− else
44− {
45− raw = Vector2.ClampMagnitude(new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")), 1f);
46− jumpPressed = Input.GetButtonDown("Jump");
47− }
48−
49− Vector2 move = raw * moveSpeed;
32+ Vector2 move = input.Move * moveSpeed;
5033 body.velocity = new Vector3(move.x, body.velocity.y, move.y);
5134
52− if (jumpPressed)
35+ if (input.JumpPressed)
5336 body.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
5437 }
5538 }
5639 }

Advantages & Disadvantages

+ Advantages

  • Gameplay depends on your interface rather than a vendor’s, so SDK upgrades are contained to one file.
  • Multiple backends become interchangeable per platform or even at runtime.
  • Testing gets easy: a fake adapter can feed scripted input or canned responses deterministically.

− Disadvantages

  • Adds a class and an indirection per adaptee; trivial wrappers can feel like ceremony.
  • The shared interface drifts toward the lowest common denominator, so features only one backend has are awkward to expose.
  • Semantic mismatches (polled vs. event-driven, sync vs. async) can leak through however clean the signature looks.

Tips

  1. 01Keep the target interface small and game-shaped (JumpPressed, Move) instead of mirroring any single SDK.
  2. 02If the adaptee is event-driven, buffer events inside the adapter so the target can still be polled once per frame.
  3. 03Choose the adapter in a bootstrapper or composition root, not inside the consumer, so PlayerController never names concrete types.
  4. 04Use [SerializeReference] or a ScriptableObject factory if designers need to pick the backend in the Inspector.
  5. 05A ScriptedInputAdapter that replays a recording makes play-mode tests trivial: it is the same pattern pointed at a file.