unity-vcontainer
Unity VContainer - High-Performance DI for Unity
Overview
VContainer is a high-performance IoC container for Unity, providing dependency injection patterns for testable and maintainable code.
Core Topics:
- Constructor and method injection
- Service registration patterns (Singleton, Transient, Scoped)
- LifetimeScope hierarchies
- MonoBehaviour injection
- Factory patterns with DI
- Testing with mocks
Foundation Required: unity-csharp-fundamentals (TryGetComponent, FindAnyObjectByType, null-safe coding)
Learning Path: DI fundamentals → VContainer basics → Advanced patterns → Testing
Quick Start
using VContainer;
using VContainer.Unity;
// Define service interface
public interface IPlayerService
{
void Initialize();
}
// Implement service
public class PlayerService : IPlayerService
{
public void Initialize() => Debug.Log("Player initialized");
}
// Setup LifetimeScope
public class GameLifetimeScope : LifetimeScope
{
protected override void Configure(IContainerBuilder builder)
{
builder.Register<IPlayerService, PlayerService>(Lifetime.Singleton);
builder.RegisterComponentInHierarchy<PlayerController>();
}
}
// Inject into MonoBehaviour
public class PlayerController : MonoBehaviour
{
[Inject] private readonly IPlayerService mPlayerService;
void Start() => mPlayerService.Initialize();
}
Key Concepts
Lifetime Scopes
- Singleton: One instance per container
- Transient: New instance every resolve
- Scoped: One instance per scope
Injection Types
- Constructor Injection: Preferred for required dependencies
- Method Injection: For optional dependencies
- Property/Field Injection: Use
[Inject]attribute
Reference Documentation
VContainer Best Practices
Core DI patterns:
- Registration patterns and lifetime management
- LifetimeScope hierarchies
- Testing with mock dependencies
VContainer Integration Patterns
Advanced integrations:
- MVVM with reactive properties
- Cross-framework integration patterns
Best Practices
- Register interfaces: Loose coupling and testability
- Constructor injection first: Explicit dependencies
- Avoid Service Locator: Don't resolve in Update loops
- Test with mocks: Use ContainerBuilder in tests
- Clear hierarchies: Root → Scene → Local scopes
More from creator-hian/claude-code-plugins
unity-networking
Implement multiplayer games with Unity Netcode, Mirror, or Photon. Masters client-server architecture, state synchronization, and lag compensation. Use for multiplayer features, networking issues, or real-time synchronization.
18unity-unitask
UniTask library expert specializing in allocation-free async/await patterns, coroutine migration, and Unity-optimized asynchronous programming. Masters UniTask performance optimizations, cancellation handling, and memory-efficient async operations. Use PROACTIVELY for UniTask implementation, async optimization, or coroutine replacement.
15unity-mobile
Optimize Unity games for mobile platforms with IL2CPP, platform-specific code, and memory management. Masters iOS/Android deployment, app size reduction, and battery optimization. Use for mobile builds, platform issues, or device-specific optimization.
13unity-csharp-fundamentals
Unity C# fundamental patterns including TryGetComponent, SerializeField, RequireComponent, and safe coding practices. Essential patterns for robust Unity development. Use PROACTIVELY for any Unity C# code to ensure best practices.
11unity-ui
Build and optimize Unity UI with UI Toolkit and UGUI. Masters responsive layouts, event systems, and performance optimization. Use for UI implementation, Canvas optimization, or cross-platform UI challenges.
11csharp-code-style
C# code style and naming conventions based on POCU standards. Covers naming rules (mPascalCase for private, bBoolean prefix, EEnum prefix), code organization, C# 9.0 patterns. Use PROACTIVELY for C# code reviews, refactoring, or establishing project standards.
11