diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 5e1d88e2f..1fd42ff1a 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -182,7 +182,7 @@ namespace HeavenStudio { string game = Beatmap.Entities[0].datamodel.Split(0); SetCurrentGame(game); - SetGame(game); + StartCoroutine(WaitAndSetGame(game)); } else { @@ -310,7 +310,7 @@ namespace HeavenStudio { string game = Beatmap.Entities[0].datamodel.Split(0); SetCurrentGame(game); - SetGame(game); + StartCoroutine(WaitAndSetGame(game)); } else { @@ -387,8 +387,9 @@ namespace HeavenStudio return (bp == bLen); } - public void SeekAheadAndPreload(double start, float seekTime = 8f) + public List SeekAheadAndPreload(double start, float seekTime = 8f) { + List gamesToPreload = new(); List entitiesAtSameBeat = ListPool.Get(); Minigames.Minigame inf; @@ -401,6 +402,7 @@ namespace HeavenStudio inf = GetGameInfo(gameName); if (inf != null && inf.usesAssetBundle && !inf.AssetsLoaded) { + gamesToPreload.Add(inf); Debug.Log($"ASYNC loading assetbundles for game {gameName}"); inf.LoadAssetsAsync().Forget(); } @@ -426,6 +428,7 @@ namespace HeavenStudio inf = GetGameInfo(gameName); if (inf != null && inf.usesAssetBundle && !inf.AssetsLoaded) { + gamesToPreload.Add(inf); Debug.Log($"ASYNC loading assetbundles for game {gameName}"); inf.LoadAssetsAsync().Forget(); } @@ -433,8 +436,8 @@ namespace HeavenStudio } } } - ListPool.Release(entitiesAtSameBeat); + return gamesToPreload; } public void SeekAheadAndDoPreEvent(double start) @@ -738,6 +741,17 @@ namespace HeavenStudio WaitUntil yieldOverlays = new WaitUntil(() => OverlaysManager.OverlaysReady); WaitUntil yieldBeatmap = new WaitUntil(() => Beatmap != null && Beatmap.Entities.Count > 0); WaitUntil yieldAudio = new WaitUntil(() => AudioLoadDone || (ChartLoadError && !GlobalGameManager.IsShowingDialog)); + WaitUntil yieldGame = null; + List gamesToPreload = SeekAheadAndPreload(beat, 4f); + if (gamesToPreload.Count > 0) + { + yieldGame = new WaitUntil(() => gamesToPreload.All(x => x.AssetsLoaded)); + foreach (var game in gamesToPreload) + { + Debug.Log($"ASYNC loading assetbundles for game {game.LoadableName}"); + game.LoadAssetsAsync().Forget(); + } + } // wait for overlays to be ready yield return yieldOverlays; @@ -745,6 +759,9 @@ namespace HeavenStudio yield return yieldBeatmap; //wait for audio clip to be loaded yield return yieldAudio; + //wait for games to be loaded + if (yieldGame != null) + yield return yieldGame; SkillStarManager.instance.KillStar(); TimingAccuracyDisplay.instance.StopStarFlash(); @@ -1018,6 +1035,18 @@ namespace HeavenStudio SetCurrentGame(game, useMinigameColor); } + private IEnumerator WaitAndSetGame(string game, bool useMinigameColor = true) + { + var inf = GetGameInfo(game); + if (inf != null && inf.usesAssetBundle && !inf.AssetsLoaded) + { + Debug.Log($"ASYNC loading assetbundles for game {game}"); + inf.LoadAssetsAsync().Forget(); + yield return new WaitUntil(() => inf.AssetsLoaded); + } + SetGame(game, useMinigameColor); + } + public void PreloadGameSequences(string game) { var gameInfo = GetGameInfo(game); @@ -1047,12 +1076,18 @@ namespace HeavenStudio if (gameInfo.usesAssetBundle) { //game is packed in an assetbundle, load from that instead - if (gameInfo.LoadedPrefab != null) return gameInfo.LoadedPrefab; - // StartCoroutine(gameInfo.LoadCommonAudioClipsAsync()); - // StartCoroutine(gameInfo.LoadLocalizedAudioClipsAsync()); - return gameInfo.GetCommonAssetBundle().LoadAsset(name); + if (gameInfo.AssetsLoaded && gameInfo.LoadedPrefab != null) return gameInfo.LoadedPrefab; + + try + { + return gameInfo.GetCommonAssetBundle().LoadAsset(name); + } + catch (Exception e) + { + Debug.LogWarning($"Failed to load assetbundle for game {name}, using sync loading: {e.Message}"); + return Resources.Load($"Games/{name}"); + } } - name = gameInfo.LoadableName; } } return Resources.Load($"Games/{name}"); diff --git a/Assets/Scripts/Minigames.cs b/Assets/Scripts/Minigames.cs index aae4d6c49..b8e1faaf4 100644 --- a/Assets/Scripts/Minigames.cs +++ b/Assets/Scripts/Minigames.cs @@ -367,6 +367,13 @@ namespace HeavenStudio public AssetBundle GetLocalizedAssetBundle() { + if (bundleLocalized != null && !localeLoaded) + { + bundleLocalized.Unload(true); + bundleLocalized = null; + localeLoaded = false; + localePreloaded = false; + } if (!hasLocales) return null; if (!usesAssetBundle) return null; if (bundleLocalized == null || currentLoadedLocale != defaultLocale) //TEMPORARY: use the game's default locale until we add localization support @@ -382,6 +389,13 @@ namespace HeavenStudio public AssetBundle GetCommonAssetBundle() { + if (bundleCommon != null && !commonLoaded) + { + bundleCommon.Unload(true); + bundleCommon = null; + commonLoaded = false; + commonPreloaded = false; + } if (commonLoaded) return bundleCommon; if (!usesAssetBundle) return null; if (bundleCommon == null) @@ -404,6 +418,14 @@ namespace HeavenStudio public async UniTask LoadCommonAssetBundleAsync() { + if (bundleCommon != null && !commonLoaded) + { + await bundleCommon.UnloadAsync(true); + bundleCommon = null; + commonLoaded = false; + commonPreloaded = false; + } + if (commonPreloaded || commonLoaded) return; commonPreloaded = true; if (!usesAssetBundle) return; @@ -417,6 +439,14 @@ namespace HeavenStudio public async UniTask LoadLocalizedAssetBundleAsync() { + if (bundleLocalized != null && !localeLoaded) + { + await bundleLocalized.UnloadAsync(true); + bundleLocalized = null; + localeLoaded = false; + localePreloaded = false; + } + if (!hasLocales) return; if (localePreloaded) return; localePreloaded = true;