Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback Please Criticize Our New Simple Audio Core!

Discussion in 'Works In Progress - Archive' started by JeZxLee, Jul 11, 2019.

  1. JeZxLee

    JeZxLee

    Joined:
    Jul 24, 2016
    Posts:
    222
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AudioCore : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Awake()
    9.     {
    10.         AudioSource temp = GetComponent<AudioSource>();
    11.         int index = 0;
    12.         if (temp.name == "Effect0") index = 0;
    13.         else if (temp.name == "Effect1") index = 1;
    14.         else if (temp.name == "Effect2") index = 2;
    15.         else if (temp.name == "Effect3") index = 3;
    16.         else if (temp.name == "Effect4") index = 4;
    17.         else if (temp.name == "Music0") index = 5;
    18.         else if (temp.name == "Music1") index = 6;
    19.         else if (temp.name == "Music2") index = 7;
    20.         else if (temp.name == "Music3") index = 8;
    21.         else if (temp.name == "Music4") index = 9;
    22.         else if (temp.name == "Music5") index = 10;
    23.         else if (temp.name == "Music6") index = 11;
    24.         else if (temp.name == "Music7") index = 12;
    25.         else if (temp.name == "Music8") index = 13;
    26.         else if (temp.name == "Music9") index = 14;
    27.         else if (temp.name == "Music10") index = 15;
    28.         else if (temp.name == "Music11") index = 16;
    29.  
    30.         GlobalVariables.AudioSources[index] = GetComponent<AudioSource>();
    31.         GlobalVariables.AudioSources[index].volume = 1.0f;
    32.         DontDestroyOnLoad(GlobalVariables.AudioSources[index]);
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         if (GlobalVariables.MusicToPlay > -1)
    39.         {
    40.             AudioSource temp = GetComponent<AudioSource>();
    41.             int index = 0;
    42.             if (temp.name == "Music0") index = 5;
    43.             else if (temp.name == "Music1") index = 6;
    44.             else if (temp.name == "Music2") index = 7;
    45.             else if (temp.name == "Music3") index = 8;
    46.             else if (temp.name == "Music4") index = 9;
    47.             else if (temp.name == "Music5") index = 10;
    48.             else if (temp.name == "Music6") index = 11;
    49.             else if (temp.name == "Music7") index = 12;
    50.             else if (temp.name == "Music8") index = 13;
    51.             else if (temp.name == "Music9") index = 14;
    52.             else if (temp.name == "Music10") index = 15;
    53.             else if (temp.name == "Music11") index = 16;
    54.  
    55.             for (int i = 5; i < 17; i++)
    56.             {
    57.                 if ( index == i && GlobalVariables.MusicToPlay == (i - 5) )
    58.                 {
    59.                     GlobalVariables.AudioSources[GlobalVariables.MusicCurrentlyPlaying].Stop();
    60.  
    61.                     GlobalVariables.AudioSources[i].Play(0);
    62.                     GlobalVariables.MusicCurrentlyPlaying = i;
    63.                     GlobalVariables.MusicToPlay = -1;
    64.                 }
    65.             }
    66.         }
    67.  
    68.         if (GlobalVariables.SoundEffectToPlay > -1)
    69.         {
    70.             AudioSource temp = GetComponent<AudioSource>();
    71.             int index = 0;
    72.             if (temp.name == "Effect0") index = 0;
    73.             else if (temp.name == "Effect1") index = 1;
    74.             else if (temp.name == "Effect2") index = 2;
    75.             else if (temp.name == "Effect3") index = 3;
    76.             else if (temp.name == "Effect4") index = 4;
    77.  
    78.             for (int i = 0; i < 5; i++)
    79.             {
    80.                 if (index == i && GlobalVariables.SoundEffectToPlay == i)
    81.                 {
    82.                     GlobalVariables.AudioSources[i].Play(0);
    83.                     GlobalVariables.SoundEffectToPlay = -1;
    84.                 }
    85.             }
    86.         }
    87.     }
    88. }
    89.  
    U3d-AudioCore-01.png
     
  2. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    785
    Are you asking for critique on the audiocore code? If so here's a few things to think about
    1. It's very hard to follow whats going on
    2. There are no code comments (part of the reason for point 1)
    3. It's not scalable in the sense that you'd have to add a new gameobject and modify the code every time you want a new sound effect or music
    4. code is running every frame in the Update function which seems unnecessary and impacts performance - this should be function driven e.g. a function ChangeMusic() and PlaySoundEffect() could be called and this would only happen once per sound effect/music change
    5. I'm not 100% sure how this fits together, but it seems like your global audiosources are indexed in a particular order based on the name of your "SoundCore" gameobjects audiosources - this could get very confusing if this order changes or when new music and audio clips get introduced. e.g. your sound effects index ends at 4, so if you added a new sound effect, assuming this would be index 5, you'd have to increment all of your music clip indexes by 1 in multiple places.
    6. You should cache your Audiosource and index value in a class level variable on awake, then you dont have to have the same gigantic if-else statement and getcomponent running every frame in the update function
    Hopefully, that helps you out a bit without sounding too pedantic - just thought I should point things like this out before you get too deep into your project!

    Take a look at this Quora response on how to write scalable code, try to apply this to everything you write:
    https://www.quora.com/How-does-one-write-scalable-code
     
    Last edited: Jul 12, 2019
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,580
    I'm also having trouble figuring out what this is supposed to do. I don't understand why half your code is spent looking up some sort of index based on the object name. What's the reason for that?