Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

How do OnUnityAdsDidFinish could differentiate between ads IDs?

Discussion in 'Unity Ads' started by kalhach, Sep 16, 2020.

  1. kalhach

    kalhach

    Joined:
    Aug 26, 2020
    Posts:
    3
    Hi, good day, im new in OBL (Object-Based Language) and im having problems with the Ad code
    this is the Ad manager code
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Advertisements;
    4. using UnityEngine.UI;
    5. public class AdManager : MonoBehaviour, IUnityAdsListener
    6. {
    7. #if UNITY_ANDROID
    8.     private static readonly string StoreId = "3776575";
    9. #elif UNITY_IOS
    10.     private static readonly string StoreID = "3776574";
    11. #endif
    12.     private static readonly string VideoID = "video";
    13.     private static readonly string RewardedvideoID = "rewardedVideo";
    14.     public GameObject RewardButton;
    15.     GameObject corona;
    16.     bool showAd = true;
    17.  
    18.  
    19.     void Awake()
    20.     {
    21.             corona = GameObject.FindGameObjectWithTag("Player");
    22.             Advertisement.AddListener(this);
    23.             Advertisement.Initialize(StoreId, true);
    24.  
    25.     }
    26.  
    27.     public void Update()
    28.     {
    29.         if (showAd == true)
    30.         {
    31.             var savebox = SaveBox.Instance;
    32.         if (savebox.Box.var2 == 1 && corona.GetComponent<coronaship>().killedUI == true)
    33.         {
    34.             StartCoroutine(ShowStandardAd());
    35.         }
    36.             showAd = false;
    37.         }
    38.     }
    39.     public void ShowRewarded()
    40.     {
    41.         StartCoroutine(ShowRewardedAd());
    42.     }
    43.     public void ShowStandard()
    44.     {
    45.  
    46.             StartCoroutine(ShowStandardAd());
    47.  
    48.     }
    49.  
    50.     IEnumerator ShowStandardAd()
    51.     {
    52.         while (!Advertisement.IsReady(VideoID))
    53.         {
    54.             yield return null;
    55.         }
    56.         Advertisement.Show(VideoID);
    57.     }
    58.  
    59.     IEnumerator ShowRewardedAd()
    60.     {
    61.         while (!Advertisement.IsReady(RewardedvideoID))
    62.         {
    63.             yield return null;
    64.         }
    65.         Advertisement.Show(RewardedvideoID);
    66.     }
    67.  
    68.     public void OnUnityAdsReady(string placementId)
    69.     {
    70.  
    71.     }
    72.     public void OnUnityAdsDidStart(string placementId)
    73.     {
    74.      
    75.     }
    76.     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    77.     {
    78.        if(showResult == ShowResult.Finished)
    79.         {
    80.                 coronaship.maxHealth = 8;
    81.                 RewardButton.SetActive(false);
    82.            
    83.         }
    84.        else if(showResult == ShowResult.Failed)
    85.        {
    86.             Debug.LogError("didn't work Ad");
    87.        }
    88.     }
    89.  
    90.     public void OnUnityAdsDidError(string message)
    91.     {
    92.    
    93.     }
    94.  
    95.    
    96. }
    97.  
    and this part of the pause code where i store some of the variables needed in the admanager
    Code (CSharp):
    1.     public void Reset()
    2.     {
    3.         var savebox = SaveBox.Instance;
    4.         SceneManager.LoadScene("SampleScene");
    5.         ScoreText.ScoreValue = 0;
    6.  
    7.         if (savebox.Box.var2 == 0)
    8.         {
    9.             savebox.Box.var2 = 1;
    10.             savebox.save();
    11.  
    12.         }
    13.         else
    14.         {
    15.             savebox.Box.var2 = 0;
    16.         }
    17.  
    18.     }
    19.     public void sreward()
    20.     {
    21.         var savebox = SaveBox.Instance;
    22.         if(savebox.Box.var1 == 0)
    23.         {
    24.             savebox.Box.var1 = 1;
    25.             savebox.save();
    26.            
    27.         }
    28.         else
    29.         {
    30.             savebox.Box.var1 = 0;
    31.         }
    32.  
    33.     }
    and here is the game over canvas where the ads appear...
    upload_2020-9-15_21-56-2.png
    now the thing is when the player lose an add appears (the skippable one but when i skip it) and when a player click the reward button another add appears (the non skippable one) but my problem is when the normal add apears the reward button disapear and i think that the problem reside in this section of the first code that i showed here
    Code (CSharp):
    1. public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    2.     {
    3.        if(showResult == ShowResult.Finished)
    4.         {
    5.                 coronaship.maxHealth = 8;
    6.                 RewardButton.SetActive(false);
    7.          
    8.         }
    because what i understand is that this part of the code check when an add finished to show an add, but not which one... any ideas of what should, could or can do here?
     

    Attached Files:

  2. AdamColes4472

    AdamColes4472

    Unity Technologies

    Joined:
    Jul 20, 2020
    Posts:
    21
    Hi, when OnUnityAdsDidFinish is called it will pass through a placementId string. This string will be the placementId, as specified in your dashboard, of the placement that has finished. You can check that string to see if it's the same as your rewarded placements Id before you call reward code.

    I hope this helped.
     
  3. kalhach

    kalhach

    Joined:
    Aug 26, 2020
    Posts:
    3
    mmmh ok ok i'll check that and see what it does thank you so much if i have a problem then i will update this tread
     
  4. kalhach

    kalhach

    Joined:
    Aug 26, 2020
    Posts:
    3
    it works, thanks a lot for the help :))
     
unityunity