Easy Code For Vivox
  • Introduction
    • Official Links
    • Getting Started
      • Wheres The Code
      • Setup EasyCode in My Project
    • Setup Demo Scenes
      • Vivox Developer Portal
      • Unity Gaming Services (UGS) Dashboard
    • Things To Consider
    • FAQ
  • Development Info
    • Design Decisions
    • Release Notes
      • v2.0
      • v1.3
      • v1.2
      • v1.1
      • Template
    • Not Supported
  • Easy Code For Vivox
    • How do I setup EasyCode?
      • Setup Your Credentials
    • How do I do this in EasyCode?
      • Login / Logout
        • Login Properties
        • SetTransmissionMode
        • Login Events
      • Join / Leave Channel
        • Audio Channel
          • Audio Channel Events
        • Text Channel
          • Text Channel Events
        • Channel Events
      • Send Messages
        • Message Events
      • Mute / Unmute
        • Mute / Unmute Events
      • Subscribe to User Events
      • Volume / Audio Settings
        • Audio Device Events
      • Text To Speech ( TTS )
        • TTS Events
    • Vivox Access Tokens
      • Unity Cloud Code
    • Supported Vivox Events
      • Callback Methods
    • Folder Structure / Info
      • / Demo Scenes /
      • / Documentation /
      • / Examples /
      • / Plugins /
      • / Resources /
      • / Scripts /
      • / Settings /
    • Common Errors
  • API Info
    • EasySession.cs
    • EasyManager.cs
      • Main Methods
      • Vivox Event Callbacks
    • Easy3DPositional.cs
    • EasyVivoxUtilities.cs
    • EasySettings.cs
    • Extension Methods
      • EasySIPExtensions.cs
      • GameObjectExtensions.cs
      • TTSMessageExtensions.cs
      • UIExtensions.cs
      • VivoxExtensions.cs
      • EasyDebug.cs
    • EasyEvents.cs
  • Dependency Injection
    • Zenject vs Extenject
    • Install Dependencies
    • Inject Classes
  • Dynamic Events
    • Dynamic Events
    • Tests
    • Gotchas
    • Dynamic Async Events
      • Dont Do
    • Event Examples
  • Related Info
    • How do I do this in Vivox?
      • Conference Chat
    • Pre-Processor Directives
    • How to set iOS Info.plist for Unity?
    • Unity Gaming Services
  • The Future
    • Roadmap
    • Todo / Notes / Changelog
Powered by GitBook
On this page
  • Constructor Injection
  • Method Injection
  • 1. Have a private variable that is assigned via method injection and used throughout your class. Injected only once.
  • 2. Inject the class into every method that needs. Injected into every class with [Inject] attribute

Was this helpful?

  1. Dependency Injection

Inject Classes

Use Zenject Dependency Injection

PreviousInstall DependenciesNextDynamic Events

Last updated 2 years ago

Was this helpful?

There are different ways to inject classes using dependency injection. Zenject provides constructor, field, property, and method injection.

It is recommended to only use Constructor and Method Injection.

Constructor Injection

Most of your scripts will probably inherit form MonoBehaviour so you will not be able to use constructor injection. If your class is a service or helper class that does not inherit MonoBehaviour then you can use constructor injection.

internal class DependencyInjectionExample
{
    private EasyEvents _events;

    public DependencyInjectionExample(EasyEvents events)
    {
        _events = events;
    }
}

Zenject dependency Injection will automatically provide an instance of EasyEvents into the DependencyInjectionExample class

Zenject will inject dependencies during Unity'sAwake() phase. Recommended to access the injected dependencies in the Start()method instead of Awake()

Method Injection

Most of your scripts will probably inherit form MonoBehaviour so you will not be able to use constructor injection. In that case you will need to use Method injection. There 2 ways that you can use Method Injection.

1. Have a private variable that is assigned via method injection and used throughout your class. Injected only once.

I have named the method Initialize() but you can use whatever method name you want. I have seen other people use the naming convention Constructor() since it acts like a constructor for classes that inherit from MonoBehaviour

internal class DependencyInjectionExample : MonoBehaviour
{
    private EasyEvents _events;

    [Inject]
    private void Initialize(EasyEvents events)
    {
        _events = events;
    }
    
    public void AddEvent()
    {
        _events.LoggedIn += OnLoggedIn;
    }

    public void RemoveEvent()
    {
        _events.LoggedIn -= OnLoggedIn;
    }

    private void OnLoggedIn(ILoginSession loginSession)
    {
        Debug.Log($"User {loginSession.LoginSessionId.DisplayName} has logged in");
    }
}

2. Inject the class into every method that needs. Injected into every class with [Inject] attribute

internal class DependencyInjectionExample : MonoBehaviour
{
    [Inject]
    public void AddEvent(EasyEvents events)
    {
        events.LoggedIn += OnLoggedIn;
    }

    [Inject]
    public void RemoveEvent(EasyEvents events)
    {
        events.LoggedIn -= OnLoggedIn;
    }

    private void OnLoggedIn(ILoginSession loginSession)
    {
        Debug.Log($"User {loginSession.LoginSessionId.DisplayName} has logged in");
    }
    
    [Inject]
    private void AudioSettings(EasyAudio audio)
    {
        audio.AdjustLocalPlayerAudioVolume(25, EasySession.Client);
    }
}
Read more here