Search Unity

Rewarded video shows on Start()

Discussion in 'Unity Ads & User Acquisition' started by TortoRacoon, Jul 19, 2019.

Thread Status:
Not open for further replies.
  1. TortoRacoon

    TortoRacoon

    Joined:
    Apr 17, 2019
    Posts:
    61
    So as soon as I run the test mode, it's showing the Ad, and when I close it it throws the reward
    upload_2019-7-19_17-27-33.png

    The desired outcome is to make it show the Ad after pressing a button that will pop up when reaching level 10. Press the button, show the ad, watch it all the way through, then close it, recieve the reward and don't see that button again at least until 8 minutes have passed...


    here is my code wrote in accordance to the implementation guide

    Code (CSharp):
    1. private void Start()
    2.     {
    3.         dummyOfferWindow.SetActive(false);
    4.  
    5.         canDummyShowWin = false;
    6.  
    7.  
    8.         prizeSpawnLocation = prizeSpawner.transform.position;
    9.  
    10.         //This two lines correspond to Unity Ads, not only rewarded video but normal video according to the implementation guide
    11.         Advertisement.AddListener(this);
    12.         Advertisement.Initialize(gameId, testMode);
    13.  
    14.     }
    15.  
    16.     private void Update()
    17.     {
    18.  
    19.         if(this.gameObject.GetComponent<Wavecontroller>().currentWave >= 10 &&
    20.             timeBetweenActivateButton <= 0)
    21.         {
    22.             bool waveIsOn = this.gameObject.GetComponent<Wavecontroller>().waveIsOn;
    23.  
    24.             if(!waveIsOn &&
    25.                 Advertisement.IsReady("rewardedVideo"))
    26.             {
    27.                 dummyOfferButton.SetActive(true);
    28.             }
    29.             else
    30.             {
    31.                 dummyOfferButton.SetActive(false);
    32.             }
    33.            
    34.         }
    35.         else
    36.         {
    37.             dummyOfferButton.SetActive(false);
    38.             timeBetweenActivateButton -= Time.deltaTime;
    39.         }
    40.        
    41.     }
    42.  
    43.     public void Reward()
    44.     {
    45.         canDummyShowWin = false;
    46.         Time.timeScale = 1f;
    47.         Instantiate(spawnVisualEffect, prizeSpawnLocation, Quaternion.identity);
    48.         Instantiate(dummyPrefab, prizeSpawnLocation, Quaternion.identity);
    49.  
    50.  
    51.         timeBetweenActivateButton = startTimeBwnActivateBtn;
    52.  
    53.     }
    54.  
    55.  
    56.     public void DummyButPressed()
    57.     {
    58.         if(canDummyShowWin)
    59.         {
    60.             canDummyShowWin = false;
    61.             Time.timeScale = 1f;
    62.         }
    63.         else if(!canDummyShowWin)
    64.         {
    65.             canDummyShowWin = true;
    66.             Time.timeScale = 0f;
    67.         }
    68.     }
    69.  
    70.  
    71.  
    72.     //All bellow is for rewarded ads
    73.  
    74.  
    75.     // Implement IUnityAdsListener interface methods:
    76.     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    77.     {
    78.         // Define conditional logic for each ad completion status:
    79.         if (showResult == ShowResult.Finished)
    80.         {
    81.             // Reward the user for watching the ad to completion.
    82.             Reward();
    83.         }
    84.         else if (showResult == ShowResult.Skipped)
    85.         {
    86.             // Do not reward the user for skipping the ad.
    87.             checker.GetComponent<GameChecker>().Patience();
    88.  
    89.         }
    90.         else if (showResult == ShowResult.Failed)
    91.         {
    92.             checker.GetComponent<GameChecker>().AdWentWrong();
    93.             Debug.LogWarning("The ad did not finish due to an error");
    94.         }
    95.     }
    96.  
    97.     public void OnUnityAdsReady(string placementId)
    98.     {
    99.         // If the ready Placement is rewarded, show the ad:
    100.         if (placementId == "rewardedVideo")
    101.         {
    102.             Advertisement.Show("rewardedVideo");
    103.         }
    104.     }
    105.  
    106.     public void OnUnityAdsDidError(string message)
    107.     {
    108.         // Log the error.
    109.         Debug.Log(message);
    110.     }
    111.  
    112.     public void OnUnityAdsDidStart(string placementId)
    113.     {
    114.         // Optional actions to take when the end-users triggers an ad.
    115.     }
    116.  
    117. }
    118.  
    Thank you for your time
     
  2. TortoRacoon

    TortoRacoon

    Joined:
    Apr 17, 2019
    Posts:
    61
    So I changed some lines in Start and replaced the code as follows:

    Code (CSharp):
    1. public Button playDummyRewarded;
    2.     Button dummyButtonComponent;
    3.  
    4.     public GameObject checker;
    5.  
    6.  
    7.     private float timeBetweenActivateButton;
    8.     private float startTimeBwnActivateBtn = 480;
    9.  
    10.  
    11.     private void Start()
    12.     {
    13.         dummyOfferWindow.SetActive(false);
    14.  
    15.         canDummyShowWin = false;
    16.  
    17.  
    18.         prizeSpawnLocation = prizeSpawner.transform.position;
    19.  
    20.         //This two lines correspond to Unity Ads, not only rewarded video but normal video according to the implementation guide
    21.         dummyButtonComponent = playDummyRewarded.GetComponent<Button>();
    22.  
    23.         // Set interactivity to be dependent on the Placement’s status:
    24.         dummyButtonComponent.interactable = Advertisement.IsReady("rewardedVideo");
    25.  
    26.         // Map the ShowRewardedVideo function to the button’s click listener:
    27.         if (dummyButtonComponent) dummyButtonComponent.onClick.AddListener(ShowRewardedVideo);
    28.  
    29.         // Initialize the Ads listener and service:
    30.         Advertisement.AddListener(this);
    31.         Advertisement.Initialize(gameId, true);
    32.  
    33.     }
    34.  
    35.     private void Update()
    36.     {
    37.  
    38.  
    39.         if(canDummyShowWin)
    40.         {
    41.             dummyOfferWindow.SetActive(true);
    42.            
    43.  
    44.         }
    45.         else
    46.         {
    47.             dummyOfferWindow.SetActive(false);
    48.            
    49.         }
    50.  
    51.  
    52.  
    53.         if(this.gameObject.GetComponent<Wavecontroller>().currentWave >= 10 &&
    54.             timeBetweenActivateButton <= 0)
    55.         {
    56.             bool waveIsOn = this.gameObject.GetComponent<Wavecontroller>().waveIsOn;
    57.  
    58.             if(!waveIsOn &&
    59.                 Advertisement.IsReady("rewardedVideo"))
    60.             {
    61.                 dummyOfferButton.SetActive(true);
    62.             }
    63.             else
    64.             {
    65.                 dummyOfferButton.SetActive(false);
    66.             }
    67.            
    68.         }
    69.         else
    70.         {
    71.             dummyOfferButton.SetActive(false);
    72.             timeBetweenActivateButton -= Time.deltaTime;
    73.         }
    74.        
    75.     }
    76.  
    77.     public void Reward()
    78.     {
    79.         Debug.Log("This is the reward line being runned by the listener");
    80.         canDummyShowWin = false;
    81.         Time.timeScale = 1f;
    82.         Instantiate(spawnVisualEffect, prizeSpawnLocation, Quaternion.identity);
    83.         Instantiate(dummyPrefab, prizeSpawnLocation, Quaternion.identity);
    84.  
    85.  
    86.         timeBetweenActivateButton = startTimeBwnActivateBtn;
    87.  
    88.     }
    89.  
    90.  
    91.     public void DummyButPressed()
    92.     {
    93.         if(canDummyShowWin)
    94.         {
    95.             canDummyShowWin = false;
    96.             Time.timeScale = 1f;
    97.         }
    98.         else if(!canDummyShowWin)
    99.         {
    100.             canDummyShowWin = true;
    101.             Time.timeScale = 0f;
    102.         }
    103.     }
    104.  
    105.  
    106.     public void PlayThatAdMister()
    107.     {
    108.         if(Advertisement.IsReady("video"))
    109.         {
    110.             Advertisement.Show("video");
    111.             Debug.Log("This thing should show an AD");
    112.         }
    113.         else
    114.         {
    115.             Debug.Log("Advertisement is ready :" + Advertisement.IsReady("video"));
    116.         }
    117.     }
    118.  
    119.     public void DummyDeclineButton()
    120.     {
    121.         canDummyShowWin = false;
    122.         Time.timeScale = 1f;
    123.     }
    124.  
    125.  
    126.     //All bellow is for rewarded ads
    127.  
    128.     // Implement a function for showing a rewarded video ad:
    129.     void ShowRewardedVideo()
    130.     {
    131.         Advertisement.Show("rewardedVideo");
    132.     }
    133.  
    134.     // Implement IUnityAdsListener interface methods:
    135.     public void OnUnityAdsReady(string placementId)
    136.     {
    137.         // If the ready Placement is rewarded, activate the button:
    138.         if (placementId == "rewardedVideo")
    139.         {
    140.             dummyButtonComponent.interactable = true;
    141.         }
    142.     }
    143.  
    144.     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    145.     {
    146.         // Define conditional logic for each ad completion status:
    147.         if (showResult == ShowResult.Finished)
    148.         {
    149.             // Reward the user for watching the ad to completion.
    150.             Reward();
    151.             Debug.Log("this is the result returned by the listener");
    152.         }
    153.         else if (showResult == ShowResult.Skipped)
    154.         {
    155.             // Do not reward the user for skipping the ad.
    156.             checker.GetComponent<GameChecker>().Patience();
    157.  
    158.         }
    159.         else if (showResult == ShowResult.Failed)
    160.         {
    161.             checker.GetComponent<GameChecker>().AdWentWrong();
    162.             Debug.LogWarning("The ad did not finish due to an error");
    163.         }
    164.     }
    165.  
    166.     public void OnUnityAdsDidError(string message)
    167.     {
    168.         // Log the error.
    169.         Debug.Log(message);
    170.     }
    171.  
    172.     public void OnUnityAdsDidStart(string placementId)
    173.     {
    174.         // Optional actions to take when the end-users triggers an ad.
    175.     }
    176.  
    It works as intended BUT the problem now is that its running the reward twice though it is only referenced once (I tripple checked all the code).

    upload_2019-7-19_19-14-56.png

    the way this thing work is a complete mystery to me
     
  3. TortoRacoon

    TortoRacoon

    Joined:
    Apr 17, 2019
    Posts:
    61
    It-s worse than I thought, every time you play the rewarded video it doubles the number of times it ran the Reward function last time... si if first was two, the next will be 4, and the next to that will be 8....

    I literally copy-pasted the implementation guide example...
    upload_2019-7-19_19-25-49.png
     
  4. TortoRacoon

    TortoRacoon

    Joined:
    Apr 17, 2019
    Posts:
    61
    I figured it out!! Just in case someone out there gets the same result and is trying to set it up like the implementetion guide, notice that the Listener takes information from every kind of advertisement, from their side (maybe) they runing the ad in two functions (one for "video" and another for "rewardedVideo", so you will recieve those two results from the listener.

    You just have to specify that you are only usint the result from your rewarded video as follows

    Code (CSharp):
    1.     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    2.     {
    3.         // Define conditional logic for each ad completion status:
    4.         if(placementId == "rewardedVideo")
    5.         {
    6.             if (showResult == ShowResult.Finished)
    7.             {
    8.                 // Reward the user for watching the ad to completion.
    9.                 Reward();
    10.                 Debug.Log("this is the result returned by the listener");
    11.             }
    12.             else if (showResult == ShowResult.Skipped)
    13.             {
    14.                 // Do not reward the user for skipping the ad.
    15.                 checker.GetComponent<GameChecker>().Patience();
    16.  
    17.             }
    18.             else if (showResult == ShowResult.Failed)
    19.             {
    20.                 checker.GetComponent<GameChecker>().AdWentWrong();
    21.                 Debug.LogWarning("The ad did not finish due to an error");
    22.             }
    23.         }
    24.         else if(placementId == "video")
    25.         {
    26.             Debug.Log("A video played but it was not rewarded");
    27.         }
    28.        
    29.     }
    and like this it will ignore the "video" id-s. If you have many kinds of rewards, you can specify with conditions inside the function inside your reward.

    Good luck people
     
    MadGraph likes this.
Thread Status:
Not open for further replies.