Detect when Unity Ads has been closed

Hi there,

I’ve been trying to figure out how to trigger a function when the unity ad has been closed.

Another solution would be to disable ingame clicks whilst unity ad is open and the enable them again once the close button has been pressed.

Currently Im able to play the game whilst the ad is showing which kinda spoils it.

THanks

Hi,

By passing in a ShowOptions object to Advertisement.Show(), you can specify a callback method which is called when ads is closed. E.g.

using UnityEngine;
using UnityEngine.Advertisements;

public class UnityAdsExample : MonoBehaviour
{
    public void ShowRewardedAd()
    {
        if (Advertisement.IsReady("rewardedVideoZone"))
        {
            var options = new ShowOptions { resultCallback = HandleShowResult };
            Advertisement.Show("rewardedVideoZone", options);
        }
    }

    private void HandleShowResult(ShowResult result)
    {
        switch (result)
        {
            case ShowResult.Finished:
                Debug.Log("The ad was successfully shown.");
                //
                // YOUR CODE TO REWARD THE GAMER
                // Give coins etc.
                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;
        }
    }
}

How are you able to have game continuing while ad is being shown? Game should be paused in that situation…

/Rasmus

3 Likes

I don’t know why the clicks are still working, this hasn’t happened in any other game i’ve worked on. I’ll try and figure it out.

Thanks for your help I’ll try and implement this now

Hi @rasmus-unity i would like to know if there is a way to detect the close button has been clicked?
so i can play a reward animation when the user closes the ad after it has finished playing, is there a way?
Kinda like how crossy roads does it, the coin animation plays after you have watched the video and then press the close button as it doesnt autocloses itself

Hope I understand you correctly, but that is what the HandleShowResult() method in above does, right?

Again, if you check for result==ShowResult.Finished like in above code, this is when you know that the user watched the ad to the end, and you can give reward.

As said, hope I understood your question correct. Feel free to follow up.

Thanks,
Rasmus

Yes this happens, but i need to know when the user clicks the close ad button top righ corner.
So when they get back to the game, i play an animation, if i play the animation while the result event is fired the user wont be able to see it, unless they close the unity ad video, right?

The game is paused while ad is being shown. So you can play your animation in the callback method. Doesn’t this work?

i haven’t tested, i’m assuming that it will try to play the animation while the video is still visible and the game is paused.
I’m just trying to cover bases, so i don’t have to come back and ask again :C.

Thanks for the assistance, i will come back and post my results.

@franciscochong , so you didn’t even try this before asking? :slight_smile:

We have written plenty of documentation and guides just for you, which you can e.g. find on http://unityads.unity3d.com/help/monetization/2.0-integration-guide-unity or https://docs.unity3d.com/Manual/UnityAdsHowTo.html if you are using engine integrated version of Ads SDK.

Implement it, and let us know if you have any issues.

Thanks,
Rasmus

Hi, What should I do if I need an event when ad closed button is pressed by user…In my game, I want to do a gameplay resumes when a user closes the ad window…I have tried on finished event but if closed button is pressed after some time of ad finished it looks like all messed up as I think ad finished event called when an ad is completed but not on a closed button.

Thanks, in advance

Hi @rasmus-unity, I also need an event when ad closed button is pressed by user in my game, and I also try on finished event, but it is trigger before player press closed button. Because I run a method which will ignore time scale=0 pause, if I put it in finished event, it will run before player press closed button.

Guys I have also small problem, when I open my UI Shop and when user clicks to watch reward video, after it’s finished rewarding is done, but when you click CLOSE BUTTON unity closes my shop …
It closes shop because My Exit button from shop is on same place as Ads Close Button and click goes through…

I can disable exit button when user starts watching video and enable it after video but how do I detect CLOSE Ads was clicked ?

OK let me now give solution :slight_smile:

So when you click CLOSE button then rewarding is happening and function is being executed to reward player, (had to check when value changed),

I managed to do it by declaring GameObject (exit button)
exitButton.GetComponent ().enabled = false;

I do this on ShowReward

public void ShowRewardedAd()
{
if (Advertisement.IsReady(“rewardedVideo”))
{
var options = new ShowOptions { resultCallback = HandleShowResult };
Advertisement.Show(“rewardedVideo”, options);
exitButton.GetComponent ().enabled = false;
}

}

and when click button on finished I enable it after 1s…

//Spawn ground platform back
public IEnumerator EnableExitButton ()
{

yield return new WaitForSeconds (1f);
exitButton.GetComponent ().enabled = true;

}

Make sure to enable exit button also on fail and error

Great solution.
But I have a problem. Where is “exitButton”? Where is initialized? Where can I find it?
Even with ASD opened does in not appear in the Unity Hierarchy.
I need to listen her click (to put time scale = 1)…

Still no solution for this. If you play animation when ad is finished it doesn’t show the animation properly.

In testmode it works perfect, once you disable testmode it doesn’t.

I have this issue too. I have a particle system I fire off when I get the OnUnityAdsShowComplete() event. But it always fires before the user has closed the rewarded ad window. I can tell because if I set the particle system to loop I can see it running. Is there a way to wait until the ad window is closed?

Now this 2023 in Ads SDK 4.4.1 and callback has gone!!!

You can detect the closing of the ads by using OnUnityAdsShowComplete() callback. You may check more information from the documentation.

https://docs.unity.com/ads/en/ImplementingBasicAdsUnity.html