Search Unity

Turn Off Sound And Music Individually?

Discussion in 'Scripting' started by Dogg, Jul 29, 2014.

  1. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    As you read in the title, I want to know how to turn off the sound of my game, without turning off the music, and if I want to turn off the music, it will not affect the sound. So far, I haven't been able to do so. I've only been able to turn off both the sound and music by using AudioListener.volume = 0;, but not individually. So how can I do it?

    I have only two audio sounds. One for the jump sound which is part of my character controller, and the music audio which is a different script itself, on a different gameobject. Here's my menu script in which I use to turn off the sound: http://pastebin.com/5agXeq65 Here's the Music Script: http://pastebin.com/3B7YQJ9k And finally the jump script which again is apart of my character controller script. I only took it out because my character controller script is too big for you guys to read: http://pastebin.com/2NRKyyaH

    Please help with what I have here if you want to. I've haven't been able to figure out how to do this correctly for about a month now.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You can disable each AudioSource (that is, the same thing you're using to play the music/sounds in the first place) by setting audio.volume to 0, or audio.enabled to false, or audio.Pause().
     
  3. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Thank you. I will try it out. EDIT: Alright I have a question. How can I get the audiosource, from a different scene? The sound menu script I posted is in a different scene than the jump sound and music audio. It also is on a different gameobject, so how can I disable the audiosource of a different scene from the sound menu? I'll post pictures to show you.

    EDIT2: Alright here's some pictures: First the Sound Menu Scene, the picture is of the main camera in that scene with the sound menu script attached to it: http://imgur.com/kCY7OtC
    Next is the jump audio, which is located in a different scene that has the player in it(it's the game itself). The picture shows only a little bit of the character controller. The jump audio is apart of it: http://imgur.com/Y96FwGW
    Last is the music script, which is on an empty gamobject named music attached to the Player GameObject. http://imgur.com/a3lyJP2
     
    Last edited: Jul 29, 2014
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    I suggest you create an AudioManager and pool your effects. This way you can switch off / turn down the volume of your objects at will.
     
  5. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Interesting, I never used that before. I'll see what I can do with it. Thanks for the suggestion.
     
  6. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    What you would want is something like this.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public sealed class AudioManager : MonoBehaviour {
    5.     private static readonly string _tag = "Audio Manager"; // You need to create this.
    6.     private static AudioManager _instance = null; // Cache for single instantiation.
    7.  
    8.     private AudioManager() { } // Private constructor.
    9.  
    10.     public static AudioManager AudioInstance {
    11.         get {
    12.             if(AudioManager._instance == null) {
    13.                 GameObject audioManager = GameObject.FindGameObjectWithTag(AudioManager._tag);
    14.  
    15.                 if(audioManager != null) {
    16.                     AudioManager._instance = audioManager.GetComponent<AudioManager>();
    17.                 }
    18.  
    19.                 if(AudioManager._instance == null) {
    20.                     Debug.LogError(typeof(AudioManager).ToString() + " - AudioInstance: Could not find scene instance with tag '" + AudioManager._tag + "'");
    21.                 }
    22.             }
    23.  
    24.             return AudioManager._instance;
    25.         }
    26.     }
    27.  
    28.     public AudioItem[] musicAudio = new AudioItem[0];
    29.     public AudioItem[] soundEffects = new AudioItem[0];
    30.  
    31.     public void PlayMusic(string audioName) {
    32.         this.PlayMusic(audioName, 3.0f);
    33.     }
    34.  
    35.     public void PlayMusic(string audioName, float crossFade) {
    36.         this.PlayMusic(audioName, crossFade, default(Vector3));
    37.     }
    38.  
    39.     public void PlayMusic(string audioName, float crossFade, Vector3 position) {
    40.         foreach(AudioItem musicItem in this.musicAudio) {
    41.             if(musicItem.audioName == audioName) {
    42.                 // Play audio add the effects I listed above. We could go into more detail but this is what I would do.
    43.             }
    44.         }
    45.     }
    46.  
    47. }
    48.  
    49. [System.Serializable]
    50. public class AudioItem : System.Object {
    51.     public string audioName = ""; // Name to be used to call the audio clip.
    52.     public AudioClip audioClip; // Actual audio item.
    53. }
    54.  
    I whipped this up right now but hopefully you get the basics behind what you can accomplish here. Ideally you would want to make a prefab so it can persistent throughout all the scenes and pool your audio sources.

    Screen Shot Audio Manager.png
     
    ferverence likes this.
  7. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Yeah I was reading more about it and I am currently doing a tutorial available on Unity. Thanks for the script and explanation though it really helps me understand it even more. I'll see if I can get it to work the way I want after I finish the tutorial. Thanks again.
     
  8. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    No worries if you need any help just post.
     
  9. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Okay so I finished the tutorial and messed around with the script you posted. So I created a empty GameObject with your script attached and a Audio Source. After setting up what I wanted in the inspector I turned it into a prefab like you suggested. Now the thing is, I'm a little confused with you script. I know how to work the inspector part of it, but not actually getting the audio to play. I've created a test script to see if it would play any audio, and the problem is, is that I don't know how to. I know in your script you said "// Play audio add the effects I listed above. We could go into more detail but this is what I would do." but I'm not sure which ones to add. Here's my test script that's attached to the prefab:

    Code (CSharp):
    1. public class Test01 : MonoBehaviour {
    2.  
    3.     public AudioItem[] soundEffects = new AudioItem[0];
    4.  
    5.     // Use this for initialization
    6.     void Start () {
    7.    
    8.     }
    9.    
    10.     // Update is called once per frame
    11.     void Update () {
    12.    
    13.         if(Input.GetKeyDown (KeyCode.Space))
    14.         {
    15.             //Play Audio
    16.             //I tried GameObject.Find("Audio Manager").SendMessage ("Play", "Jump"); doesn't work
    17.             //I tried this.PlayMusic(audioName, 3.0f); or something along those lines, doesn't work
    18.             //I know I'm doing it wrong, it's obvious thanks to the error I get:
    19.             //ERROR: SendMessage Play has no receiver! UnityEngine.GameObject:SendMessage(String, Object)
    20.         }
    21.     }
    22. }
    I hope I'm not asking for too much, but if you don't mind can you go into a little more detail on how to play certain audio clips? My understanding of your script is a little messy.:confused:
     
  10. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Lol no worries, what you have to do is instantiate them then pool them. By pooling you can play them whenever you need to at will and disable them whenever you need to. Let me write something up really quick.
     
    Dogg likes this.
  11. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    But what do I instantiate and pool? The audio itself or something else?
     
  12. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Alright so you still there?
     
  13. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Well if you are there, what you need to do is pool your audio clips onto a prefab. So what you want to do is instantiate a prefab with an AudioSource component attached to it. What I did was create a simple script (which you can add to it) that I attached to my AudioItem.prefab.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(AudioSource))]
    5. public class AudioItemSource : MonoBehaviour {
    6.     private string audioName;
    7.     private bool isPlaying;
    8.  
    9.     public string AudioName {
    10.         get {
    11.             return this.audioName;
    12.         } set {
    13.             this.audioName = value;
    14.         }
    15.     }
    16.  
    17.     public bool IsPlaying {
    18.         get {
    19.             return this.isPlaying;
    20.         } set {
    21.             this.isPlaying = value;
    22.         }
    23.     }
    24.  
    25.     public void Play() {
    26.         this.GetComponent<AudioSource>().Play();
    27.     }
    28. }
    29.  
    Create a prefab name it whatever you want then attach this script to it.
     
  14. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    AudioManager:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public sealed class AudioManager : MonoBehaviour {
    6.     private static readonly string _tag = "Audio Manager"; // You need to create this.
    7.     private static AudioManager _instance = null; // Cache for single instansitation.
    8.  
    9.     private AudioManager() { } // Private constrictor.
    10.  
    11.     public static AudioManager AudioInstance {
    12.         get {
    13.             if(AudioManager._instance == null) {
    14.                 GameObject audioManager = GameObject.FindGameObjectWithTag(AudioManager._tag);
    15.  
    16.                 if(audioManager != null) {
    17.                     AudioManager._instance = audioManager.GetComponent<AudioManager>();
    18.                 }
    19.  
    20.                 if(AudioManager._instance == null) {
    21.                     Debug.LogError(typeof(AudioManager).ToString() + " - AudioInstance: Could not find scene instance with tag '" + AudioManager._tag + "'");
    22.                 }
    23.             }
    24.  
    25.             return AudioManager._instance;
    26.         }
    27.     }
    28.  
    29.     public GameObject audioPredab;
    30.     public AudioItem[] musicAudio = new AudioItem[0];
    31.     public AudioItem[] soundEffects = new AudioItem[0];
    32.     private List<GameObject> musicPool = new List<GameObject>();
    33.     private List<GameObject> soundEffectsPool = new List<GameObject>();
    34.  
    35.     void Start() {
    36.         this.PoolMusic();
    37.         this.StartCoroutine (TestPlay ());
    38.     }
    39.  
    40.     IEnumerator TestPlay() {
    41.         yield return new WaitForSeconds(5.0f);
    42.         Debug.Log ("Invoked!");
    43.         this.PlayMusic("Intro");
    44.     }
    45.  
    46.     public void PlayMusic(string audioName) {
    47.         this.PlayMusic(audioName, 3.0f);
    48.     }
    49.  
    50.     public void PlayMusic(string audioName, float crossFade) {
    51.         this.PlayMusic(audioName, crossFade, default(Vector3));
    52.     }
    53.  
    54.     public void PlayMusic(string audioName, float crossFade, Vector3 position) {
    55.         foreach(AudioItem musicItem in this.musicAudio) {
    56.             if(musicItem.audioName == audioName) {
    57.                 foreach(GameObject pooledAudio in this.musicPool) {
    58.                     if(pooledAudio.GetComponent<AudioItemSource>().AudioName == audioName && pooledAudio.GetComponent<AudioItemSource>().IsPlaying == false) {
    59.                         Debug.Log("Playing: " + audioName);
    60.                         pooledAudio.SetActive(true);
    61.                         pooledAudio.GetComponent<AudioItemSource>().Play();
    62.                     }
    63.                 }
    64.             }
    65.         }
    66.     }
    67.  
    68.     public void MuteMusic() {
    69.         foreach(AudioItem audioItem in this.musicAudio) {
    70.  
    71.         }
    72.     }
    73.  
    74.     public void UnMuteMusic() {
    75.         foreach(AudioItem audioItem in this.musicAudio) {
    76.             // audioItem.audioClip.mute = false;
    77.         }
    78.     }
    79.  
    80.     public void MusicVolume(float volume) {
    81.         foreach(AudioItem audioItem in this.musicAudio) {
    82.             // audioItem.audioClip.volume = volume;
    83.         }
    84.     }
    85.  
    86.     private void PoolMusic() {
    87.         foreach(AudioItem audioItem in this.musicAudio) {
    88.             if(audioItem.audioClip != null) {
    89.                 GameObject tempAudio = Instantiate(this.audioPredab, default(Vector3), Quaternion.identity) as GameObject;
    90.  
    91.                 tempAudio.name = "AudioItem::" + audioItem.audioName;
    92.                 tempAudio.transform.parent = this.gameObject.transform;
    93.                 tempAudio.GetComponent<AudioSource>().audio.clip = audioItem.audioClip;
    94.                 tempAudio.GetComponent<AudioItemSource>().AudioName = audioItem.audioName;
    95.                 tempAudio.GetComponent<AudioItemSource>().IsPlaying = false;
    96.                 tempAudio.SetActive(false);
    97.                 this.musicPool.Add(tempAudio);
    98.             }
    99.         }
    100.     }
    101.  
    102.     private void PoolSoundEffects() {
    103.         // Same as PoolMusic
    104.     }
    105. }
    106.  
    107. [System.Serializable]
    108. public class AudioItem : System.Object {
    109.     public string audioName = ""; // Name to be used to call the audio clip.
    110.     public AudioClip audioClip; // Actual audio item.
    111. }
    112.  
    Audio Item we have to create this has 2 components AudioItemSource and AudioSource.

    Screen Shot Audio Item.png
     
    Last edited: Jul 31, 2014
  15. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Our updates to AudioManager

    Screen Shot New Audio Manager.png
     
  16. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    To play a music track from AudioManager you would use this line of code.

    AudioManager.Instance.PlayMusic("Intro");
     
  17. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Keep in mind this is not complete so don't expect it to work 100% this is at 40% working, the rest is up to you unless like I said you need help.
     
  18. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Sorry I had to pick someone up. Anyways wow thanks for this. I'll get to working on it right away.
     
  19. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Sorry quick question, am I supposed to get errors? You said it was only 40%, so is that what you meant? Errors:

    Code (CSharp):
    1. The type or namespace name `List`1' could not be found. Are you missing a using directive or an assembly reference?
    Code (CSharp):
    1. The type or namespace name `List`1' could not be found. Are you missing a using directive or an assembly reference?
     
  20. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Did you get all of the 'using' lines at the top when you copied the script?
     
    Polymorphik likes this.
  21. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Ah so that's what it was. Super sorry about this. Thanks and again sorry.
     
  22. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Have you gotten it to work yet? I have updated some of the code if you need it.
     
  23. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Not yet, if it's a code to help make it from 40% to even more complete then don't give it to me yet. I would like to try and figure this out on my own. If I can't, then I'll come back here and then you can give me more code. Thanks a bunch for the help. I really appreciate this.
     
  24. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Alright so I got it to finally play one song. All I had to do was follow your instructions carefully and now I got it playing. Though I changed the yield return new from 5.0f to this:
    Code (CSharp):
    1. IEnumerator TestPlay() {
    2.         yield return new WaitForSeconds(0.0f);
    because I didn't want the music to play 5 seconds after I start. I have some questions though.

    How can I loop the first song? How do I play sound effects when I jump/press the space key? My first guess would be to use private void PoolSoundEffects() { or create a new function called void PlaySoundEffects. And finally how can I play the second song when I collect a power up, then change it back to the first song?(Might be a little tricky, I managed to do it before this but it always changed back to the first song when I grabbed the power up again).
     
  25. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    That was a test, you don't require it. Let me add something to it really quick
     
  26. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    So here is a major overhaul to AudioManager and AudioItemSource.

    AudioManager.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class AudioManager : MonoBehaviour {
    6.     private static readonly string _tag = "Audio Manager";
    7.  
    8.     #region Singleton Instance
    9.     private static AudioManager _instance = null;
    10.  
    11.     public static AudioManager AudioInstance {
    12.         get {
    13.             if (AudioManager._instance == null) {
    14.                 GameObject audioManager = GameObject.FindGameObjectWithTag(AudioManager._tag);
    15.              
    16.                 if (audioManager != null) {
    17.                     AudioManager._instance = audioManager.GetComponent<AudioManager>();
    18.                 }
    19.              
    20.                 if (AudioManager._instance == null) {
    21.                     Debug.LogError(typeof(AudioManager).ToString() + " - Instance: Could not find Audio Manager in scene!");
    22.                 }
    23.             }
    24.          
    25.             return AudioManager._instance;
    26.         }
    27.     }
    28.     #endregion
    29.  
    30.     public GameObject audioTrack; // Required
    31.     public AudioItem[] musicList = new AudioItem[0];
    32.     public AudioItem[] soundEffects = new AudioItem[0];
    33.  
    34.     public List<GameObject> musicPool = new List<GameObject>();
    35.     public List<GameObject> soundPool = new List<GameObject>();
    36.  
    37.     void Start() {
    38.         this.CreateMusicPool();
    39.         this.CreateSoundPool();
    40.     }
    41.  
    42.     void AddToSoundPool(string trackName) {
    43.      
    44.     }
    45.  
    46.     void CreateMusicPool() {
    47.         foreach (AudioItem audioItem in this.musicList) {
    48.             if (audioItem.trackClip != null) {
    49.                 GameObject newTrack;
    50.              
    51.                 if (audioItem.overrideAudioTrack != null) {
    52.                     newTrack = Instantiate(audioItem.overrideAudioTrack, default(Vector3), Quaternion.identity) as GameObject;
    53.                 }
    54.                 else {
    55.                     newTrack = Instantiate(this.audioTrack, default(Vector3), Quaternion.identity) as GameObject;
    56.                 }
    57.              
    58.                 if (newTrack != null) {
    59.                     if (newTrack.GetComponent<AudioItemSource>() != null) {
    60.                         newTrack.GetComponent<AudioItemSource>().SetTrack(audioItem);
    61.                         newTrack.transform.parent = this.gameObject.transform;
    62.                         newTrack.SetActive(false);
    63.                         this.musicPool.Add(newTrack);
    64.                     }
    65.                     else {
    66.                         Debug.LogWarning(typeof(AudioManager).ToString() + " - Music Pool: Overriden Track Prefab does not contain component AudioItemSource for " + audioItem.trackName + "!");
    67.                     }
    68.                 }
    69.             }
    70.         }
    71.     }
    72.  
    73.     void CreateSoundPool() {
    74.         foreach (AudioItem audioItem in this.soundEffects) {
    75.             if (audioItem.trackClip != null) {
    76.                 GameObject newTrack;
    77.              
    78.                 if (audioItem.overrideAudioTrack != null) {
    79.                     newTrack = Instantiate(audioItem.overrideAudioTrack, default(Vector3), Quaternion.identity) as GameObject;
    80.                 }
    81.                 else {
    82.                     newTrack = Instantiate(this.audioTrack, default(Vector3), Quaternion.identity) as GameObject;
    83.                 }
    84.              
    85.                 if (newTrack != null) {
    86.                  
    87.                     if (newTrack.GetComponent<AudioItemSource>() != null) {
    88.                         newTrack.GetComponent<AudioItemSource>().SetTrack(audioItem);
    89.                         newTrack.transform.parent = this.gameObject.transform;
    90.                         newTrack.SetActive(false);
    91.                         this.soundPool.Add(newTrack);
    92.                     }
    93.                     else {
    94.                         Debug.LogWarning(typeof(AudioManager).ToString() + " - Sound Effects Pool: Overriden Track Prefab does not contain component AudioItemSource for " + audioItem.trackName + "!");
    95.                     }
    96.                 }
    97.             }
    98.         }
    99.     }
    100.  
    101.     public void PlayMusic(string trackName) {
    102.         this.PlayMusic(trackName, default(Vector3));
    103.     }
    104.  
    105.     public void PlayMusic(string trackName, Vector3 position) {
    106.         this.PlayMusic(trackName, position, this.gameObject.transform);
    107.     }
    108.  
    109.     public void PlayMusic(string trackName, Vector3 position, Transform parent) {
    110.         foreach (GameObject pooledTrack in this.musicPool) {
    111.             if (pooledTrack.GetComponent<AudioItemSource>().TrackName == trackName) {
    112.                 pooledTrack.transform.parent = parent;
    113.                 pooledTrack.transform.position = position;
    114.                 pooledTrack.SetActive(true);
    115.                 pooledTrack.GetComponent<AudioItemSource>().Play();
    116.                 return;
    117.             }
    118.         }
    119.      
    120.         Debug.LogWarning(typeof(AudioManager).ToString() + "PlayMusic: Could not find music track '" + trackName + "'!");
    121.     }
    122.  
    123.     public void PlaySound(string trackName) {
    124.         this.PlaySound(trackName, default(Vector3));
    125.     }
    126.  
    127.     public void PlaySound(string trackName, Vector3 position) {
    128.         this.PlaySound(trackName, position, this.gameObject.transform);
    129.     }
    130.  
    131.     public void PlaySound(string trackName, Vector3 position, Transform parent) {
    132.         foreach (GameObject pooledTrack in this.soundPool) {
    133.             if (pooledTrack.GetComponent<AudioItemSource>().TrackName == trackName) {
    134.                 pooledTrack.transform.parent = parent;
    135.                 pooledTrack.transform.position = position;
    136.                 pooledTrack.SetActive(true);
    137.                 pooledTrack.GetComponent<AudioItemSource>().Play();
    138.                 return;
    139.             }
    140.         }
    141.      
    142.         Debug.LogWarning(typeof(AudioManager).ToString() + "PlaySound: Could not find sound effect '" + trackName + "'!");
    143.     }
    144.  
    145.     public string[] GetMusicList() {
    146.         List<string> music = new List<string>();
    147.      
    148.         foreach (AudioItem trackInfo in this.musicList) {
    149.             music.Add(trackInfo.trackName);
    150.         }
    151.      
    152.         return music.ToArray();
    153.     }
    154.  
    155.     public string[] GetSoundList() {
    156.         List<string> soundList = new List<string>();
    157.      
    158.         foreach (AudioItem trackInfo in this.soundEffects) {
    159.             soundList.Add(trackInfo.trackName);
    160.         }
    161.      
    162.         return soundList.ToArray();
    163.     }
    164. }
    165.  
    166. [System.Serializable]
    167. public class AudioItem : System.Object {
    168.     public string trackName;
    169.     public AudioClip trackClip;
    170.     public float customVolume;
    171.     public float startDelay;
    172.     public float fadeIn;
    173.     public float fadeOut;
    174.     public float pitch;
    175.     public GameObject overrideAudioTrack;
    176.     public bool enableLoop;
    177.     public bool persist;
    178.     public int maxInstances;
    179. }
    180.  
    AudioItemSource.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(AudioSource))]
    5. public class AudioItemSource : MonoBehaviour {
    6.     private float delay;
    7.     private bool isPlaying;
    8.     private float fadeIn;
    9.     private float fadeOut;
    10.     private bool loop;
    11.     private bool persist;
    12.     private float pitch;
    13.     private string trackName;
    14.     private float trackLength;
    15.     public float trackTime;
    16.     private float volume;
    17.     private float fadeOutTime;
    18.  
    19.     public float Delay {
    20.         get {
    21.             return this.delay;
    22.         }
    23.         set {
    24.             this.delay = value;
    25.         }
    26.     }
    27.  
    28.     public bool IsPlaying {
    29.         get {
    30.             return this.isPlaying;
    31.         }
    32.         set {
    33.             this.isPlaying = value;
    34.         }
    35.     }
    36.  
    37.     public float FadeIn {
    38.         get {
    39.             return this.fadeIn;
    40.         }
    41.         set {
    42.             this.fadeIn = value;
    43.         }
    44.     }
    45.  
    46.     public float FadeOut {
    47.         get {
    48.             return this.fadeOut;
    49.         }
    50.         set {
    51.             this.fadeOut = value;
    52.         }
    53.     }
    54.  
    55.     public bool Loop {
    56.         get {
    57.             return this.loop;
    58.         }
    59.         set {
    60.             this.loop = value;
    61.         }
    62.     }
    63.  
    64.     public bool Persist {
    65.         get {
    66.             return this.persist;
    67.         }
    68.         set {
    69.             this.persist = value;
    70.         }
    71.     }
    72.  
    73.     public float Pitch {
    74.         get {
    75.             return this.pitch;
    76.         }
    77.         set {
    78.             this.pitch = value;
    79.         }
    80.     }
    81.  
    82.     public float TrackLength {
    83.         get {
    84.             return this.trackLength;
    85.         }
    86.         set {
    87.             this.trackLength = value;
    88.         }
    89.     }
    90.  
    91.     public string TrackName {
    92.         get {
    93.             return this.trackName;
    94.         }
    95.         set {
    96.             this.trackName = value;
    97.         }
    98.     }
    99.  
    100.     public float TrackTime {
    101.         get {
    102.             return this.trackTime;
    103.         }
    104.         set {
    105.             this.trackTime = value;
    106.         }
    107.     }
    108.  
    109.     public float Volume {
    110.         get {
    111.             return this.volume;
    112.         }
    113.         set {
    114.             this.volume = value;
    115.         }
    116.     }
    117.  
    118.     void CheckLoop() {
    119.         if (this.Loop == true) {
    120.             this.Play();
    121.         }
    122.         else {
    123.             this.transform.position = default(Vector3);
    124.             this.gameObject.SetActive(false);
    125.         }
    126.     }
    127.  
    128.     public void Play() {
    129.         this.StopCoroutine("PlayTrack");
    130.         this.StartCoroutine("PlayTrack");
    131.     }
    132.  
    133.     IEnumerator PlayTrack() {
    134.      
    135.         yield return new WaitForSeconds(this.Delay);
    136.      
    137.         this.GetComponent<AudioSource>().Play();
    138.      
    139.         this.StopCoroutine("StartFadeIn");
    140.         this.StopCoroutine("StartFadeOut");
    141.         this.StartCoroutine("StartFadeIn");
    142.      
    143.         while (this.GetComponent<AudioSource>().isPlaying == true) {
    144.             this.IsPlaying = true;
    145.             this.TrackTime = this.GetComponent<AudioSource>().time;
    146.          
    147.             if (this.TrackTime >= this.fadeOutTime) {
    148.                 this.StartCoroutine("StartFadeOut");
    149.             }
    150.          
    151.             yield return null;
    152.         }
    153.      
    154.         this.IsPlaying = false;
    155.         this.TrackTime = 0.0f;
    156.         this.CheckLoop();
    157.     }
    158.  
    159.     IEnumerator StartFadeIn() {
    160.         float timeElapsed = 0.0f;
    161.      
    162.         Debug.Log("Fading In....");
    163.      
    164.         while (timeElapsed <= this.FadeIn) {
    165.          
    166.             if (this.FadeIn != 0.0f) {
    167.                 this.GetComponent<AudioSource>().volume = Mathf.Lerp(0.0f, this.Volume, timeElapsed / this.FadeIn);
    168.             }
    169.          
    170.             timeElapsed += Time.deltaTime;
    171.             yield return new WaitForEndOfFrame();
    172.         }
    173.      
    174.         this.GetComponent<AudioSource>().volume = this.Volume;
    175.     }
    176.  
    177.     IEnumerator StartFadeOut() {
    178.         float timeElapsed = 0.0f;
    179.      
    180.         Debug.Log("Fading out....");
    181.      
    182.         while (this.TrackTime <= this.TrackLength) {
    183.             if (this.FadeOut != 0) {
    184.                 this.GetComponent<AudioSource>().volume = Mathf.Lerp(this.Volume, 0.0f, timeElapsed / this.FadeOut);
    185.             }
    186.          
    187.             timeElapsed += Time.deltaTime;
    188.             yield return null;
    189.         }
    190.      
    191.         this.GetComponent<AudioSource>().volume = 0.0f;
    192.     }
    193.  
    194.     public void SetTrack(AudioItem audioItem) {
    195.         this.GetComponent<AudioSource>().audio.clip = audioItem.trackClip;
    196.         this.GetComponent<AudioSource>().volume = 0.0f;
    197.         this.Delay = audioItem.startDelay;
    198.         this.FadeIn = audioItem.fadeIn;
    199.         this.FadeOut = audioItem.fadeOut;
    200.         this.Loop = audioItem.enableLoop;
    201.         this.Persist = audioItem.persist;
    202.         this.Pitch = audioItem.pitch;
    203.         this.TrackLength = audioItem.trackClip.length;
    204.         this.TrackName = audioItem.trackName;
    205.         this.Volume = audioItem.customVolume;
    206.      
    207.         this.name = "Audio Track - " + this.TrackName;
    208.         this.fadeOutTime = this.TrackLength - this.FadeOut;
    209.         this.CheckPersist();
    210.     }
    211.  
    212.     void CheckPersist() {
    213.         if (this.Persist == true) {
    214.             DontDestroyOnLoad(this.gameObject);
    215.         }
    216.     }
    217. }
     
  27. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    To play an effect just call AudioManager.

    Specify which sound effect to play
    AudioManager.AudioInstance.PlaySound("Jump");

    Specify which sound effect to play AND position.
    AudioManager.AudioInstance.PlaySound("Jump", (position here));

    Specify which sound effect to play AND position AND parent.
    AudioManager.AudioInstance.PlaySound("Jump", (position here), (parent here));

    So there are 3 ways of doing it. Same goes with the music.
     
  28. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Okay then, I'll see what I can do. Question: Where do you play it from? So like let's say I chose the first way of doing it, would I use it in the audio manager script, the audioItemSource script, or the Player script?
     
  29. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    And wow your right there's a whole lot more features to the AudioManager and AudioItem now. This looks interesting(and awesome). Thank you.
     
  30. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    One more thing, I may be doing this wrong so tell me if I am. After you posted the newest updated Audio Manager script and AudioItemSource script, I couldn't get the music to play again. So I created an empty GameObject and put one of my audio tracks on it, and then I put it where it says Audio Track on the Audio Manager in the inspector. So now it plays. I know I have to be doing this wrong because then what would be the point of having track name and track clip in the inspector?
     
  31. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Wherever you need the instance to be called from.
     
  32. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    No worries this is like 70% there are some bugs in it that I am going to look at and them here. Hope it works for you.
     
  33. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Before we had the TestPlay() or something I forgot what I called it. That would play a song at run time automatically. I took that off as it was for debugging purposes. What you need to do is create a script that connects to AudioManager and calls the method you for you to play the clip you want. This works the same for the sound effects.
     
  34. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    By connect do you mean GetComponent from the new script I will create or put it on the AudioManager prefab?
     
  35. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    No, what I mean is from any other script that requires some audio clip to be played. For instance in your Player script, suppose you have a if statement to check for lets say when the player jumps.

    Code (CSharp):
    1.         if (Input.GetMouseButtonDown(0)) {
    2.             this.Jump(); // Some method you created that makes the player jump.
    3.             AudioManager.AudioInstance.PlaySound("Jump"); // Calls the AudioManager and request for it to play "Jump" audio clip.
    4.         }
     
  36. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Ah I see now okay then thanks.
     
  37. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    If you need anymore help just post.
     
  38. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Update: Okay so I'm able to play one sound effect when I press the jump button, and also able to play one music track when I collect a power up however, I still can not figure out how to play a music track on start up. When I press play it takes me straight into the game because my main menu is a different scene, so how can I play it on start? Also how do you stop music?
     
  39. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
  40. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    This can be a very useful thing for me in the future, as it's something I plan to use for a thing I have planned. However, how exactly does this work for what I want now? How does this play the first audio track on start, or stops the music? Thanks for the help.
     
  41. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I may have misunderstood what your last comment was asking? I thought you were saying you wanted the music to keep playing from the main menu into the level.
     
  42. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    My script takes care of songs to persists. In the AudioManager just check the Persist to true and it will persist throughout the scene. I still need to update it so it can still connect to the AudioManager. I am working on that though and I have the Stop / Pause working now.
     
  43. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    If you look at the AudioSourceItem I made a method for that. If you look on line 177 of AudioManager public bool persist; there is a check box you can tick to make that specific audio clip persist throughout all the scenes.
    1. void CheckPersist() {
    2. if (this.Persist == true) {
    3. DontDestroyOnLoad(this.gameObject);
    4. }
    5. }
     
  44. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Oh okay then sorry for the misunderstanding. I was asking how can I play the music on start up. I have two scenes, one for the main menu, and another for the actual game itself/the first level. So far the two audio clips(my jump sound effect and music when I grab a powerup) are on the actual game scene. What I want to do, is add another music track to the actual game scene, and when I press play on the same scene, the music starts automatically and I play the game. I think I'll make a video to show you guys what I mean.
     
  45. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    That's a great feature, I didn't know persist does that. So what does override audio do then?
     
  46. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Override audio is so for you to have a custom AudioSource where you want to modify the 3D log graph. You cannot edit this via code BUT with my script you can override the basic AudioItem prefab with a custom one for a specific AudioSource here you can see how by changing the graph you can change the way the 3D audio sound drops off as you get closer and further away from it.
     
  47. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Okay then I see thanks. Hey, do you know how to play a song on start up with audio manager? I'm still having trouble with that.
     
  48. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Yes, what you need to do is in Start() method just call AudioManager.

    So like this...

    void Start() {
    AudioManager.Instance.PlayMusic("Intro");
    }

    I noticed a bug though so i am looking into fixing a persistence error. What is happening is that if the clip is supposed to persists its still getting destroyed because it is parented to AudioManager so I am trying to set its parent to null but for some reason its not working out. When I get this fixed I will post it here.
     
  49. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Okay then thanks. Yeah I tried this out before and it didn't work, but I'll try it again.
     
  50. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Right like I said before I just typed this out really fast I wasn't really checking for bugs, but I like what I am making so I might continue to add to it and probably put this on the Asset store for cheap.