privatevoidAwake() { // Check if there isn't an instance if (instance == null) { instance = this; } // Check if there is an instance elseif (instance != this) { Destroy(gameObject); return; } // Set this Managers instance Don't Destroy on Load DontDestroyOnLoad(gameObject);
// Get Managers Instances Data = GetComponent<DataManager>(); Inventory = GetComponent<InventoryManager>(); Player = GetComponent<PlayerManager>();
// Add Managers to Load Sequeue _startSequeue = new List<IGameManager> { Player, Inventory, Data }; // Start Load Managers StartCoroutine(StartupManagers()); }
private IEnumerator StartupManagers() { // Call Startup() of Every Manager foreach (var manager in _startSequeue) { manager.Startup(); } // Wait for Next Frame yieldreturnnull;
// Check the number of Managers int numModules = _startSequeue.Count, numReady = 0; // While not Loaded while (numReady < numModules) { int lastReady = numReady; numReady = 0; // Get the Status of Managers foreach (var manager in _startSequeue) { if (manager.status == ManagerStatus.Started) { numReady++; } }
// If undone if (numReady > lastReady) { Debug.Log("Progress: " + numReady + "/" + numModules); // Wait for Next Frame yieldreturnnull; } } // Complete Debug.Log("All managers started up"); } }
_savepath = Path.Combine(Application.persistentDataPath, SaveName); Debug.Log("Set the " + SaveName + " in " + _savepath);
status = ManagerStatus.Started; }
[ContextMenu("Save")] publicvoidSave() { // Dic of objects which should be saved Dictionary<string, object> gamestate = new Dictionary<string, object> { { "inventory", Managers.Inventory.GetData() }, { "health", Managers.Player.health }, { "maxHealth", Managers.Player.maxHealth } };
// Format the serializable object to stream FileStream stream = File.Create(_savepath); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, gamestate); stream.Close(); }