implement UniTask to replace some coroutines

This commit is contained in:
minenice55 2023-10-07 22:09:33 -04:00
parent 6adf6775db
commit ef783a1573
6 changed files with 115 additions and 85 deletions

View file

@ -1,8 +1,8 @@
using System; using System;
public static class AppInfo { public static class AppInfo {
public const string Version = "0.0.1002"; public const string Version = "0.0.1003";
public static readonly DateTime Date = new DateTime(2023, 09, 27, 22, 20, 38, 655, DateTimeKind.Utc); public static readonly DateTime Date = new DateTime(2023, 10, 08, 01, 35, 34, 715, DateTimeKind.Utc);
} }

View file

@ -10,6 +10,7 @@ using Jukebox;
using HeavenStudio.Util; using HeavenStudio.Util;
using HeavenStudio.Games; using HeavenStudio.Games;
using HeavenStudio.Common; using HeavenStudio.Common;
using Cysharp.Threading.Tasks;
namespace HeavenStudio namespace HeavenStudio
{ {
@ -339,15 +340,15 @@ namespace HeavenStudio
{ {
int aLen = a.Length; int aLen = a.Length;
int bLen = b.Length; int bLen = b.Length;
int ap = 0; int bp = 0; int ap = 0; int bp = 0;
while (ap < aLen && bp < bLen && a [ap] == b [bp]) while (ap < aLen && bp < bLen && a[ap] == b[bp])
{ {
ap++; ap++;
bp++; bp++;
} }
return (bp == bLen); return (bp == bLen);
} }
@ -366,8 +367,7 @@ namespace HeavenStudio
if (inf != null && inf.usesAssetBundle && !inf.AssetsLoaded) if (inf != null && inf.usesAssetBundle && !inf.AssetsLoaded)
{ {
Debug.Log($"ASYNC loading assetbundles for game {gameName}"); Debug.Log($"ASYNC loading assetbundles for game {gameName}");
StartCoroutine(inf.LoadCommonAssetBundleAsync(this)); inf.LoadAssetsAsync().Forget();
StartCoroutine(inf.LoadLocalizedAssetBundleAsync(this));
} }
currentPreSwitch++; currentPreSwitch++;
} }
@ -392,8 +392,7 @@ namespace HeavenStudio
if (inf != null && inf.usesAssetBundle && !inf.AssetsLoaded) if (inf != null && inf.usesAssetBundle && !inf.AssetsLoaded)
{ {
Debug.Log($"ASYNC loading assetbundles for game {gameName}"); Debug.Log($"ASYNC loading assetbundles for game {gameName}");
StartCoroutine(inf.LoadCommonAssetBundleAsync(this)); inf.LoadAssetsAsync().Forget();
StartCoroutine(inf.LoadLocalizedAssetBundleAsync(this));
} }
currentPreEvent++; currentPreEvent++;
} }
@ -620,7 +619,8 @@ namespace HeavenStudio
if (miniGame != null) if (miniGame != null)
miniGame.OnPlay(beat); miniGame.OnPlay(beat);
} }
Application.backgroundLoadingPriority = ThreadPriority.Low;
Conductor.instance.Play(beat); Conductor.instance.Play(beat);
} }
@ -664,6 +664,10 @@ namespace HeavenStudio
{ {
Play(0, restartDelay); Play(0, restartDelay);
} }
else
{
Application.backgroundLoadingPriority = ThreadPriority.Normal;
}
// when rating screen gets added playOnStart will instead move to that scene // when rating screen gets added playOnStart will instead move to that scene
} }

View file

