This document outlines the core concepts, scenes, and services involved in the app lifecycle, as well as the order of events during startup and session management.
Core Components
Standard Scenes
Lobby Scene
- Entry point for clients
- Allows users to view active sessions, create new ones, and select content
Session Scene
- Primary scene for training simulations
- Contains elements like player rig, UI, and runtime services
- Servers start directly in this scene and await client connections
Key Services
AppLifecycleService
Handles transitions between Lobby and Session scenes, manages ECS worlds, and provides methods for starting different types of sessions.
LocalPlayerInfoService
Carries player information such as selected content scene between Lobby and Session scenes.
Lifecycle Flow
Application Startup
Startup.cs configures the app and registers services
NetCodeBootstrap.cs runs:
- For server builds: Starts a server-hosted session using
AppLifecycleService
- For client builds or editor:
- Creates offline client+server NetCode worlds
- Allows
AppLifecycleService to be triggered for starting or joining sessions
Scene Transitions
Lobby to Session
- Triggered when a session is selected in the Lobby
AppLifecycleService manages the transition
- Selected content scene info is carried over via
LocalPlayerInfoService
Session to Lobby
- Allows clients to return to Lobby to join or create new sessions
Content Scene Loading
- Content scenes are loaded additively during a session
- Selected in Lobby and carried over to Session using
LocalPlayerInfoService
- For details, see the Loading Content Scenes page
Usage Example
Here's a basic example of how to use the AppLifecycleService:
using Kanda.Foundation.DependencyInjection;
using Unity.Entities.Content;
using UnityEngine;
public class AppLifecycleExample : MonoBehaviour
{
[SerializeField] private string _entitySceneName;
[SerializeField] private WeakObjectSceneReference _entitySceneReference;
private void Awake()
{
_localPlayerInfoService.SetSelectedContentScene(new ContentSceneMetadata
{
Name = _entitySceneName,
Guid = _entitySceneReference.Id.GlobalId.AssetGUID
});
}
public async void GoToLobby()
{
var result = await _appLifecycleService.
Lobby.Go();
Debug.Log(result.Succeeded ? "Went to lobby successfully" : "Failed to go to lobby: " + result.Message);
}
public async void StartOfflineSession()
{
var result = await _appLifecycleService.Offline.Host();
Debug.Log(result.Succeeded ? "Started offline session successfully" : "Failed to start offline session: " + result.Message);
}
}
A service which handles application lifecycle, moving between lobby and sessions by loading scenes an...
Definition AppLifecycleService.cs:11
ILobbyStrategy Lobby
Definition AppLifecycleService.cs:13
A service which stores local player information for consumption throughout the application lifecycle....
Definition LocalPlayerInfoService.cs:13
Definition AppLifecycleResult.cs:5
Definition AppEventUtils.cs:5
Session Strategies
The AppLifecycleService uses different strategies for managing various session types. These strategies can be overridden to customize behavior for specific needs.
Strategy Types
ILobbyStrategy: Handles transitioning to the lobby.
IOfflineSessionStrategy: Manages offline (single-player) sessions.
ILanSessionStrategy: Handles client-hosted sessions using local network.
IRelaySessionStrategy: Handles client-hosted session using a relay.
IServerSessionHostingStrategy: Manages dedicated server-hosted sessions.
IServerSessionJoiningStrategy: Handles joining server-hosted sessions.
For more details on these strategies, see Session Hosting Modes.