Allow games to load assets from AssetBundles (#102)

* Loading improvements: prepwork for loading from assetbundles

* note for sfx

* cleaner code

* correct type

* put loaded assetbundle in the minigame data instead

also lays groundwork for future localization stuff

* add samurai slice gold, coin toss assetbundles

* very messy "already loaded" checks

* make Dj School load from assetbundle
This commit is contained in:
minenice55
2022-06-12 15:32:00 -04:00
committed by GitHub
parent cd70267243
commit 1dda4f9477
387 changed files with 1582 additions and 1463 deletions

View File

@ -40,7 +40,7 @@ namespace HeavenStudio.Util
FindJukebox().GetComponent<AudioSource>().volume = volume;
}
public static Sound PlayOneShot(string name, float beat = -1, float pitch = 1f, float volume = 1f, bool looping = false)
public static Sound PlayOneShot(string name, float beat = -1, float pitch = 1f, float volume = 1f, bool looping = false, string game = null)
{
GameObject oneShot = new GameObject("oneShot");
@ -49,7 +49,29 @@ namespace HeavenStudio.Util
audioSource.playOnAwake = false;
Sound snd = oneShot.AddComponent<Sound>();
AudioClip clip = Resources.Load<AudioClip>($"Sfx/{name}");
AudioClip clip = null;
if (game != null)
{
string soundName = name.Split('/')[2];
var inf = GameManager.instance.GetGameInfo(game);
//first try the game's common assetbundle
// Debug.Log("Jukebox loading sound " + soundName + " from common");
clip = inf.GetCommonAssetBundle()?.LoadAsset<AudioClip>(soundName);
//then the localized one
if (clip == null)
{
// Debug.Log("Jukebox loading sound " + soundName + " from locale");
clip = inf.GetLocalizedAssetBundle()?.LoadAsset<AudioClip>(soundName);
}
}
//can't load from assetbundle, load from resources
if (clip == null)
{
// Debug.Log("Jukebox loading sound " + name + " from resources");
clip = Resources.Load<AudioClip>($"Sfx/{name}");
}
snd.clip = clip;
snd.beat = beat;
snd.pitch = pitch;
@ -62,7 +84,7 @@ namespace HeavenStudio.Util
return snd;
}
public static Sound PlayOneShotScheduled(string name, double targetTime, float pitch = 1f, float volume = 1f, bool looping = false)
public static Sound PlayOneShotScheduled(string name, double targetTime, float pitch = 1f, float volume = 1f, bool looping = false, string game = null)
{
GameObject oneShot = new GameObject("oneShotScheduled");
@ -70,8 +92,26 @@ namespace HeavenStudio.Util
audioSource.playOnAwake = false;
var snd = oneShot.AddComponent<Sound>();
AudioClip clip = null;
if (game != null)
{
string soundName = name.Split('/')[2];
var inf = GameManager.instance.GetGameInfo(game);
//first try the game's common assetbundle
// Debug.Log("Jukebox loading sound " + soundName + " from common");
clip = inf.GetCommonAssetBundle()?.LoadAsset<AudioClip>(soundName);
//then the localized one
if (clip == null)
{
// Debug.Log("Jukebox loading sound " + soundName + " from locale");
clip = inf.GetLocalizedAssetBundle()?.LoadAsset<AudioClip>(soundName);
}
}
//can't load from assetbundle, load from resources
if (clip == null)
clip = Resources.Load<AudioClip>($"Sfx/{name}");
var clip = Resources.Load<AudioClip>($"Sfx/{name}");
audioSource.clip = clip;
snd.clip = clip;
snd.pitch = pitch;
@ -89,9 +129,11 @@ namespace HeavenStudio.Util
public static Sound PlayOneShotGame(string name, float beat = -1, float pitch = 1f, float volume = 1f, bool looping = false, bool forcePlay = false)
{
if (GameManager.instance.currentGame == name.Split('/')[0] || forcePlay)
string gameName = name.Split('/')[0];
var inf = GameManager.instance.GetGameInfo(gameName);
if (GameManager.instance.currentGame == gameName || forcePlay)
{
return PlayOneShot($"games/{name}", beat, pitch, volume, looping);
return PlayOneShot($"games/{name}", beat, pitch, volume, looping, inf.usesAssetBundle ? gameName : null);
}
return null;
@ -99,14 +141,18 @@ namespace HeavenStudio.Util
public static Sound PlayOneShotScheduledGame(string name, double targetTime, float pitch = 1f, float volume = 1f, bool looping = false, bool forcePlay = false)
{
if (GameManager.instance.currentGame == name.Split('/')[0] || forcePlay)
string gameName = name.Split('/')[0];
var inf = GameManager.instance.GetGameInfo(gameName);
if (GameManager.instance.currentGame == gameName || forcePlay)
{
return PlayOneShotScheduled($"games/{name}", targetTime, pitch, volume, looping);
return PlayOneShotScheduled($"games/{name}", targetTime, pitch, volume, looping, inf.usesAssetBundle ? gameName : null);
}
return null;
}
//TODO: playing sounds from assetbundles
public static void KillLoop(Sound source, float fadeTime)
{
// Safeguard against previously-destroyed sounds.