@ -140,6 +140,7 @@ namespace HeavenStudio
IEnumerator LoadSceneAsync(string scene, float fadeOut) IEnumerator LoadSceneAsync(string scene, float fadeOut)
{ {
Application.backgroundLoadingPriority = ThreadPriority.Normal;
//TODO: create flow mem loading icon //TODO: create flow mem loading icon
asyncLoad = SceneManager.LoadSceneAsync(scene); asyncLoad = SceneManager.LoadSceneAsync(scene);
while (!asyncLoad.isDone) while (!asyncLoad.isDone)

View file

@ -1,5 +1,7 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using UnityEngine; using UnityEngine;
using DG.Tweening; using DG.Tweening;
@ -293,9 +295,9 @@ namespace HeavenStudio
public List<string> supportedLocales; public List<string> supportedLocales;
public bool inferred; public bool inferred;
public bool usesAssetBundle => (wantAssetBundle != ""); public bool usesAssetBundle => wantAssetBundle != "";
public bool hasLocales => (supportedLocales.Count > 0); public bool hasLocales => supportedLocales.Count > 0;
public bool AssetsLoaded => (((hasLocales && localeLoaded && currentLoadedLocale == defaultLocale) || (!hasLocales)) && commonLoaded); public bool AssetsLoaded => ((hasLocales && localeLoaded && currentLoadedLocale == defaultLocale) || (!hasLocales)) && commonLoaded;
public bool SequencesPreloaded => soundSequences != null; public bool SequencesPreloaded => soundSequences != null;
public string LoadableName => inferred ? "noGame" : name; public string LoadableName => inferred ? "noGame" : name;
public GameObject LoadedPrefab => loadedPrefab; public GameObject LoadedPrefab => loadedPrefab;
@ -308,6 +310,8 @@ namespace HeavenStudio
private bool localeLoaded = false; private bool localeLoaded = false;
private bool localePreloaded = false; private bool localePreloaded = false;
private GameObject loadedPrefab = null; private GameObject loadedPrefab = null;
private Dictionary<string, AudioClip> commonAudioClips;
private Dictionary<string, AudioClip> localeAudioClips;
private SoundSequence.SequenceKeyValue[] soundSequences = null; private SoundSequence.SequenceKeyValue[] soundSequences = null;
@ -316,6 +320,8 @@ namespace HeavenStudio
get => soundSequences; get => soundSequences;
set => soundSequences = value; set => soundSequences = value;
} }
public Dictionary<string, AudioClip> CommonAudioClips => commonAudioClips;
public Dictionary<string, AudioClip> LocaleAudioClips => localeAudioClips;
public Minigame(string name, string displayName, string color, bool hidden, bool fxOnly, List<GameAction> actions, List<string> tags = null, string assetBundle = "", string defaultLocale = "en", List<string> supportedLocales = null, bool inferred = false) public Minigame(string name, string displayName, string color, bool hidden, bool fxOnly, List<GameAction> actions, List<string> tags = null, string assetBundle = "", string defaultLocale = "en", List<string> supportedLocales = null, bool inferred = false)
{ {
@ -361,64 +367,52 @@ namespace HeavenStudio
return bundleCommon; return bundleCommon;
} }
AssetBundleCreateRequest commonBundleRequest; public async UniTask LoadAssetsAsync()
public IEnumerator LoadCommonAssetBundleAsync(MonoBehaviour runner)
{ {
if (commonPreloaded || commonLoaded) yield break; if (AssetsLoaded || !usesAssetBundle) return;
commonPreloaded = true; await UniTask.WhenAll(LoadCommonAssetBundleAsync(), LoadLocalizedAssetBundleAsync());
if (!usesAssetBundle) yield break; await UniTask.WhenAll(LoadGamePrefabAsync());
if (bundleCommon != null) yield break; await UniTask.WhenAll(LoadCommonAudioClips());
await UniTask.WhenAll(LoadLocalizedAudioClips());
if (commonBundleRequest == null)
commonBundleRequest = AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/common"));
if (bundleCommon != null) yield break;
yield return commonBundleRequest;
bundleCommon = commonBundleRequest.assetBundle;
commonLoaded = true;
commonBundleRequest = null;
runner.StartCoroutine(LoadGamePrefabAsync());
// runner.StartCoroutine(LoadCommonAudioClipsAsync());
} }
AssetBundleCreateRequest localeBundleRequest; public async UniTask LoadCommonAssetBundleAsync()
public IEnumerator LoadLocalizedAssetBundleAsync(MonoBehaviour runner)
{ {
if (localePreloaded) yield break; if (commonPreloaded || commonLoaded) return;
commonPreloaded = true;
if (!usesAssetBundle) return;
if (bundleCommon != null) return;
AssetBundle bundle = await AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/common")).ToUniTask();
bundleCommon = bundle;
commonLoaded = true;
}
public async UniTask LoadLocalizedAssetBundleAsync()
{
if (!hasLocales) return;
if (localePreloaded) return;
localePreloaded = true; localePreloaded = true;
if (!hasLocales) yield break; if (!usesAssetBundle) return;
if (!usesAssetBundle) yield break; if (localeLoaded && bundleLocalized != null && currentLoadedLocale == defaultLocale) return;
if (localeLoaded && bundleLocalized != null && currentLoadedLocale == defaultLocale) yield break;
if (localeBundleRequest == null) AssetBundle bundle = await AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/locale." + defaultLocale)).ToUniTask();
localeBundleRequest = AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/locale." + defaultLocale)); if (localeLoaded && bundleLocalized != null && currentLoadedLocale == defaultLocale) return;
if (localeLoaded && bundleLocalized != null && currentLoadedLocale == defaultLocale) yield break;
yield return localeBundleRequest;
bundleLocalized = localeBundleRequest.assetBundle; bundleLocalized = bundle;
currentLoadedLocale = defaultLocale; currentLoadedLocale = defaultLocale;
localeLoaded = true; localeLoaded = true;
localeBundleRequest = null;
// runner.StartCoroutine(LoadLocalizedAudioClipsAsync());
} }
AssetBundleRequest prefabRequest; public async UniTask LoadGamePrefabAsync()
public IEnumerator LoadGamePrefabAsync()
{ {
if (!usesAssetBundle) yield break; if (!usesAssetBundle) return;
if (!commonLoaded) yield break; if (!commonLoaded) return;
if (bundleCommon == null) yield break; if (bundleCommon == null) return;
if (prefabRequest == null) UnityEngine.Object asset = await bundleCommon.LoadAssetAsync<GameObject>(name).ToUniTask();
{ loadedPrefab = asset as GameObject;
prefabRequest = bundleCommon.LoadAssetAsync<GameObject>(name);
prefabRequest.priority = 0;
}
yield return prefabRequest;
loadedPrefab = prefabRequest.asset as GameObject;
prefabRequest = null;
// load sound sequences here for now // load sound sequences here for now
// this is taxing and is still done synchronously // this is taxing and is still done synchronously
@ -429,36 +423,67 @@ namespace HeavenStudio
} }
} }
AssetBundleRequest audioCommonRequest; public async UniTask LoadCommonAudioClips()
public IEnumerator LoadCommonAudioClipsAsync()
{ {
if (!usesAssetBundle) yield break; if (!commonLoaded) return;
if (!commonLoaded) yield break; if (bundleCommon == null) return;
if (bundleCommon == null) yield break;
if (audioCommonRequest == null) commonAudioClips = new();
{
audioCommonRequest = bundleCommon.LoadAllAssetsAsync<AudioClip>(); var assets = bundleCommon.LoadAllAssetsAsync();
audioCommonRequest.priority = 1; await assets;
}
yield return audioCommonRequest; // await UniTask.SwitchToThreadPool();
audioCommonRequest = null; // foreach (var asset in assets.allAssets)
// {
// AudioClip clip = asset as AudioClip;
// commonAudioClips.Add(clip.name, clip);
// }
// await UniTask.SwitchToMainThread();
} }
AssetBundleRequest audioLocaleRequest; public async UniTask LoadLocalizedAudioClips()
public IEnumerator LoadLocalizedAudioClipsAsync()
{ {
if (!usesAssetBundle) yield break; if (!localeLoaded) return;
if (!localeLoaded) yield break; if (bundleLocalized == null) return;
if (bundleLocalized == null) yield break;
if (audioLocaleRequest == null) localeAudioClips = new();
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()
{
if (!usesAssetBundle) return;
commonAudioClips.Clear();
localeAudioClips.Clear();
if (loadedPrefab != null)
{ {
audioLocaleRequest = bundleLocalized.LoadAllAssetsAsync<AudioClip>(); loadedPrefab = null;
audioLocaleRequest.priority = 1; }
if (bundleCommon != null)
{
await bundleCommon.UnloadAsync(true);
bundleCommon = null;
commonLoaded = false;
commonPreloaded = false;
}
if (bundleLocalized != null)
{
await bundleLocalized.UnloadAsync(true);
bundleLocalized = null;
localeLoaded = false;
localePreloaded = false;
} }
yield return audioLocaleRequest;
audioLocaleRequest = null;
} }
} }

