Skip to content

simonsanchezart/Jeringa-Dependency-Injector

Repository files navigation

Jeringa 💉

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

Features

  • Attribute-based dependency injection
  • Works with single components or arrays
  • Hierarchy-based injection (self, parent, child, sibling)
  • Supports non-MonoBehaviour classes (FromAny - FromType)

Installation

  • Method 1: Clone repository into your Unity project
  • Method 2: Download latest .unitypackage from Releases
  • Method 3: Add to Assets from Unity Store

Basic Usage

To resolve dependencies, you must explicitly run the injector.

MonoBehaviour

Call the injector from Awake():

using Jeringa;

public class DemoInjection : MonoBehaviour {
    [FromAny] AudioSource audioSource;

    void Awake() => Injector.Inject(this);
}

Non-MonoBehaviour

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.cs script 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 run Injector.Inject() in all of them.

Non-MonoBehaviour Support

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 ❌

Scene Hierarchy Reference

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

Injection Attributes

[FromSelf]

Fetches components from the same GameObject.

[FromSelf] Rigidbody selfRigidbody;
[FromSelf] Collider[] selfColliders;

[FromType(Type)]

Fetches components from any object that contains a specific component type.

[FromType(typeof(Animator))] Transform typeTransform;
[FromType(typeof(Animator))] Collider[] typeColliders;

[FromAny]

Fetches components from anywhere in the scene.

[FromAny] AudioSource anyAudioSource;
[FromAny] Collider[] allColliders;

[FromParent(int)]

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;

[FromChild(int)]

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;

[FromSibling]

Fetches components from sibling GameObjects.

[FromSibling] Transform siblingTransform;
[FromSibling] Collider2D[] siblingColliders;

Error Handling

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 Rigidbody2D was found on any sibling of the Demo GameObject for the DemoInjection script.

Failures are explicit by design to avoid silent null references.

Want to know more?

Here's an article about how to make your own "Jeringa"
Jeringa: Custom C# Attributes for Easy Dependency Injection

About

Attribute-based Dependency Injection for Unity Scenes

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages