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;
public static class AppInfo {
public const string Version = "0.0.1002";
public static readonly DateTime Date = new DateTime(2023, 09, 27, 22, 20, 38, 655, DateTimeKind.Utc);
public const string Version = "0.0.1003";
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.Games;
using HeavenStudio.Common;
using Cysharp.Threading.Tasks;
namespace HeavenStudio
{
@ -339,15 +340,15 @@ namespace HeavenStudio
{
int aLen = a.Length;
int bLen = b.Length;
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++;
bp++;
}
return (bp == bLen);
}
@ -366,8 +367,7 @@ namespace HeavenStudio
if (inf != null && inf.usesAssetBundle && !inf.AssetsLoaded)
{
Debug.Log($"ASYNC loading assetbundles for game {gameName}");
StartCoroutine(inf.LoadCommonAssetBundleAsync(this));
StartCoroutine(inf.LoadLocalizedAssetBundleAsync(this));
inf.LoadAssetsAsync().Forget();
}
currentPreSwitch++;
}
@ -392,8 +392,7 @@ namespace HeavenStudio
if (inf != null && inf.usesAssetBundle && !inf.AssetsLoaded)
{
Debug.Log($"ASYNC loading assetbundles for game {gameName}");
StartCoroutine(inf.LoadCommonAssetBundleAsync(this));
StartCoroutine(inf.LoadLocalizedAssetBundleAsync(this));
inf.LoadAssetsAsync().Forget();
}
currentPreEvent++;
}
@ -620,7 +619,8 @@ namespace HeavenStudio
if (miniGame != null)
miniGame.OnPlay(beat);
}
Application.backgroundLoadingPriority = ThreadPriority.Low;
Conductor.instance.Play(beat);
}
@ -664,6 +664,10 @@ namespace HeavenStudio
{
Play(0, restartDelay);
}
else
{
Application.backgroundLoadingPriority = ThreadPriority.Normal;
}
// 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)
{
Application.backgroundLoadingPriority = ThreadPriority.Normal;
//TODO: create flow mem loading icon
asyncLoad = SceneManager.LoadSceneAsync(scene);
while (!asyncLoad.isDone)

View file

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

View file

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