Search Unity

Question Efficient way for implement audio sources

Discussion in 'Audio & Video' started by TheSilverHand, Oct 19, 2021.

  1. TheSilverHand

    TheSilverHand

    Joined:
    Jan 29, 2021
    Posts:
    16
    I have a soundmanager script that controls AI and player's audios, its singleton.But i'm not sure about is it efficient way to implement it on my scene ?. İ have 12 audio most of them are very short files such as food step fire sound etc. and couple ambiance sound. İ've researched little bit but couldn't find answer for it.
    here is my script ;
    Code (CSharp):
    1. public class SoundManager : MonoBehaviour
    2. {
    3.     public static SoundManager instance = null;
    4.  
    5.     public AudioSource[] audios;
    6.  
    7.  
    8.     private void Awake()
    9.     {
    10.         if (instance == null)
    11.             instance = this;
    12.         else if (instance != null)
    13.             Destroy(gameObject);
    14.  
    15.         envorinmentMusicTime = envorinmentMusicTimer;
    16.        
    17.     }
    18.     private void Start()
    19.     {
    20.         //Music starts at 60 seconds and repeat every 50 seconds
    21.         InvokeRepeating("envorinmentMusic",60f,50f);
    22.     }
    23.  
    24.  
    25.    
    26.  
    27.     //--------Movement Sounds--------//
    28.     public void playFoodStep()
    29.     {
    30.         if (!audios[1].isPlaying)
    31.         {
    32.             //Random volume for realistic sound
    33.             audios[1].volume = Random.Range(0.2f, 0.5f);
    34.             //audios[1].pitch = Random.Range(0.8f, 1.2f);
    35.             audios[1].Play();
    36.         }      
    37.     }
    38.     public void walkSound()
    39.     {
    40.         audios[1].pitch = 1f;
    41.     }
    42.     public void runSound()
    43.     {
    44.         audios[1].pitch = 1.3f;
    45.     }
    46.     public void stopFoodStep()
    47.     {
    48.         audios[1].Stop();
    49.     }
    50.  
    51.  
    52.     //-----Weapon Sounds-----//
    53.     public void fireSound()
    54.     {      
    55.        audios[2].Play();
    56.     }
    57.  
    58.     public void reloadSound()
    59.     {
    60.         if (!audios[3].isPlaying)
    61.             audios[3].Play();
    62.     }
    63.  
    64.  
    65.     //--Ambiance musics--//
    66.     public void envorinmentMusic()
    67.     {
    68.         if(!audios[4].isPlaying && !audios[5].isPlaying)
    69.         {
    70.             audios[Random.Range(4, 6)].Play();                    
    71.         }            
    72.     }
    73.  
    74.  
    75. }
    76.