Hi,
After what seemed to be an eternity we have finally finished our new Unity simple audio core!
Before we continue with game development we were hoping some people could look at the audio core?
Please look at it and provide feedback, complaints, or suggestions for improvement.
You can download the entire project as it stands below:
http://fallenangelsoftware.com/stuff/files/FerrariF40-Engine/FerrariF40-Engine.zip
Audio core does the following:
- Load all music and sound effects at game start
- Play music: stop old music and play new music(survives scene change)
- Play sound effect
Big thanks to all the forum members who help us out!
We will be porting this simple Flappy Bird styled car race game to Unity:
http://fallenangelsoftware.com/redlightracer.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioCore : MonoBehaviour
{
// Start is called before the first frame update
void Awake()
{
AudioSource temp = GetComponent<AudioSource>();
int index = 0;
if (temp.name == "Effect0") index = 0;
else if (temp.name == "Effect1") index = 1;
else if (temp.name == "Effect2") index = 2;
else if (temp.name == "Effect3") index = 3;
else if (temp.name == "Effect4") index = 4;
else if (temp.name == "Music0") index = 5;
else if (temp.name == "Music1") index = 6;
else if (temp.name == "Music2") index = 7;
else if (temp.name == "Music3") index = 8;
else if (temp.name == "Music4") index = 9;
else if (temp.name == "Music5") index = 10;
else if (temp.name == "Music6") index = 11;
else if (temp.name == "Music7") index = 12;
else if (temp.name == "Music8") index = 13;
else if (temp.name == "Music9") index = 14;
else if (temp.name == "Music10") index = 15;
else if (temp.name == "Music11") index = 16;
GlobalVariables.AudioSources[index] = GetComponent<AudioSource>();
GlobalVariables.AudioSources[index].volume = 1.0f;
DontDestroyOnLoad(GlobalVariables.AudioSources[index]);
}
// Update is called once per frame
void Update()
{
if (GlobalVariables.MusicToPlay > -1)
{
AudioSource temp = GetComponent<AudioSource>();
int index = 0;
if (temp.name == "Music0") index = 5;
else if (temp.name == "Music1") index = 6;
else if (temp.name == "Music2") index = 7;
else if (temp.name == "Music3") index = 8;
else if (temp.name == "Music4") index = 9;
else if (temp.name == "Music5") index = 10;
else if (temp.name == "Music6") index = 11;
else if (temp.name == "Music7") index = 12;
else if (temp.name == "Music8") index = 13;
else if (temp.name == "Music9") index = 14;
else if (temp.name == "Music10") index = 15;
else if (temp.name == "Music11") index = 16;
for (int i = 5; i < 17; i++)
{
if ( index == i && GlobalVariables.MusicToPlay == (i - 5) )
{
GlobalVariables.AudioSources[GlobalVariables.MusicCurrentlyPlaying].Stop();
GlobalVariables.AudioSources[i].Play(0);
GlobalVariables.MusicCurrentlyPlaying = i;
GlobalVariables.MusicToPlay = -1;
}
}
}
if (GlobalVariables.SoundEffectToPlay > -1)
{
AudioSource temp = GetComponent<AudioSource>();
int index = 0;
if (temp.name == "Effect0") index = 0;
else if (temp.name == "Effect1") index = 1;
else if (temp.name == "Effect2") index = 2;
else if (temp.name == "Effect3") index = 3;
else if (temp.name == "Effect4") index = 4;
for (int i = 0; i < 5; i++)
{
if (index == i && GlobalVariables.SoundEffectToPlay == i)
{
GlobalVariables.AudioSources[i].Play(0);
GlobalVariables.SoundEffectToPlay = -1;
}
}
}
}
}