Class GameInstance
A high-level singleton-style manager for the current game session.
[DisallowMultipleComponent]
[RequireComponent(typeof(SceneLoader))]
public class GameInstance : MonoBehaviour, IUnityGameService
Inheritance
Implements
Examples
A scene-local data handler hooks into the pre-transition flush so its payload is uploaded before LoadNextLevel unloads the scene. The callback passed to Action}) must be invoked when the upload is finished; GameInstance will not load the next scene until every registered callback has signalled completion.
// MyDataHandler.cs — attach to a GameObject in the scene alongside GameMode.
public class MyDataHandler : MonoBehaviour
{
private GameMode _gameMode;
private void OnEnable()
{
// GameMode relays the event to GameInstance, so subscribe here rather than
// fetching GameInstance directly.
if (UnityServiceLocator.Current.TryGet(out _gameMode))
_gameMode.SubscribleDataSend(SendToServer);
}
private void OnDisable()
{
_gameMode?.UnsubscribleDataSend(SendToServer);
}
// GameInstance fires this via GameMode just before the scene is unloaded.
// 'callback' MUST be called once the data send is complete so the transition
// sequence inside LoadLevelSequenceCoroutine can proceed.
private void SendToServer(Action callback)
{
StartCoroutine(UploadCoroutine(callback));
}
private IEnumerator UploadCoroutine(Action callback)
{
yield return StartCoroutine(MyNetworkHelper.PostData(GetPayload()));
callback?.Invoke(); // unblocks GameInstance.LoadLevelSequenceCoroutine
}
}
Remarks
The GameInstance is instantiated once as part of the GameInstance Prefab and persists for the lifetime of the game (until the application is closed or reloaded). An instance of GameInstance is created in the initial scene, but the same instance will persist across scene loads.
It is responsible for:
- Initializing global session data.
- Handling level loading (next, current, or specified scene).
- Invoking error handling callbacks.
- Sending data to a server before scene transitions.
GameInstance connects scene-local logic (like GameMode) to global, session-wide functionality, such as tracking player progress, handling errors, and quitting the game.
Fields
ErrorMessageThrown
Message received when an error occurs during initialization (such as a failed session fetch). The error message is passed for display or handling.
[NonSerialized]
public string ErrorMessageThrown
Field Value
string
SendDataToServerEvent
An event that allows systems to send data to a server before a level transition. Listeners must invoke the provided callback when their data send is complete.
public Action<Action> SendDataToServerEvent
Field Value
Properties
CurrentSessionInfo
Returns the current runtime session info if available.
public SessionInfo CurrentSessionInfo { get; }
Property Value
IsLoadingMap
True if a map is currently loading, false otherwise.
public bool IsLoadingMap { get; }
Property Value
bool
Methods
LoadAssetReferenceScene(AssetReference)
Loads a scene from an asset reference.
public void LoadAssetReferenceScene(AssetReference assetLevel)
Parameters
| Name | Type | Description |
|---|---|---|
assetLevel | AssetReference |
LoadCurrentLevel()
Reloads the current level based on the active SceneGraph state.
public void LoadCurrentLevel()
LoadNextLevel()
Loads the next level as defined by the configured SceneGraph.
public void LoadNextLevel()