Search Unity

Volume While Ads Are Playing

Discussion in 'Scripting' started by DaRealMik, Jan 26, 2020.

  1. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    I have a script that plays an ad every time you press the 'play again' or 'home' buttons.
    Code (CSharp):
    1. public void ShowAds()
    2.     {
    3.         if (Advertisement.IsReady("video"))
    4.         {
    5.             Advertisement.Show("video");
    6.             if (Advertisement.isShowing)
    7.             {
    8.                 Time.timeScale = 0f;
    9.             }
    10.         }
    11.     }
    This works perfectly fine, however I need to set my volume to 0 when the ad is playing and set it back to whatever it was if it isn't. I currently set my volume using a UI slider.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Audio;
    3. using UnityEngine.UI;
    4.  
    5. public class OptionsMenu : MonoBehaviour {
    6.  
    7.     public AudioMixer mixer;
    8.     public Slider slider;
    9.  
    10.     public void SetVolume(float volume)
    11.     {
    12.         mixer.SetFloat("Volume", volume);
    13.     }
    14.  
    15.     public void Start()
    16.     {
    17.         SetVolume(PlayerPrefs.GetFloat("Volume", 0));
    18.         slider.value = PlayerPrefs.GetFloat("Volume");
    19.         Time.timeScale = 1f;
    20.     }
    21.  
    22.     public void OnDisable()
    23.     {
    24.         float Volume = 0f;
    25.  
    26.         mixer.GetFloat("Volume", out Volume);
    27.  
    28.         PlayerPrefs.SetFloat("Volume", Volume);
    29.         PlayerPrefs.Save();
    30.     }
    31.  
    32.  
    33.  
    34.  
    35. }
    36.  
    Please help!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Not sure what your question is. When you go play an ad, copy the current volume level, store it somewhere else, set it to zero, play the ad, and when you know the ad is done, restore the volume to both the slider and the mixer.
     
  3. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    I don't understand how to do this, can you provide some code or an example(sorry i'm kind of new to unity).
     
  4. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    Let me know if you need some other part of my code. Please help!
     
  5. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    Nvm, I figured it out. Thanks.