Reputation: 1
The goal is to have an audio switch whenever a new "Instrumon" is on the screen and be on time with the drum set. Issue I'm having that there is a lot of load time when loading a battle and I've seen it happen before where some people have it switch during a certain event. The method I used (I KNOW this is a god awful) is to play all tracks at the same time then immediately mute them. If I needed to switch the object on screen, it would call "MutePlayerSongs" or "MuteOppSongs" every time depending on which one needed to switch. Here is the code snippet. (PLEASE don't roast me too hard, this was made like 2 years ago learning C# for the first time and it was for a class under crunch time. I know this code is god awful, no need to remind me.)
I currently do not have the Unity file to show the hierarchy tab.
''' public class BattleAudioManager : MonoBehaviour {
// Getting the main drum track
[Header("-------Drum Track---------")]
public AudioSource battleMusic;
[Header("-----Back Up Tracks---------")]
public AudioSource instrumonSongs;
public AudioSource oppInstrumonSongs;
// Getting the audio sources for the player
[Header("--------Player Audio Source---------")]
public AudioSource clarinetInstrumon;
public AudioSource fluteInstrumon;
public AudioSource saxophoneInstrumon;
public AudioSource violinInstrumon;
public AudioSource celloInstrumon;
public AudioSource guitarInstrumon;
public AudioSource pianoInstrumon;
public AudioSource timpaniInstrumon;
public AudioSource xylophoneInstrumon;
public AudioSource tubaInstrumon;
public AudioSource tromboneInstrumon;
public AudioSource trumpetInstrumon;
public AudioSource cymbalInstrumon;
//Getting the audio sources for the opponents
[Header("-------Opponent Audio Source--------")]
public AudioSource oppClarinetInstrumon;
public AudioSource oppFluteInstrumon;
public AudioSource oppSaxophoneInstrumon;
public AudioSource oppViolinInstrumon;
public AudioSource oppCelloInstrumon;
public AudioSource oppGuitarInstrumon;
public AudioSource oppPianoInstrumon;
public AudioSource oppTimpaniInstrumon;
public AudioSource oppXylophoneInstrumon;
public AudioSource oppTubaInstrumon;
public AudioSource oppTromboneInstrumon;
public AudioSource oppTrumpetInstrumon;
public AudioSource oppCymbalInstrumon;
// Getting all Audio clips
[Header("--------Audio Clip------------")]
public AudioClip drumLoop;
public AudioClip violinIntro;
public AudioClip violinSong;
public AudioClip tubaIntro;
public AudioClip tubaSong;
public AudioClip trumpetIntro;
public AudioClip trumpetSong;
public AudioClip tromboneIntro;
public AudioClip tromboneSong;
public AudioClip timpaniIntro;
public AudioClip timpaniSong;
public AudioClip pianoIntro;
public AudioClip pianoSong;
public AudioClip guitarIntro;
public AudioClip guitarSong;
public AudioClip fluteIntro;
public AudioClip fluteSong;
public AudioClip clarinetIntro;
public AudioClip clarinetSong;
public AudioClip celloIntro;
public AudioClip celloSong;
public AudioClip saxophoneIntro;
public AudioClip saxophoneSong;
public AudioClip xylophoneIntro;
public AudioClip xylophoneSong;
public AudioClip cymbalSong;
// Initiallizes the battle music
private void Start()
{
battleMusic = GetComponent<AudioSource>();
}
public void PlayInstrumonSongs(AudioClip clip1, AudioClip clip2)
{
battleMusic.clip = clip2;
instrumonSongs.clip = clip1;
battleMusic.PlayScheduled(AudioSettings.dspTime + .05f);
instrumonSongs.PlayScheduled(AudioSettings.dspTime + .05f);
}
public void OppInstrumonSongs(AudioClip clip1)
{
oppInstrumonSongs.clip = clip1;
oppInstrumonSongs.PlayScheduled(AudioSettings.dspTime + .05f);
}
// This starts all Instrumon songs (player AND opponent) to be played at the same time
public void StartInstrumonSongs()
{
//starting player songs
battleMusic.PlayScheduled(AudioSettings.dspTime + .05f);
clarinetInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
fluteInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
saxophoneInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
timpaniInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
pianoInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
xylophoneInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
tubaInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
trumpetInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
tromboneInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
violinInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
celloInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
guitarInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
cymbalInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
//Starting opponents song
oppClarinetInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
oppCelloInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
oppSaxophoneInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
oppTimpaniInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
oppPianoInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
oppXylophoneInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
oppTubaInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
oppTrumpetInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
oppTromboneInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
oppViolinInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
oppCelloInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
oppGuitarInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
oppCymbalInstrumon.PlayScheduled(AudioSettings.dspTime + .05f);
}
/**
* This method mutes all player songs and unmutes the appropriate audio source depending on the active instrumon on the scene.
* This method is recalled again after an instrumon switches or dies
*/
public void MutePlayerSongs()
{
//Muting Player audio clips
clarinetInstrumon.mute = true;
fluteInstrumon.mute = true;
saxophoneInstrumon.mute = true;
trumpetInstrumon.mute = true;
tromboneInstrumon.mute = true;
tubaInstrumon.mute = true;
violinInstrumon.mute = true;
celloInstrumon.mute = true;
guitarInstrumon.mute = true;
pianoInstrumon.mute = true;
timpaniInstrumon.mute = true;
xylophoneInstrumon.mute = true;
cymbalInstrumon.mute = true;
if (BattleController.playerCurrentMon.Base.instrumonName == "Corvinet") {
clarinetInstrumon.mute = false;
}
if(BattleController.playerCurrentMon.Base.instrumonName == "Guitowl") {
guitarInstrumon.mute = false;
}
if (BattleController.playerCurrentMon.Base.instrumonName == "Trumpig") {
trumpetInstrumon.mute = false;
}
if (BattleController.playerCurrentMon.Base.instrumonName == "Elephone") {
saxophoneInstrumon.mute = false;
}
if (BattleController.playerCurrentMon.Base.instrumonName == "Flumingo") {
fluteInstrumon.mute = false;
}
if (BattleController.playerCurrentMon.Base.instrumonName == "Locello") {
celloInstrumon.mute = false;
}
if (BattleController.playerCurrentMon.Base.instrumonName == "Tarampani") {
timpaniInstrumon.mute = false;
}
if (BattleController.playerCurrentMon.Base.instrumonName == "Tortuba") {
tubaInstrumon.mute = false;
}
if (BattleController.playerCurrentMon.Base.instrumonName == "Trombeaver") {
tromboneInstrumon.mute = false;
}
if (BattleController.playerCurrentMon.Base.instrumonName == "Viperlin") {
violinInstrumon.mute = false;
}
if (BattleController.playerCurrentMon.Base.instrumonName == "Xylynx") {
xylophoneInstrumon.mute = false;
}
if (BattleController.playerCurrentMon.Base.instrumonName == "Cymbalisk") {
cymbalInstrumon.mute = false;
}
}
/**
* This method mutes all opponents songs and unmutes the appropriate audio source depending on the active instrumon on the scene.
* This method is recalled again after an instrumon switches or dies
*/
public void MuteOppSongs()
{
//Muting opponent audio clips
oppClarinetInstrumon.mute = true;
oppFluteInstrumon.mute = true;
oppSaxophoneInstrumon.mute = true;
oppTrumpetInstrumon.mute = true;
oppTromboneInstrumon.mute = true;
oppTubaInstrumon.mute = true;
oppViolinInstrumon.mute = true;
oppCelloInstrumon.mute = true;
oppGuitarInstrumon.mute = true;
oppPianoInstrumon.mute = true;
oppTimpaniInstrumon.mute = true;
oppXylophoneInstrumon.mute = true;
oppCymbalInstrumon.mute = true;
if (BattleController.oppCurrentMon.Base.instrumonName == "Corvinet")
{
oppClarinetInstrumon.mute = false;
}
if (BattleController.oppCurrentMon.Base.instrumonName == "Guitowl")
{
oppGuitarInstrumon.mute = false;
}
if (BattleController.oppCurrentMon.Base.instrumonName == "Trumpig")
{
oppTrumpetInstrumon.mute = false;
}
if (BattleController.oppCurrentMon.Base.instrumonName == "Elephone")
{
oppSaxophoneInstrumon.mute = false;
}
if (BattleController.oppCurrentMon.Base.instrumonName == "Flumingo")
{
oppFluteInstrumon.mute = false;
}
if (BattleController.oppCurrentMon.Base.instrumonName == "Locello")
{
oppCelloInstrumon.mute = false;
}
if (BattleController.oppCurrentMon.Base.instrumonName == "Tarampani")
{
oppTimpaniInstrumon.mute = false;
}
if (BattleController.oppCurrentMon.Base.instrumonName == "Tortuba")
{
oppTubaInstrumon.mute = false;
}
if (BattleController.oppCurrentMon.Base.instrumonName == "Trombeaver")
{
oppTromboneInstrumon.mute = false;
}
if (BattleController.oppCurrentMon.Base.instrumonName == "Viperlin")
{
oppViolinInstrumon.mute = false;
}
if (BattleController.oppCurrentMon.Base.instrumonName == "Xylynx")
{
oppXylophoneInstrumon.mute = false;
}
if (BattleController.oppCurrentMon.Base.instrumonName == "Cymbalisk")
{
oppCymbalInstrumon.mute = false;
}
}
'''
Upvotes: 0
Views: 20
Reputation: 59
If you are using only one AudioSource at a time, you should first reduce the number of AudioSource variables to just one and play your audio clips on that single AudioSource. I didn't fully understand your reason for muting all of them at the same time—could you explain that part a bit more?
Additionally, you might prefer storing your AudioClips in a list. This way, you can use loops within the list for muting and playing them.
This makes the code much harder to read—maybe you can find a way to improve its readability.
Upvotes: 0