Search Unity

Detect when Unity Ads has been closed

Discussion in 'Unity Ads & User Acquisition' started by will_brett, Sep 3, 2015.

  1. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    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
     
  2. rasmus-unity

    rasmus-unity

    Moderator

    Joined:
    Aug 15, 2014
    Posts:
    1,312
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3.  
    4. public class UnityAdsExample : MonoBehaviour
    5. {
    6.     public void ShowRewardedAd()
    7.     {
    8.         if (Advertisement.IsReady("rewardedVideoZone"))
    9.         {
    10.             var options = new ShowOptions { resultCallback = HandleShowResult };
    11.             Advertisement.Show("rewardedVideoZone", options);
    12.         }
    13.     }
    14.  
    15.     private void HandleShowResult(ShowResult result)
    16.     {
    17.         switch (result)
    18.         {
    19.             case ShowResult.Finished:
    20.                 Debug.Log("The ad was successfully shown.");
    21.                 //
    22.                 // YOUR CODE TO REWARD THE GAMER
    23.                 // Give coins etc.
    24.                 break;
    25.             case ShowResult.Skipped:
    26.                 Debug.Log("The ad was skipped before reaching the end.");
    27.                 break;
    28.             case ShowResult.Failed:
    29.                 Debug.LogError("The ad failed to be shown.");
    30.                 break;
    31.         }
    32.     }
    33. }
    How are you able to have game continuing while ad is being shown? Game should be paused in that situation...

    /Rasmus
     
    Matdrox, ToroidGames and akaBase like this.
  3. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    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
     
  4. franciscochong

    franciscochong

    Joined:
    Jul 9, 2015
    Posts:
    30
    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
     
  5. rasmus-unity

    rasmus-unity

    Moderator

    Joined:
    Aug 15, 2014
    Posts:
    1,312
    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
     
  6. franciscochong

    franciscochong

    Joined:
    Jul 9, 2015
    Posts:
    30
    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?
     
  7. rasmus-unity

    rasmus-unity

    Moderator

    Joined:
    Aug 15, 2014
    Posts:
    1,312
    The game is paused while ad is being shown. So you can play your animation in the callback method. Doesn't this work?
     
  8. franciscochong

    franciscochong

    Joined:
    Jul 9, 2015
    Posts:
    30
    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.
     
  9. rasmus-unity

    rasmus-unity

    Moderator

    Joined:
    Aug 15, 2014
    Posts:
    1,312
  10. mitshah97

    mitshah97

    Joined:
    Jun 22, 2016
    Posts:
    1
    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
     
  11. TobyYe

    TobyYe

    Joined:
    May 25, 2013
    Posts:
    24
    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.
     
  12. nitroy2k

    nitroy2k

    Joined:
    Jun 27, 2013
    Posts:
    7
    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 ?
     
  13. nitroy2k

    nitroy2k

    Joined:
    Jun 27, 2013
    Posts:
    7
    OK let me now give solution :)

    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<Button> ().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<Button> ().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<Button> ().enabled = true;

    }

    Make sure to enable exit button also on fail and error
     
  14. WILEz1975

    WILEz1975

    Joined:
    Mar 23, 2013
    Posts:
    375
    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)..
     
  15. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    92
    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.
     
  16. CWatsonT2

    CWatsonT2

    Joined:
    Jan 9, 2019
    Posts:
    114
    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?
     
  17. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    781
    Now this 2023 in Ads SDK 4.4.1 and callback has gone!!!
     
  18. Unity-Boon

    Unity-Boon

    Unity Technologies

    Joined:
    Jan 18, 2017
    Posts:
    135