Пример #1
0
        /// <summary>
        /// Loads game state from the specified save slot.
        /// Will reset the engine services and unload unused assets before load.
        /// </summary>
        public async Task <GameStateMap> LoadGameAsync(string slotId)
        {
            if (string.IsNullOrEmpty(slotId) || !GameStateSlotManager.SaveSlotExists(slotId))
            {
                Debug.LogError($"Slot '{slotId}' not found when loading '{typeof(GameStateMap)}' data.");
                return(null);
            }

            var quick = slotId.EqualsFast(LastQuickSaveSlotId);

            OnGameLoadStarted?.Invoke(new GameSaveLoadArgs(slotId, quick));

            if (config.LoadStartDelay > 0)
            {
                await new WaitForSeconds(config.LoadStartDelay);
            }

            Engine.Reset();
            await Resources.UnloadUnusedAssets();

            var state = await GameStateSlotManager.LoadAsync(slotId) as GameStateMap;

            await LoadAllServicesFromStateAsync <IStatefulService <GameStateMap>, GameStateMap>(state);

            if (config.StateRollbackMode == StateRollbackMode.Full && config.SavedRollbackSteps > 0)
            {
                rollbackStateStack.OverrideFromJson(state.RollbackStackJson);
            }

            foreach (var task in onGameDeserializeTasks)
            {
                await task(state);
            }

            OnGameLoadFinished?.Invoke(new GameSaveLoadArgs(slotId, quick));

            return(state);
        }
Пример #2
0
        public async UniTask <GameStateMap> LoadGameAsync(string slotId)
        {
            if (string.IsNullOrEmpty(slotId) || !GameStateSlotManager.SaveSlotExists(slotId))
            {
                Debug.LogError($"Slot '{slotId}' not found when loading '{typeof(GameStateMap)}' data.");
                return(null);
            }

            var quick = slotId.EqualsFast(LastQuickSaveSlotId);

            OnGameLoadStarted?.Invoke(new GameSaveLoadArgs(slotId, quick));

            if (Configuration.LoadStartDelay > 0)
            {
                await UniTask.Delay(TimeSpan.FromSeconds(Configuration.LoadStartDelay));
            }

            Engine.Reset();
            await Resources.UnloadUnusedAssets();

            var state = await GameStateSlotManager.LoadAsync(slotId) as GameStateMap;

            await LoadAllServicesFromStateAsync <IStatefulService <GameStateMap>, GameStateMap>(state);

            if (Configuration.EnableStateRollback && Configuration.SavedRollbackSteps > 0)
            {
                rollbackStack.OverrideFromJson(state.RollbackStackJson, s => s.AllowPlayerRollback());
            }

            for (int i = onGameDeserializeTasks.Count - 1; i >= 0; i--)
            {
                await onGameDeserializeTasks[i](state);
            }

            OnGameLoadFinished?.Invoke(new GameSaveLoadArgs(slotId, quick));

            return(state);
        }