// These could be threaded but the Unity web platform doesn't support System.Thread and all platforms warn when // reading and writing Json from a background thread despite seeming to work correctly. private static void Read(LoadInformation info, string filename, SaveData data) { try { var json = File.ReadAllText(filename); JsonUtility.FromJsonOverwrite(json, data); } catch (Exception e) { info.SetError(string.Format(_invalidLoadError, filename, e.Message)); } }
private IEnumerator Load(LoadInformation info, GameSaveData game, string filename) { info.UpdateProgress(LoadState.ReadingData, 0.0f); var data = new SaveData(); if (!string.IsNullOrEmpty(filename)) { Read(info, filename, data); } if (info.State == LoadState.Error) { yield break; } game = game ?? data.Game; var worldData = data.World ?? new WorldSaveData(); var playerData = data.Player ?? new PlayerSaveData(); info.UpdateProgress(LoadState.LoadingWorld, 0.1f); var mainLoader = string.IsNullOrEmpty(game.MainScene) ? null : SceneManager.LoadSceneAsync(game.MainScene); if (mainLoader == null) { info.SetError(string.Format(_missingMainSceneError, game.MainScene)); yield break; } while (!mainLoader.isDone) { yield return(null); } if (WorldManager.Instance == null) { info.SetError(string.Format(_missingWorldManagerError, game.MainScene)); yield break; } if (WorldManager.Instance.World == null) { info.SetError(string.Format(_missingWorldAssetError, game.MainScene)); yield break; } if (InstructionManager.Instance == null) { info.SetError(string.Format(_missingCompositionManagerError, game.MainScene)); yield break; } if (Player.Instance == null) { info.SetError(string.Format(_missingPlayerError, game.MainScene)); yield break; } var camera = ComponentHelper.GetComponentInScene <Camera>(WorldManager.Instance.gameObject.scene.buildIndex, false); if (camera == null) { info.SetError(string.Format(_missingCameraError, game.MainScene)); yield break; } var zone = WorldManager.Instance.World.GetZoneByName(game.StartingZone); if (zone == null) { info.SetError(string.Format(_invalidZoneError, game.StartingZone, game.MainScene)); yield break; } info.UpdateProgress(LoadState.LoadingUi, 0.2f); yield return(WorldManager.Instance.LoadUi()); info.UpdateProgress(LoadState.LoadingZones, 0.4f); WorldManager.Instance.Load(filename, worldData); Player.Instance.Load(playerData); WorldManager.Instance.TransitionZone(zone, game.PlayerSpawn, null); while (WorldManager.Instance.IsTransitioning) { yield return(null); } info.SetComplete(); }