FANDOM


In the Update function of Music.MusicPiece.SubTrack, you'll see a section of code like this:

if (!this.readyToPlay) {
    if (this.source.clip == null) {
        if (this.piece.IsProcedural) {
            this.source.clip = (Resources.Load("Music/Procedural/" + this.trackName, typeof(AudioClip)) as AudioClip);
        }
	else {
	    this.source.clip = (Resources.Load("Music/Songs/" + this.trackName, typeof(AudioClip)) as AudioClip);
	}
    }
    else if (!this.source.isPlaying && this.source.clip.isReadyToPlay) {
        this.readyToPlay = true;
    }
}

This is where the game loads a song from the resources.assets file before it is about to play the song. However, we can't edit the resources.assets file to change the music, so instead we need to create our own resource loader and audio clip streamer that streams the music from disk instead. Then we can hijack this function to use that resource loader for our custom music instead of the default one.

Here's an example custom resource loader/streamer:

string rootpath = Application.dataPath;
string filepath = rootpath.Substring(0, rootpath.LastIndexOf("/")) + "/Assets/Futile/Resources/Music/Procedural/" + this.trackName + ".ogg";
WWW www = new WWW("file://" + filepath);
this.source.clip = www.GetAudioClip(false, true, AudioType.OGGVORBIS);

For OGG files, you would use AudioType.OGGVORBIS, and for MP3 files, you would you AudioType.MPEG.

Then simply add a new condition to the Update function to compare the trackName to the one you want to override. If it's a match, load the corresponding music file from disk with the custom resource loader, otherwise fall back to the default resource loader.

Community content is available under CC-BY-SA unless otherwise noted.