Creating a Bootstrap
Overview
Creating a bootstrap provides a way to link a physical Unity scene to the framework's scene management system. Under the hood, all bootstraps should extend from AbstractBootstrap
, which acts as the base class for integrating with the framework.
A bootstrap is still a regular MonoBehaviour
, meaning it operates within Unity's GameObject and component system. This allows you to attach it to a GameObject in your scene and use familiar Unity features like Start
, Update
, and dependency injection provided by the framework.
Dependency Injection
When using the [InjectField]
attribute to inject dependencies, it’s important to note that these dependencies will only be resolved once the OnBootstrapStartAsync
method is called. This ensures that the bootstrap has been fully initialized within the framework and that all required services are available. Attempting to access dependencies before this point may result in null references or incomplete setup.
Canvases
Similarly, the [FetchCanvas]
attribute allows you to automatically resolve and bind canvas objects to fields. Canvases marked with [FetchCanvas]
will only be fetched and resolved after OnBootstrapStartAsync
has been executed. This ensures that the bootstrap has discovered and initialized all active canvases in the scene.
Best Practices
It is recommended to create a custom interface that implements IBootstrap
for each bootstrap you define. This approach ensures better code organization and makes it easier to navigate to a scene programmatically using its specific interface. For example, instead of relying on generic methods, you can directly access scene-specific functionality through the strongly-typed interface. This practice enhances code readability, reduces the risk of errors, and provides clear separation between different scenes and their associated logic.
Last updated