View file

@ -55,7 +55,7 @@ namespace HeavenStudio.Util
await UniTask.WaitUntil(() => Conductor.instance.songPositionInBeatsAsDouble >= actions[idx].beat || (!Conductor.instance.isPlaying) || behaviour == null, cancellationToken: token); await UniTask.WaitUntil(() => Conductor.instance.songPositionInBeatsAsDouble >= actions[idx].beat || (!Conductor.instance.isPlaying) || behaviour == null, cancellationToken: token);
if (behaviour == null || !Conductor.instance.isPlaying) if (behaviour == null || !Conductor.instance.isPlaying)
break; return;
actions[idx].function.Invoke(); actions[idx].function.Invoke();
idx++; idx++;

View file

@ -134,7 +134,7 @@ PlayerSettings:
16:10: 1 16:10: 1
16:9: 1 16:9: 1
Others: 1 Others: 1
bundleVersion: 0.0.1002 bundleVersion: 0.0.1003
preloadedAssets: preloadedAssets:
- {fileID: 102900000, guid: 5348c08b82446e0478cee8bda6c02cfc, type: 3} - {fileID: 102900000, guid: 5348c08b82446e0478cee8bda6c02cfc, type: 3}
metroInputSource: 0 metroInputSource: 0
@ -158,11 +158,11 @@ PlayerSettings:
applicationIdentifier: applicationIdentifier:
Standalone: com.RHeavenStudio.Heaven-Studio Standalone: com.RHeavenStudio.Heaven-Studio
buildNumber: buildNumber:
Standalone: 1002 Standalone: 1003
iPhone: 0 iPhone: 0
tvOS: 0 tvOS: 0
overrideDefaultApplicationIdentifier: 0 overrideDefaultApplicationIdentifier: 0
AndroidBundleVersionCode: 1002 AndroidBundleVersionCode: 1003
AndroidMinSdkVersion: 22 AndroidMinSdkVersion: 22
AndroidTargetSdkVersion: 0 AndroidTargetSdkVersion: 0
AndroidPreferredInstallLocation: 1 AndroidPreferredInstallLocation: 1