mirror of
https://github.com/RHeavenStudio/HeavenStudio.git
synced 2025-06-12 11:07:39 +02:00
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:
@ -31,7 +31,7 @@ namespace HeavenStudio
|
||||
Coroutine currentGameSwitchIE;
|
||||
|
||||
[Header("Properties")]
|
||||
public int currentEvent, currentTempoEvent;
|
||||
public int currentEvent, currentTempoEvent, currentPreEvent, currentPreSwitch;
|
||||
public float startOffset;
|
||||
public bool playOnStart;
|
||||
public float startBeat;
|
||||
@ -57,7 +57,11 @@ namespace HeavenStudio
|
||||
|
||||
public void Init()
|
||||
{
|
||||
currentPreEvent= 0;
|
||||
currentPreSwitch = 0;
|
||||
|
||||
this.transform.localScale = new Vector3(30000000, 30000000);
|
||||
|
||||
SpriteRenderer sp = this.gameObject.AddComponent<SpriteRenderer>();
|
||||
sp.enabled = false;
|
||||
sp.color = Color.black;
|
||||
@ -148,6 +152,49 @@ namespace HeavenStudio
|
||||
}
|
||||
}
|
||||
|
||||
public void SeekAheadAndPreload(float start, float seekTime = 8f)
|
||||
{
|
||||
//seek ahead to preload games that have assetbundles
|
||||
//check game switches first
|
||||
var gameSwitchs = Beatmap.entities.FindAll(c => c.datamodel.Split(1) == "switchGame");
|
||||
if (currentPreSwitch < gameSwitchs.Count && currentPreSwitch >= 0)
|
||||
{
|
||||
if (start + seekTime >= gameSwitchs[currentPreSwitch].beat)
|
||||
{
|
||||
string gameName = gameSwitchs[currentPreSwitch].datamodel.Split(2);
|
||||
var inf = GetGameInfo(gameName);
|
||||
if (inf.usesAssetBundle && !inf.AssetsLoaded)
|
||||
{
|
||||
Debug.Log("ASYNC loading assetbundle for game " + gameName);
|
||||
StartCoroutine(inf.LoadCommonAssetBundleAsync());
|
||||
StartCoroutine(inf.LoadLocalizedAssetBundleAsync());
|
||||
}
|
||||
currentPreSwitch++;
|
||||
}
|
||||
}
|
||||
//then check game entities
|
||||
List<float> entities = Beatmap.entities.Select(c => c.beat).ToList();
|
||||
if (currentPreEvent < Beatmap.entities.Count && currentPreEvent >= 0)
|
||||
{
|
||||
if (start + seekTime >= entities[currentPreEvent])
|
||||
{
|
||||
var entitiesAtSameBeat = Beatmap.entities.FindAll(c => c.beat == Beatmap.entities[currentPreEvent].beat && !EventCaller.FXOnlyGames().Contains(EventCaller.instance.GetMinigame(c.datamodel.Split('/')[0])));
|
||||
for (int i = 0; i < entitiesAtSameBeat.Count; i++)
|
||||
{
|
||||
string gameName = entitiesAtSameBeat[i].datamodel.Split('/')[0];
|
||||
var inf = GetGameInfo(gameName);
|
||||
if (inf.usesAssetBundle && !inf.AssetsLoaded)
|
||||
{
|
||||
Debug.Log("ASYNC loading assetbundle for game " + gameName);
|
||||
StartCoroutine(inf.LoadCommonAssetBundleAsync());
|
||||
StartCoroutine(inf.LoadLocalizedAssetBundleAsync());
|
||||
}
|
||||
}
|
||||
currentPreEvent++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// LateUpdate works a bit better(?) but causes some bugs (like issues with bop animations).
|
||||
private void Update()
|
||||
{
|
||||
@ -171,6 +218,10 @@ namespace HeavenStudio
|
||||
}
|
||||
}
|
||||
|
||||
float seekTime = 8f;
|
||||
//seek ahead to preload games that have assetbundles
|
||||
SeekAheadAndPreload(Conductor.instance.songPositionInBeats, seekTime);
|
||||
|
||||
if (currentEvent < Beatmap.entities.Count && currentEvent >= 0)
|
||||
{
|
||||
if (Conductor.instance.songPositionInBeats >= entities[currentEvent] /*&& SongPosLessThanClipLength(Conductor.instance.songPositionInBeats)*/)
|
||||
@ -281,6 +332,7 @@ namespace HeavenStudio
|
||||
List<float> entities = Beatmap.entities.Select(c => c.beat).ToList();
|
||||
|
||||
currentEvent = entities.IndexOf(Mathp.GetClosestInList(entities, beat));
|
||||
currentPreEvent = entities.IndexOf(Mathp.GetClosestInList(entities, beat));
|
||||
|
||||
var gameSwitchs = Beatmap.entities.FindAll(c => c.datamodel.Split(1) == "switchGame");
|
||||
|
||||
@ -289,6 +341,7 @@ namespace HeavenStudio
|
||||
if (gameSwitchs.Count > 0)
|
||||
{
|
||||
int index = gameSwitchs.FindIndex(c => c.beat == Mathp.GetClosestInList(gameSwitchs.Select(c => c.beat).ToList(), beat));
|
||||
currentPreSwitch = index;
|
||||
var closestGameSwitch = gameSwitchs[index];
|
||||
if (closestGameSwitch.beat <= beat)
|
||||
{
|
||||
@ -342,6 +395,8 @@ namespace HeavenStudio
|
||||
}
|
||||
// Debug.Log("currentTempoEvent is now " + currentTempoEvent);
|
||||
}
|
||||
|
||||
SeekAheadAndPreload(beat);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -434,6 +489,14 @@ namespace HeavenStudio
|
||||
return !newGameInfo.fxOnly;
|
||||
}).ToList()[0].datamodel.Split(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (gameInfo.usesAssetBundle)
|
||||
{
|
||||
//game is packed in an assetbundle, load from that instead
|
||||
return gameInfo.GetCommonAssetBundle().LoadAsset<GameObject>(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Resources.Load<GameObject>($"Games/{name}");
|
||||
}
|
||||
|
Reference in New Issue
Block a user