Skip to main content

ServiceLocator

Without VContainer Only

ServiceLocator is excluded from compilation when ACHENGINE_VCONTAINER is defined (i.e., when VContainer is installed). Use [Inject] in VContainer projects.

ServiceLocator is a static facade for resolving services by type at runtime — available only when VContainer is not installed.

API

namespace AchEngine.DI
{
public static class ServiceLocator
{
// Whether the container is ready
public static bool IsReady { get; }

// Resolve a service (throws InvalidOperationException if missing)
public static T Resolve<T>();

// Try to resolve safely (returns false if missing)
public static bool TryResolve<T>(out T result);
}
}

Usage Example

// Basic lookup
var ui = ServiceLocator.Resolve<IUIService>();
ui.Show<MainMenuView>();

// Safe lookup
if (ServiceLocator.TryResolve<IAudioService>(out var audio))
{
audio.PlayBGM("main_theme");
}

// Readiness check
if (!ServiceLocator.IsReady)
{
Debug.LogWarning("The service container has not been initialized yet.");
return;
}

[Inject] vs ServiceLocator

[Inject]ServiceLocator
VContainer✅ Required❌ Only available without VContainer
Usage locationObjects created by the DI containerAnywhere (no-VContainer env)
Recommended forAll services and viewsMonoBehaviour without VContainer
TestabilityHighMedium

Manual Setup (Without VContainer)

When running without VContainer, ServiceLocator is wired up internally by the engine's bootstrap layer — there is no public Setup() API available to user code.

Non-VContainer builds

Build without the ACHENGINE_VCONTAINER symbol, implement a custom AchEngineInstaller, and register services via IServiceBuilder. They will then be accessible at runtime via ServiceLocator.Resolve<T>().