From 294cdf9a8e94826a1ac01b9dd6fcbee889229bfb Mon Sep 17 00:00:00 2001 From: minenice55 Date: Wed, 24 Jan 2024 00:17:35 -0500 Subject: [PATCH] fix certain types of sounds firing a frame late --- Assets/Scripts/Conductor.cs | 7 +-- Assets/Scripts/GameManager.cs | 58 ++++++++++++------- Assets/Scripts/Minigames.cs | 28 +++------ Assets/Scripts/Util/Sound.cs | 30 ++++++++-- ...Generator.Editor.ProjectSettingsData.asset | 3 + 5 files changed, 74 insertions(+), 52 deletions(-) diff --git a/Assets/Scripts/Conductor.cs b/Assets/Scripts/Conductor.cs index 11076f7f..1acff611 100644 --- a/Assets/Scripts/Conductor.cs +++ b/Assets/Scripts/Conductor.cs @@ -189,6 +189,7 @@ namespace HeavenStudio RiqBeatmap chart = GameManager.instance.Beatmap; double offset = chart.data.offset; double dspTime = AudioSettings.dspTime; + dspStart = dspTime; startPos = GetSongPosFromBeat(beat); firstBeatOffset = offset; @@ -213,10 +214,6 @@ namespace HeavenStudio musicSource.pitch = timelinePitch; Debug.Log($"playback scheduled for dsptime {dspStart}"); } - if (musicSource.clip == null) - { - dspStart = dspTime; - } songPosBeat = beat; startBeat = songPosBeat; @@ -404,7 +401,7 @@ namespace HeavenStudio } } - double MapTimeToPitchChanges(double time) + public double MapTimeToPitchChanges(double time) { double counter = 0; double lastChangeTime = 0; diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 8bdd8eef..821d0124 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -21,7 +21,7 @@ namespace HeavenStudio [Header("Lists")] [NonSerialized] public RiqBeatmap Beatmap = new(); - private List preloadedGames = new(); + private Dictionary cachedGamePrefabs = new(); [NonSerialized] public ObjectPool SoundObjects; [Header("Components")] @@ -1092,9 +1092,11 @@ namespace HeavenStudio { ResetCamera(); // resetting camera before setting new minigame so minigames can set camera values in their awake call - Rasmus - Destroy(currentGameO); + GameObject prefab = GetGame(game); + if (prefab == null) return; - currentGameO = Instantiate(GetGame(game)); + Destroy(currentGameO); + currentGameO = Instantiate(prefab); if (currentGameO.TryGetComponent(out var minigame)) { _currentMinigame = minigame; @@ -1112,6 +1114,7 @@ namespace HeavenStudio public void DestroyGame() { + cachedGamePrefabs.Clear(); SetGame("noGame"); } @@ -1152,29 +1155,40 @@ namespace HeavenStudio .Select(x => GetGameInfo(x)) .Where(x => x != null) .Where(x => !x.fxOnly) - .Select(x => x.LoadableName); - name = gameInfos.FirstOrDefault() ?? "noGame"; - } - else - { - if (gameInfo.usesAssetBundle) + .Where(x => x.LoadableName is not "noGame" or "" or null); + if (gameInfos.Count() > 0) { - //game is packed in an assetbundle, load from that instead - 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}"); - } + gameInfo = gameInfos.FirstOrDefault(); + if (gameInfo == null) return Resources.Load($"Games/noGame"); + } + else + { + return Resources.Load($"Games/noGame"); } } + if (gameInfo.usesAssetBundle) + { + //game is packed in an assetbundle, load from that instead + if (gameInfo.AssetsLoaded && gameInfo.LoadedPrefab != null) return gameInfo.LoadedPrefab; + // couldn't load cached prefab, try loading from assetbundle + try + { + Debug.LogWarning($"Game prefab wasn't cached, loading from assetbundle for game {name}"); + return gameInfo.LoadGamePrefab(); + } + catch (Exception e) + { + Debug.LogWarning($"Failed to load assetbundle for game {name}, using sync loading: {e.Message}"); + return Resources.Load($"Games/{name}"); + } + } + return Resources.Load($"Games/{name}"); + } + else + { + Debug.LogWarning($"Game {name} not found, using noGame"); + return Resources.Load($"Games/noGame"); } - return Resources.Load($"Games/{name}"); } public Minigames.Minigame GetGameInfo(string name) diff --git a/Assets/Scripts/Minigames.cs b/Assets/Scripts/Minigames.cs index 63e6d098..98542264 100644 --- a/Assets/Scripts/Minigames.cs +++ b/Assets/Scripts/Minigames.cs @@ -479,9 +479,7 @@ namespace HeavenStudio { if (AssetsLoaded || !usesAssetBundle) return; await UniTask.WhenAll(LoadCommonAssetBundleAsync(), LoadLocalizedAssetBundleAsync()); - await UniTask.WhenAll(LoadGamePrefabAsync()); - await UniTask.WhenAll(LoadCommonAudioClips()); - await UniTask.WhenAll(LoadLocalizedAudioClips()); + await UniTask.WhenAll(LoadGamePrefabAsync(), LoadCommonAudioClips(), LoadLocalizedAudioClips()); } public async UniTask LoadCommonAssetBundleAsync() @@ -547,6 +545,14 @@ namespace HeavenStudio } } + public GameObject LoadGamePrefab() + { + if (!usesAssetBundle) return null; + + loadedPrefab = GetCommonAssetBundle().LoadAsset(name); + return loadedPrefab; + } + public async UniTask LoadCommonAudioClips() { if (!commonLoaded) return; @@ -556,14 +562,6 @@ namespace HeavenStudio var assets = bundleCommon.LoadAllAssetsAsync(); await assets; - - // await UniTask.SwitchToThreadPool(); - // foreach (var asset in assets.allAssets) - // { - // AudioClip clip = asset as AudioClip; - // commonAudioClips.Add(clip.name, clip); - // } - // await UniTask.SwitchToMainThread(); } public async UniTask LoadLocalizedAudioClips() @@ -575,14 +573,6 @@ namespace HeavenStudio var assets = bundleLocalized.LoadAllAssetsAsync(); await assets; - - // await UniTask.SwitchToThreadPool(); - // foreach (var asset in assets.allAssets) - // { - // AudioClip clip = asset as AudioClip; - // localeAudioClips.Add(clip.name, clip); - // } - // await UniTask.SwitchToMainThread(); } public async UniTask UnloadAllAssets() diff --git a/Assets/Scripts/Util/Sound.cs b/Assets/Scripts/Util/Sound.cs index e41f88b3..5084bdba 100644 --- a/Assets/Scripts/Util/Sound.cs +++ b/Assets/Scripts/Util/Sound.cs @@ -70,6 +70,7 @@ namespace HeavenStudio.Util audioSource = GetComponent(); cond = Conductor.instance; + double dspTime = AudioSettings.dspTime; available = false; audioSource.clip = clip; @@ -85,8 +86,8 @@ namespace HeavenStudio.Util { playInstant = true; played = true; - startTime = AudioSettings.dspTime; - audioSource.PlayScheduled(startTime); + startTime = dspTime; + audioSource.Play(); } else { @@ -94,15 +95,17 @@ namespace HeavenStudio.Util if (cond != null) { scheduledPitch = cond.SongPitch; - startTime = (AudioSettings.dspTime + (cond.GetSongPosFromBeat(beat) - cond.songPositionAsDouble) / (double)scheduledPitch) - offset; + startTime = (dspTime + (cond.GetSongPosFromBeat(beat) - cond.songPositionAsDouble) / (double)scheduledPitch) - offset; } - if (scheduledPitch != 0 && AudioSettings.dspTime >= startTime) + if (scheduledPitch != 0 && dspTime >= startTime) { audioSource.PlayScheduled(startTime); + played = true; queued = true; } } + Update(); } private void Update() @@ -157,7 +160,7 @@ namespace HeavenStudio.Util audioSource.UnPause(); } scheduledPitch = cond.SongPitch; - startTime = (dspTime + (cond.GetSongPosFromBeat(beat) - cond.songPositionAsDouble) / (double)scheduledPitch); + startTime = (dspTime + (cond.GetSongPosFromBeat(beat) - cond.songPositionAsDouble) / (double)scheduledPitch) - offset; if (queued) audioSource.SetScheduledStartTime(startTime); } @@ -203,7 +206,7 @@ namespace HeavenStudio.Util { if (looping && loopEndBeat != -1) // Looping sounds play forever unless params are set. { - if (cond.songPositionInBeatsAsDouble > loopEndBeat) + if (cond.songPositionInBeatsAsDouble >= loopEndBeat) { KillLoop(fadeTime); loopDone = true; @@ -257,12 +260,22 @@ namespace HeavenStudio.Util { if (!gameObject.activeSelf) return; this.bendedPitch = bendedPitch; + if (bendTime == 0) + { + audioSource.pitch = bendedPitch; + return; + } StartCoroutine(BendUpLoop(bendTime)); } public void BendDown(float bendTime) { if (!gameObject.activeSelf) return; + if (bendTime == 0) + { + audioSource.pitch = pitch; + return; + } StartCoroutine(BendDownLoop(bendTime)); } @@ -301,6 +314,11 @@ namespace HeavenStudio.Util public void KillLoop(double fadeTime) { if (!gameObject.activeSelf) return; + if (fadeTime == 0) + { + GameManager.instance.SoundObjects.Release(this); + return; + } StartCoroutine(FadeLoop(fadeTime)); } diff --git a/ProjectSettings/SatorImaging.UnitySourceGenerator.Editor.ProjectSettingsData.asset b/ProjectSettings/SatorImaging.UnitySourceGenerator.Editor.ProjectSettingsData.asset index 03e0aa52..a7875bb4 100644 --- a/ProjectSettings/SatorImaging.UnitySourceGenerator.Editor.ProjectSettingsData.asset +++ b/ProjectSettings/SatorImaging.UnitySourceGenerator.Editor.ProjectSettingsData.asset @@ -48,5 +48,8 @@ MonoBehaviour: - Assets/Scripts/LevelEditor/Timeline/SpecialTmeline/SectionDialog.cs - Assets/Scripts/LevelEditor/Timeline/SpecialTmeline/TimelineObjs/SectionTimelineObj.cs - Assets/Scripts/Games/DJSchool/Student.cs + - Assets/Scripts/UI/PauseMenu.cs + - Assets/Scripts/Util/Sound.cs + - Assets/Scripts/Conductor.cs PathsToSkipImportEvent: [] PathsToIgnoreOverwriteSettingOnAttribute: []