MuharremTozan/Agentic-Duel / .agent/skills/unity-dependency-injection
unity-dependency-injection
VerifiedImplements Dependency Inversion and Injection patterns. Decouples high-level logic from concrete implementations using attributes and reflection.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 1.6 KB |
| assets/code/DIAttributes.cs.txt | markdown | 566 B |
| assets/code/DIExample.cs.txt | markdown | 762 B |
| assets/code/DependencyInjector.cs.txt | markdown | 2.3 KB |
assets/code/DependencyInjector.cs.txt
View on GitHubusing System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
namespace Standard.Patterns.DI {
[DefaultExecutionOrder(-1000)]
public class DependencyInjector : MonoBehaviour {
[Header("Settings")]
[SerializeField] private bool logInjections = true;
private void Awake() {
InjectAll();
}
public void InjectAll() {
var monoBehaviours = FindObjectsByType<MonoBehaviour>(FindObjectsInactive.Include, FindObjectsSortMode.None);
// 1. Find all Providers
var providers = new Dictionary<Type, object>();
foreach (var mb in monoBehaviours) {
var methods = mb.GetType().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var method in methods) {
if (method.GetCustomAttribute<ProvideAttribute>() != null) {
providers[method.ReturnType] = method.Invoke(mb, null);
}
}
var fields = mb.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var field in fields) {
if (field.GetCustomAttribute<ProvideAttribute>() != null) {
providers[field.FieldType] = field.GetValue(mb);
}
}
}
// 2. Inject into marked fields
foreach (var mb in monoBehaviours) {
var fields = mb.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var field in fields) {
if (field.GetCustomAttribute<InjectAttribute>() != null) {
if (providers.TryGetValue(field.FieldType, out var value)) {
field.SetValue(mb, value);
if (logInjections) Debug.Log($"[DI] Status: Injected {field.FieldType.Name} into {mb.name} | Data: Success");
} else {
Debug.LogError($"[DI] Fail: No provider found for {field.FieldType.Name} on {mb.name}");
}
}
}
}
}
}
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |