Attribute-based Dependency Injection for Unity Scenes
Jeringa is a lightweight Dependency Injection (DI) helper for Unity that uses custom C# attributes to automatically fetch component references from the active scene hierarchy.
using Jeringa;
public class DemoInjection : MonoBehaviour {
[FromSelf, SerializeField] Rigidbody selfRigidbody;
[FromType(typeof(MainPlayer))] Transform mainPlayerTransform;
[FromAny] AudioSource anyAudioSource;
[FromParent] CapsuleCollider parentCapsule;
[FromChild] Rigidbody childRigidbody;
[FromSibling] Collider2D[] siblingCollider2dArray;
void Awake() => Injector.Inject(this);
}It is designed for developers who want to:
- Avoid excessive
GetComponent()calls - Reduce manual inspector wiring
- Skip heavy DI frameworks for small projects
- Attribute-based dependency injection
- Works with single components or arrays
- Hierarchy-based injection (self, parent, child, sibling)
- Supports non-
MonoBehaviourclasses (FromAny-FromType)
- Method 1: Clone repository into your Unity project
- Method 2: Download latest .unitypackage from Releases
- Method 3: Add to Assets from Unity Store
To resolve dependencies, you must explicitly run the injector.
Call the injector from Awake():
using Jeringa;
public class DemoInjection : MonoBehaviour {
[FromAny] AudioSource audioSource;
void Awake() => Injector.Inject(this);
}Call the injector manually from a setup or constructor method:
using Jeringa;
public class InternalDemo {
[FromType(typeof(Animator))] Transform otherTransform;
public void Construct() => Injector.Inject(this);
}A
MassInjector.csscript is also provided. If this script is attached to a GameObject in the scene, it will iterate through all MonoBehaviors in the scene and will runInjector.Inject()in all of them.
Not all attributes are supported outside MonoBehaviour classes.
| Attribute | MonoBehaviour | Non-MonoBehaviour |
|---|---|---|
| FromType | Yes ✅ | Yes ✅ |
| FromAny | Yes ✅ | Yes ✅ |
| FromSelf | Yes ✅ | No ❌ |
| FromParent | Yes ✅ | No ❌ |
| FromChild | Yes ✅ | No ❌ |
| FromSibling | Yes ✅ | No ❌ |
Examples below assume the following hierarchy:
Any
Root
└─ Grandparent
└─ DirectParent
├─ Demo (script attached)
│ ├─ Child 1
│ │ └─ Grandchild 1
│ ├─ Child 2
│ └─ Child 3
│ └─ Grandchild 2
├─ Sibling 1
└─ Sibling 2
Fetches components from the same GameObject.
[FromSelf] Rigidbody selfRigidbody;
[FromSelf] Collider[] selfColliders;Fetches components from any object that contains a specific component type.
[FromType(typeof(Animator))] Transform typeTransform;
[FromType(typeof(Animator))] Collider[] typeColliders;Fetches components from anywhere in the scene.
[FromAny] AudioSource anyAudioSource;
[FromAny] Collider[] allColliders;Fetches components from parent objects.
| Value | Behavior |
|---|---|
0 |
Root parent |
1 |
Any parent (default) |
2+ |
Nth parent up the hierarchy |
[FromParent(0)] CapsuleCollider rootCapsule;
[FromParent(1)] Collider[] allParentColliders;
[FromParent(2)] AudioSource grandparentAudio;Fetches components from child objects.
| Value | Behavior |
|---|---|
0 |
Any child (recursive) |
1 |
Immediate children |
2+ |
Nth child |
[FromChild(0)] Rigidbody anyChildRigidbody;
[FromChild(1)] Transform immediateChildTransform;
[FromChild(2)] AudioSource secondChildAudio;Fetches components from sibling GameObjects.
[FromSibling] Transform siblingTransform;
[FromSibling] Collider2D[] siblingColliders;If a dependency cannot be resolved, Jeringa throws an exception:
Exception: [FromSibling] Component of type UnityEngine.Rigidbody2D not found in Demo (DemoInjection) for Demo (DemoInjection)
This means:
No
Rigidbody2Dwas found on any sibling of theDemoGameObject for theDemoInjectionscript.
Failures are explicit by design to avoid silent null references.
Here's an article about how to make your own "Jeringa"
Jeringa: Custom C# Attributes for Easy Dependency Injection