Giter VIP home page Giter VIP logo

unity-dependency-injection-lite's Introduction

Unity C# Dependency Injection Lite

DependencyInjection

Unity Dependency Injection Lite is a lightweight, easy-to-use dependency injection system for Unity. It leverages Unity's MonoBehaviour to automatically inject dependencies into your classes. This system supports field, method and property injections, and allows for easy setup of providers.

Features

  • Automatic Dependency Injection: Automatically injects dependencies into your Unity MonoBehaviours.
  • Custom Attributes: Use [Inject] and [Provide] attributes to denote injectable members and providers.
  • Method Injection: Supports method injection for more complex and multiple initialization.
  • Field Injection: Simplify your code with direct field injection.
  • Property Injection: Supports property injection.

Usage

Defining Injectable Fields, Methods and Properties

Use the [Inject] attribute on fields, methods, or properties to mark them as targets for injection.

using DependencyInjection;
using UnityEngine;

public class ClassA : MonoBehaviour {
    [Inject] IServiceA serviceA;
    
    IServiceB serviceB;
    
    [Inject]
    public void Init(ServiceB service) {
        this.serviceB = service;
    }
    
    [Inject]
    public IServiceC Service { get; private set; }
}

Creating Providers

Implement IDependencyProvider and use the [Provide] attribute on methods to define how dependencies are created.

using DependencyInjection;
using UnityEngine;

public class Provider : MonoBehaviour, IDependencyProvider {
    [Provide]
    public IServiceA ProvideServiceA() {
        return new ServiceA();
    }

    // Other provides...
}

Example of Using Multiple Dependencies

using DependencyInjection;
using UnityEngine;

public class ClassB : MonoBehaviour {
    [Inject] IServiceA serviceA;
    
    IServiceB serviceB;
    IFactoryA factoryA;
        
    [Inject] // Method injection supports multiple dependencies
    public void Init(IFactoryA factoryA, IServiceB serviceB) {
        this.factoryA = factoryA;
        this.serviceB = serviceB;
    }

    void Start() {
        serviceA.Initialize("ServiceA initialized from ClassB");
        serviceB.Initialize("ServiceB initialized from ClassB");
        factoryA.CreateServiceA().Initialize("ServiceA initialized from FactoryA");
    }
}

Setup

  • Include the Dependency Injection System: Add the provided DependencyInjection namespace and its classes to your project.
  • Add the Injector Component: Attach the Injector MonoBehaviour to a GameObject in your scene.
  • Define Providers: Create provider MonoBehaviours and attach them to GameObjects.
  • Mark Providers: Use [Provide] in your MonoBehaviours to provide a dependency of a particular type.
  • Mark Dependencies: Use [Inject] in your MonoBehaviours to satifsy dependencies.

YouTube

Watch the tutorial video here

You can also check out my YouTube channel for more Unity content.

Inspired by

This project takes inspiration from the following open source projects:

unity-dependency-injection-lite's People

Contributors

adammyhre avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.