Skip to main content
Attribute: [Inject]Assets/_Molca/_Core/Attributes/InjectAttribute.cs
Resolver: RuntimeManager

Behaviour fields

MonoBehaviour fields and properties marked [Inject] are filled after runtime initialization when the manager scans the scene.

Dynamic objects

  • RuntimeManager.InjectDependencies(instance)
  • RuntimeManager.CreateWithInjection<T>() for types with constructor injection

Constructor injection

Supported for non-Unity types created through the injection factory (see RuntimeManager source).

Example: consumer MonoBehaviour

Scene objects are discovered after RuntimeManager finishes startup; fields stay null in Awake until injection runs. For anything in Start or later, await RuntimeManager.WaitForInitialization() first (see also Sequence controller for the same pattern on EventDispatcher and ReferenceManager).
using UnityEngine;
using Molca;
using Molca.Events;
using Molca.ReferenceSystem;

public class MyFeatureController : MonoBehaviour
{
    [SerializeField] private string displayName = "Feature";

    // Filled automatically once RuntimeManager has registered services and scanned the scene.
    [Inject] private EventDispatcher _eventDispatcher;
    [Inject] private ReferenceManager _referenceManager;

    // Optional: allow missing service without logging an error (default Required = true).
    // [Inject(Required = false)] private IOptionalSubsystem _optional;

    private async void Start()
    {
        await RuntimeManager.WaitForInitialization();

        if (_eventDispatcher == null || _referenceManager == null)
        {
            Debug.LogError($"{nameof(MyFeatureController)}: injection failed; check RuntimeManager and registrations.");
            return;
        }

        // Use injected services from here on.
    }
}

Objects created at runtime

AddComponent / Instantiate does not get the automatic scene pass. Call RuntimeManager.InjectInto(component) (or InjectDependencies on the instance) after the object exists and RuntimeManager.IsReady is true.
var go = new GameObject("RuntimeWorker");
var worker = go.AddComponent<MyFeatureController>();
RuntimeManager.InjectInto(worker);