using UnityEngine; /// /// singleton for a single source of truth game state and flow management /// public class GameManager : MonoBehaviour { /// /// singleton pattern: define instance field for accessing the singleton elsewhere /// public static GameManager Instance; /// /// the local player data object for storing player data /// private LocalPlayerData _localPlayerData; /// /// backend object for handling communication with the firebase backend /// public Backend Backend; /// /// ui manager object for handling ui state and flow /// public UIManager ui; /// /// enforces singleton behaviour; sets doesn't destroy on load and checks for multiple instances /// private void Awake() { // check if instance hasn't been set yet if (Instance == null) { Debug.Log("awake as singleton instance, setting self as the forever-alive instance"); Instance = this; DontDestroyOnLoad(gameObject); } // check if instance is already set and it's not this instance else if (Instance != null && Instance != this) { Debug.Log("awake as non-singleton instance, destroying self"); Destroy(gameObject); } } /// /// called when the game object is enabled, initialises variables /// private void OnEnable() { Backend = new Backend(); Backend.Initialise(); ui = UIManager.Instance; } private void Start() { // load the local player data and refresh the ui _localPlayerData = new LocalPlayerData(); _localPlayerData.LoadFromTheWorld(); // register a callback to refresh the ui when the player signs in Backend.RegisterOnSignInCallback(_ => { _localPlayerData.LoadFromTheWorld(); }); } /// /// called when the game object is disabled /// private void OnDestroy() { Backend.Cleanup(); } }