mcpField
U
unity-dependency-injection

unity-dependency-injection

github.com/muharremtozanDevelopment1751 installsUpdated 22w ago
Repository

About unity-dependency-injection

A Unity dependency-injection system for developers who want classes to depend on abstractions rather than concrete providers. It wires scene dependencies during Awake and logs missing providers.

Key features

  1. 01Attribute-based injection
  2. 02Interface-oriented dependencies
  3. 03Scene-wide wiring on Awake
  4. 04Injection diagnostics

Use cases

  1. 01Decoupling gameplay classes from implementations
  2. 02Providing dependencies across a Unity scene

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/muharremtozan/unity-agent-skills --skill unity-dependency-injection

After installing, type/unity-dependency-injectionin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

assets/code/
DependencyInjector.cs.txt
DIAttributes.cs.txt
DIExample.cs.txt

Details

From the project README

Unity Dependency Injection (DI)

A lightweight system to satisfy the "D" in SOLID. Allows classes to request dependencies via [Inject] without knowing who provides them.

Core Features

  1. Attribute-Based Injection: Use [Inject] to mark fields and [Provide] to define sources.
  2. Interface Focus: Encourages programming to abstractions, making systems hot-swappable.
  3. Automated Wiring: Scene-wide injection happens on Awake, ensuring dependencies are ready before Start.
  4. Diagnostic Logging: Tracks successful injections and warns about missing providers.

Core Files (Max 3)

  • DIAttributes.cs.txt: Contains the marker attributes for the compiler.
  • DependencyInjector.cs.txt: The engine that finds and wires dependencies in the scene.
  • DIExample.cs.txt: Shows how to decouple a Hero from an AbilitySystem.

Usage

1. Define an Interface
public interface IWeapon { void Fire(); }
2. Provide the Implementation
public class PlasmaRifle : MonoBehaviour, IWeapon {
    [Provide] public IWeapon GetWeapon() => this;
    public void Fire() { /* ... */ }
}
3. Inject into Client
public class Player : MonoBehaviour {
    [Inject] private IWeapon currentWeapon;
    void Start() => currentWeapon.Fire();
}
4. Setup

Place a DependencyInjector component in your scene or persistent prefab.

Comments

Markdown supported