0
\$\begingroup\$

I am a bit of a novice here but I have created a game and I am showing ads when a scene is loaded but the background music keeps playing in the background. Is there a way in which the background music can be paused when the ad begins but resumes playing once the ad has finished?

I have an ad controller for the ad placements and an coroutine to play the ads once a specific scene has been loaded.

The background music is currently working using the inspector on a random background gameobject.

This is my code for the ad manager for the scene:

public static Ads instance;
private string playStoreID = "XXXXXXXX";

private string vidad = "XXXXXX";
private string rewardedVideoAD = "YYYYYYY";
private string BannerAD = "ZZZZZZ";



   private void Awake()
{
    if (instance != null)
    {

        Destroy(gameObject);
    }

    else
    {
        instance = this;
        DontDestroyOnLoad(gameObject);
    }
}

void Start()
{
    Monetization.Initialize(playStoreID, true);
}



public void ShowInterstitialAD()
{

   if (Monetization.IsReady(vidad))
    {
        ShowAdPlacementContent ad = null;
        ad = Monetization.GetPlacementContent(vidad) as ShowAdPlacementContent;


        if (ad != null)
        {
            ToggleMute();

         
            ad.Show(ToggleMute);
        }
    }

}

void ToggleMute(ShowResult finishState = default(ShowResult))
{
    audioToMute.mute = !audioToMute.mute;
}

public void ShowRewardVideoAD2()
{

    if (Monetization.IsReady(rewardedVideoAD))
    {
        ShowAdPlacementContent ad = null;
        ad = Monetization.GetPlacementContent(rewardedVideoAD) as ShowAdPlacementContent;


        if (ad != null)
        {
            ToggleMute();

   
            ad.Show(ToggleMute);
        }
    }

}


public void ShowRewardVideoAD()
{

    if (Monetization.IsReady(rewardedVideoAD))
    {
        ShowAdPlacementContent ad = null;
        ad = Monetization.GetPlacementContent(rewardedVideoAD) as ShowAdPlacementContent;


        if (ad != null)
        {
            
            ToggleMute();

           
            ad.Show(ToggleMute);
        }
    }

}

}

Here is a picture of the inspector... Inspector

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

Inside your ad script, add a field to point to the audio source that you want to mute:

public AudioSource audioToMute;

Be sure to wire this up in the Inspector so it references your background music source.

Then we prepare a method to mute/unmute this audio source each time we call it. In the Monetization API that looks like this ShowAdFinishCallback:

void ToggleMute(ShowResult finishState = default(ShowResult)) {
    audioToMute.mute = !audioToMute.mute;
}

Then in your show method:

if (Monetization.IsReady(rewardedVideoAD))
{
    ShowAdPlacementContent ad = null;
    ad = Monetization.GetPlacementContent(rewardedVideoAD) as ShowAdPlacementContent;


    if (ad != null)
    {
        // Mute our audio while the ad is shown.
        ToggleMute();

        // Show our ad and call back to our mute function once it finishes.
        ad.Show(ToggleMute);
    }
}

In the Advertisement API we can work very similarly, we just set up our callback as a listener, registered up-front, rather than part of the Show call:

// Declare our manager as implementing the IUnityAdsListener interface.
public class AdManager : MonoBehaviour, IUnityAdsListener {

    // All your existing member variables go here. I'll skip repeating that.

    // Handle the various messages this interface exposes:
    // (We don't need anything special with three of these, so we can ignore them)
    public void OnUnityAdsReady (string placementId) {}
    public void OnUnityAdsDidError (string message) {}
    public void OnUnityAdsDidStart (string placementId) {}

    // This is where we want to unmute our audio, after the ad is gone.
    public void OnUnityAdsDidFinish (string placementId, ShowResult showResult) {
        // Call the same ToggleMute method we defined in the first example.
        ToggleMute();
    }

    // When you initialize your ads in Start, also register this listener.
    void Start()
    {
        Advertisement.Initialize(playStoreID, true);
        Advertisement.AddListener(this);
}

IEnumerator ShowBannerWhenReady () {
    while (!Advertisement.IsReady (placementId)) {
        yield return new WaitForSeconds (0.5f);
    }

    // Mute our audio before showing the ad.
    ToggleMute();
    Advertisement.Banner.Show (placementId);
    // After the ad finishes, it will call the listener method we defined above.
}
\$\endgroup\$
4
  • \$\begingroup\$ Is there one for monetization API? \$\endgroup\$ Mar 17, 2020 at 13:30
  • \$\begingroup\$ unityads.unity3d.com/help/unity/api-unity \$\endgroup\$ Mar 17, 2020 at 15:20
  • \$\begingroup\$ I've updated this answer to show examples for both the Advertisement API and the Monetization API \$\endgroup\$
    – DMGregory
    Mar 17, 2020 at 21:53
  • \$\begingroup\$ Hi thanks for this it was very helpful. The only issue now is that once the ad has played and the audio has muted, then when we cycle back to that scene after playing the game the ad then doesn't play again? Is there a way to fix this too? I have edited the question to show my current code. \$\endgroup\$ Mar 30, 2020 at 12:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .