Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Other UniInject - Dependency Injection for Unity

Discussion in 'Scripting' started by achimmihca, Nov 25, 2022.

  1. achimmihca

    achimmihca

    Joined:
    Feb 13, 2016
    Posts:
    160
    I created a small Dependency Injection library for Unity call UniInject.
    Find it on GitHub (MIT license): https://github.com/achimmihca/UniInject

    Why Dependency Injection?
    • Better separation of concerns and loose coupling, which leads to composable software and code reuse
    • Better testability
    • Less boilerplate code to get instances
    Dependency injection (DI) is a general concept in object oriented programming.
    If you are new to the topic, then I recommend you this introduction.
    You might also be interested in the introduction from the Zenject library, which is another DI library for Unity.

    Why UniInject?

    The goal of UniInject is to unify the handling of getting an instance.

    In Unity you can wire up your object's dependencies in the Editor's inspector window.
    Furthermore, there are GetComponent or GetComponentInChildren.
    Using UIToolkit, you also have visualElement.Query to get instances.

    In UniInject, the same Inject-annotation can be used to get an instance
    • from custom bindings
    • from components (e.g. GetComponent, GetComponentInChildren)
    • from VisualElements (when using Unity's new UIToolkit / UXML / UIDocument)
    Features
    • Field, property, method and constructor injection
    • Cyclic dependencies are handled (except for constructor injection)
    • Optional injection
      • Marking something as optional will not throw an Exception when no value is present
    • Custom key for injection
      • The default is the type of the field that should be injected
    • Hierarchy of different injection contexts (e.g. for Player1 and Player2)
    • Scene injection is finished after the Awake() method, such that the injected values can be used in the Start() and OnEnable() methods for further setup logic
    • The values of GetComponent, GetComponentInChildren, etc. are mockable.
      • Thus, for tests the scene hierarchy can be simulated.
    • Custom bindings can be created using normal MonoBehaviours that implement the IBinder interface
    • Static validation in Edit Mode that there is a value for every symbol, which should be injected
    • Mark fields that are set in the inspector via the InjectedInInspector-annotation.
      • It makes the origin of values easier to grasp.
      • The static validation can check that a non-null value has been set in such an annotated field.
    • Calling injection methods is also possible in edit-mode (e.g. calling SceneInjectionManager.DoInjection())
    • UniInject provides you with tools for DI that you can adapt for your own needs.
      • Build upon the given logic to change when, how, and what is injected.
      • The included SceneInjectionManager is a good starting point for inspiration.
    Examples
    Can be found here: https://github.com/achimmihca/UniInject#get-an-instance-that-has-been-bound
     
    Last edited: Nov 25, 2022
  2. achimmihca

    achimmihca

    Joined:
    Feb 13, 2016
    Posts:
    160
    UniInject was originally created for UltraStar Play.

    The project is doing a Kickstarter now.
    An investment in UltraStar Play is also an investment in open source solutions for Unity.

    Besides UniInject, the development of UltraStar Play has led to libraries for translation, scene change animation, and a simple HTTP server library for Unity.

    But there are even more interesting parts of UltraStar Play that could be turned into reusable libraries, for example the automatic smartphone connection when in the same local area network, real-time microphone recording and playback, or Material Design and Bootstrap Icons for UIToolkit.

    Plus, you can learn by example on this project. For example, a successful Kickstarter will lead to a demo how online multiplayer can be implemented.

    Thus, supporting the project is good for the game itself and you as a Unity dev.
    Back it on Kickstarter