The Framework
  • Introduction
    • What is The Framework
    • Core Objectives
    • Key Features
    • Architecture Overview
    • Benefits
    • Conclusion
  • Getting Started
    • Installation
  • Quick Setup
    • Unity Setup
    • Creating a Scene
    • Creating a Bootstrap
    • Creating a Canvas
  • API Reference
    • Scene Management
      • ICanvas
      • INavigationService
      • IDataService
      • IBootstrap
      • IAsyncScene
Powered by GitBook
On this page
  • Overview
  • Dependency Injection
  • Canvases
  • Best Practices
  1. Quick Setup

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.

public class GameBootstrap: AbstractBootstrap 
{
    // Using our D.I you can inject your dependencies
    [InjectField] IPlayerController _playerController;
    
    // Automatically fetch the correct framework canvas reference
    [FetchCanvas] IGameHud _hud;
 
    public override async Task OnBootstrapStartAsync()
    {
        await base.OnBootstrapStartAsync();
        
        // Dependencies are resolved here
        
        await _hud.Show();
    }
   
    public override async Task OnBootstrapStopAsync()
    {
        // Stop any action before this scene is destroyed
        await _hud.Hide();
    
    
        await base.OnBootstrapStopAsync();
    }
    
    // Navigate to a game over scene
    private async void GameOver() 
    {
        await _navigationService.NavigateAsync<IGameOverBootstrap>();
        await UnloadAsync();
    }
}
PreviousCreating a SceneNextCreating a Canvas

Last updated 6 months ago