在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:hadashiA/VContainer开源软件地址:https://github.com/hadashiA/VContainer开源编程语言:C# 94.9%开源软件介绍:VContainerThe extra fast DI (Dependency Injection) library running on Unity Game Engine. "V" means making Unity's initial "U" more thinner and solid ... !
Features
DocumentationVisit vcontainer.hadashikick.jp to view the full documentation. PerformanceGC Alloc Result ExampleInstallationRequires Unity 2018.4+ Install via UPM (using Git URL)
Install via OpenUPM
Install manually (using .unitypackage)
Basic UsageFirst, create a scope. References are automatically resolved for types registered here. public class GameLifetimeScope : LifetimeScope
{
public override void Configure(IContainerBuilder builder)
{
builder.RegisterEntryPoint<ActorPresenter>();
builder.Register<CharacterService>(Lifetime.Scoped);
builder.Register<IRouteSearch, AStarRouteSearch>(Lifetime.Singleton);
builder.RegisterComponentInHierarchy<ActorsView>();
}
} Where definitions of classes are public interface IRouteSearch
{
}
public class AStarRouteSearch : IRouteSearch
{
}
public class CharacterService
{
readonly IRouteSearch routeSearch;
public CharacterService(IRouteSearch routeSearch)
{
this.routeSearch = routeSearch;
}
} public class ActorsView : MonoBehaviour
{
} and public class ActorPresenter : IStartable
{
readonly CharacterService service;
readonly ActorsView actorsView;
public ActorPresenter(
CharacterService service,
ActorsView actorsView)
{
this.service = service;
this.actorsView = actorsView;
}
void IStartable.Start()
{
// Scheduled at Start () on VContainer's own PlayerLoopSystem.
}
}
Flexible Scoping with asyncLifetimeScope can dynamically create children. This allows you to deal with the asynchronous resource loading that often occurs in games. public void LoadLevel()
{
// ... Loading some assets
// Create a child scope
instantScope = currentScope.CreateChild();
// Create a child scope with LifetimeScope prefab
instantScope = currentScope.CreateChildFromPrefab(lifetimeScopePrefab);
// Create a child with additional registration
instantScope = currentScope.CreateChildFromPrefab(
lifetimeScopePrefab,
builder =>
{
// Extra Registrations ...
});
instantScope = currentScope.CreateChild(builder =>
{
// ExtraRegistrations ...
});
instantScope = currentScope.CreateChild(extraInstaller);
}
public void UnloadLevel()
{
instantScope.Dispose();
} In addition, you can create a parent-child relationship with LifetimeScope in an Additive scene. class SceneLoader
{
readonly LifetimeScope currentScope;
public SceneLoader(LifetimeScope currentScope)
{
this.currentScope = currentScope; // Inject the LifetimeScope to which this class belongs
}
IEnumerator LoadSceneAsync()
{
// LifetimeScope generated in this block will be parented by `this.lifetimeScope`
using (LifetimeScope.EnqueueParent(currentScope))
{
// If this scene has a LifetimeScope, its parent will be `parent`.
var loading = SceneManager.LoadSceneAsync("...", LoadSceneMode.Additive);
while (!loading.isDone)
{
yield return null;
}
}
}
// UniTask example
async UniTask LoadSceneAsync()
{
using (LifetimeScope.EnqueueParent(parent))
{
await SceneManager.LoadSceneAsync("...", LoadSceneMode.Additive);
}
}
} // LifetimeScopes generated during this block will be additionally Registered.
using (LifetimeScope.Enqueue(builder =>
{
// Register for the next scene not yet loaded
builder.RegisterInstance(extraInstance);
}))
{
// Loading the scene..
} See scoping for more information. UniTaskpublic class FooController : IAsyncStartable
{
public async UniTask StartAsync(CancellationToken cancellation)
{
await LoadSomethingAsync(cancellation);
await ...
...
}
} builder.RegisterEntryPoint<FooController>(); See integrations for more information. Diagnostics WindowSee diagnostics for more information. CreditsVContainer is inspired by: AuthorLicenseMIT |
2022-08-15
2022-08-17
2022-09-23
2023-10-27
2022-08-18
请发表评论