Giter VIP home page Giter VIP logo

unity-injector's Introduction

Dependency Injection library for Unity3D

A simple but powerful full dependency Injection library for Unity3D

Features

  • Injection
    • Supports for normal C# classes , GameObject and MonoBehaviours
    • Singleton and factory
  • Optional dependencies

How to set up

using UnityEngine;
using JohanPolosn.UnityInjector;

static class AppInit
{
    [RuntimeInitializeOnLoadMethod]
    static private void Init()
    {
        var injector = new DependencyInjector(); // the new Global Injector
        var dependencys = injector.Dependencys;
        
        // in order to use InjectOnStart , see "How to injet a MonoBehaviour in a scene or in a prefab"
        GlobalInjector.singleton = injector;

        dependencys.AddSingleton(new ApiService());

        dependencys.AddSingleton(injector.Inject(new GameService()));
    }
}

public class ApiService
{
}

public class GameService
{
    private ApiService api;
    public void Construct(ApiService api)
    {
        this.api = api;
    }
}

How to injet a MonoBehaviour in a scene or in a prefab

Example MonoBehaviour to inject.

public class Player : MonoBehaviour
{
    private ApiService api;
    public void Construct(ApiService api)
    {
        this.api = api;
    }
}
  • Add "Player" to a GameObject in scene or in a prefab.
  • Add a "InjectOnStart" to the same GameObject , ComponentMenu -> "Unity Injector/Inject On Start"

And that's it ! Construct on Player will be called at the same time as Start

unity-injector's People

Contributors

johanpolson 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.