Hi,
Been struggling to find a solution to this.
I have a mute music button in my game. The status is saved in PlayerPrefs for the next time the user loads the app. I can’t seem to get Unity Ads to mute at all.
I have unity Ads load in separate scenes - once after my game intro and once after game over.
I have a global boolean - “music” - which is set to either true or false according to the playerPrefs setting on my intro scene. It is working fine except in the ads.
I have tried in numerous places:
if (!GlobalVar.music)
AudioListener.volume = 0;
Isn’t there an Advertisement.volume type method?
Here is my unity ads script for my intro adverts. Can’t truely remember where I originally lifted it from to be honest:
using UnityEngine;
using UnityEngine.Advertisements;
using System.Collections;
public class UnityAds : MonoBehaviour
{
void Start ()
{
Advertisement.Initialize ("XXXXX", false);
StartCoroutine (ShowAdWhenReady ());
}
IEnumerator ShowAdWhenReady()
{
while (!Advertisement.isReady ())
yield return null;
DoShowAd(null);
}
// Use this method to show an ad from an external class.
public static void ShowAd (string zone = null)
{
if (Advertisement.isInitialized)
{
if (Advertisement.isReady(zone)) DoShowAd(zone);
else Debug.LogWarning("Unable to show advertisement. Placement zone is not ready.");
}
else Debug.LogError("Failed to show advertisement. Unity Ads is not initialized.");
}
private static void DoShowAd (string zone = null, int version = 0)
{
if (!GlobalVar.music)
AudioListener.volume = 0;
// Functionality wise, the following two DoShowAd methods do exactly the same thing.
// The CondensedVersion is simply a clever way of writing the ExpandedVersion.
// They are provided here as examples of two different styles of coding.
switch (version)
{
case 0:
DoShowAd_CondensedVersion(zone);
break;
case 1:
DoShowAd_ExpandedVersion(zone);
break;
}
}
private static void DoShowAd_CondensedVersion (string zone, bool pauseGameDuringAd = true)
{
if (!GlobalVar.music)
AudioListener.volume = 0;
Advertisement.Show(zone, new ShowOptions {
// With the pause option set to true, the timeScale and AudioListener
// volume for your game is set to 0 while the ad is shown.
pause = pauseGameDuringAd,
// The resultCallback is triggered when the ad is closed.
resultCallback = result => {
HandleShowResult(result);
}
});
if (!GlobalVar.music)
AudioListener.volume = 0;
}
private static void DoShowAd_ExpandedVersion (string zone, bool pauseGameDuringAd = true)
{
if (!GlobalVar.music)
AudioListener.volume = 0;
ShowOptions options = new ShowOptions();
// With the pause option set to true, the timeScale and AudioListener
// volume for your game is set to 0 while the ad is shown.
options.pause = pauseGameDuringAd;
// The resultCallback is triggered when the ad is closed.
options.resultCallback = HandleShowResult;
Advertisement.Show(zone,options);
if (!GlobalVar.music)
AudioListener.volume = 0;
}
private static void HandleShowResult (ShowResult result)
{
switch (result)
{
case ShowResult.Finished:
Debug.Log("The ad was successfully shown.");
break;
case ShowResult.Skipped:
Debug.Log("The ad was skipped before reaching the end.");
break;
case ShowResult.Failed:
Debug.LogError("The ad failed to be shown.");
break;
}
Application.LoadLevel ("Menu");
AudioListener.volume = 1;
}
}
Many Thanks