Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question In Rewarded Video Ads not work reward

Discussion in 'Unity Ads & User Acquisition' started by ab2700kd, Jan 28, 2022.

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

    ab2700kd

    Joined:
    May 12, 2016
    Posts:
    22
    In Rewarded Video Ads not work reward

    image002.jpg

    Place a GameObject on Scene (UnityAdsManager)

    image004.jpg

    Place a Button on Scene (TestButton with btnTestAdsManager.cs )

    It used:
    https://docs.unity.com/ads/UnityDeveloperIntegrations.htm

    To initialize Unity Ads SDK
    https://docs.unity.com/ads/InitializingTheUnitySDK.htm

    To show reward video
    https://docs.unity.com/ads/ImplementingRewardedAdsUnity.htm

    Code (CSharp):
    1. AdsInitializer.cs
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.Advertisements;
    7.  
    8. public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener
    9. {
    10.     [SerializeField] private string _androidGameId = "39XXXXX";
    11.     [SerializeField] private string _iOSGameId = "39XXXXX";
    12.     private string _gameId;
    13.  
    14.     void Awake()
    15.     {
    16.         InitializeAds();
    17.     }
    18.  
    19.     public void InitializeAds()
    20.     {
    21.         _gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
    22.             ? _iOSGameId
    23.             : _androidGameId;
    24.  
    25.         Advertisement.Initialize(_gameId, false);
    26.      
    27.     }
    28.  
    29.     public void OnInitializationComplete()
    30.     {
    31.         Debug.Log("Unity Ads initialization complete.");
    32.     }
    33.  
    34.     public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    35.     {
    36.         Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
    37.     }
    38. }
    39.  
    Code (CSharp):
    1. RewardedAds.cs
    2.  
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Advertisements;
    6.  
    7. public class RewardedAds : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
    8. {
    9.      public string _adUnitId = null;
    10.     public string AndroidAdID = "39XXXXX";
    11.     public string iOSAdID = "39XXXXX";
    12.        // Get the Ad Unit ID for the current platform:
    13.  
    14.     void Awake()
    15.     {
    16.  
    17. #if UNITY_IOS
    18.          _adUnitId = iOSAdID;
    19. #endif
    20.  
    21. #if UNITY_ANDROID
    22.         _adUnitId = AndroidAdID;
    23. #endif
    24.  
    25.     }
    26.  
    27.  
    28.  
    29.     // Load content to the Ad Unit:
    30.     public void LoadAd()
    31.     {
    32.         // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
    33.         Debug.Log("Loading Ad: " + _adUnitId);
    34.           Advertisement.Load(_adUnitId, this);
    35.     }
    36.  
    37.     // If the ad successfully loads, add a listener to the button and enable it:
    38.     public void OnUnityAdsAdLoaded(string adUnitId)
    39.     {
    40.         Debug.Log("Ad Loaded: " + adUnitId);
    41.  
    42.         if (adUnitId.Equals(_adUnitId))
    43.         {
    44.          
    45.         }
    46.     }
    47.  
    48.     // Implement a method to execute when the user clicks the button.
    49.     public void ShowAd()
    50.     {
    51.         // Then show the ad:
    52.           Advertisement.Show(_adUnitId, this);
    53.      }
    54.  
    55.     // Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
    56.     public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
    57.     {
    58.                 if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
    59.         {
    60.             Debug.Log("Unity Ads Rewarded Ad Completed");
    61.             // Grant a reward.
    62.             int icoin = PlayerPrefs.GetInt("coin");
    63.             icoin = icoin + 1;
    64.             PlayerPrefs.SetInt("coin", icoin);
    65.  
    66.             // Load another ad:
    67.             Advertisement.Load(_adUnitId, this);
    68.          }
    69.     }
    70.  
    71.     // Implement Load and Show Listener error callbacks:
    72.     public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
    73.     {
    74.         Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
    75.         // Use the error details to determine whether to try to load another ad.
    76.     }
    77.  
    78.     public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
    79.     {
    80.         Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
    81.         // Use the error details to determine whether to try to load another ad.
    82.     }
    83.  
    84.     public void OnUnityAdsShowStart(string adUnitId) { }
    85.     public void OnUnityAdsShowClick(string adUnitId) { }
    86.  
    87.     void OnDestroy()
    88.     {
    89.         // Clean up the button listeners:
    90.     }
    91. }
    92.  

    Code (CSharp):
    1. btnTestAdsManager.cs
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7. using UnityEngine.Advertisements;
    8.  
    9. public class btnTestAdsManager : MonoBehaviour
    10. {
    11.     Button btnTestAds;
    12. #if UNITY_IOS
    13.              string adUnitId = "39XXXXX";
    14. #endif
    15.  
    16. #if UNITY_ANDROID
    17.     //   _adUnitId = _androidAdUnitId;
    18.     string adUnitId = "39XXXXX";
    19. #endif
    20.  
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         btnTestAds = GameObject.Find("btnTestAds1").gameObject.GetComponent<Button>();
    26.  
    27.         btnTestAds.onClick.AddListener(() => {
    28.             if ((Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
    29.                || (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork))
    30.             {
    31.                 //
    32.                   RewardedAds ms = new RewardedAds();          
    33.                   ms.ShowAd();
    34.                   ms.OnUnityAdsShowComplete(adUnitId, UnityAdsShowCompletionState.COMPLETED);            
    35.             }
    36.         });
    37.     }
    38. }
    39.  
    OR

    Code (CSharp):
    1.  
    2. btnTestAds.onClick.AddListener(() => {
    3.             if ((Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
    4.                || (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork))
    5.             {
    6.                 //
    7.                   RewardedAds ms = new RewardedAds();          
    8.                   ms.ShowAd();              
    9.             }
    10.         });
    11.  
    When I press Button (TestAds) , Ads is shown, but reward not work (reward for showing ads not received)

    If I use:

    Code (CSharp):
    1.  
    2. btnTestAds.onClick.AddListener(() => {
    3.             if ((Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
    4.                || (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork))
    5.             {
    6.                 //
    7.        GameObject.Find("UnityAdsManager").GetComponent<RewardedAds>().ShowAd();
    8.             }
    9.         });
    10.  
    That receive error in log
    Error showing Ad Unit 39XXXXX: NOT_READY - Placement 39XXXXX is not ready
     
  2. LIU_Hanyang96

    LIU_Hanyang96

    Unity Technologies

    Joined:
    Jul 12, 2021
    Posts:
    113
    Hi,

    I think you are using the wrong ad unit ids. Ad unit ids are different from game id. It should be the id shown in the "Ad Units".

    upload_2022-1-28_16-54-57.png
     
  3. ab2700kd

    ab2700kd

    Joined:
    May 12, 2016
    Posts:
    22
    Game Id from here
    1.png

    Hm.... very Thanks.. It's worked

    Code (CSharp):
    1. void Awake()
    2.     {
    3. #if UNITY_IOS  
    4.      _adUnitId = "rewardedVideo";
    5. #endif
    6.  
    7. #if UNITY_ANDROID      
    8.         _adUnitId = "rewardedVideo";
    9. #endif
    10.     }
     
    Last edited: Jan 29, 2022
  4. ab2700kd

    ab2700kd

    Joined:
    May 12, 2016
    Posts:
    22
    Then I have another question in addition

    Code (CSharp):
    1. btnTestAds.onClick.AddListener(() => {
    2.             if ((Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
    3.                || (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork))
    4.             {GameObject.Find("UnityAdsManager").GetComponent<RewardedAds>().ShowAd();
    5.             }
    6.         });
    when I click the Button (btnTestAds) One time, Ads shown , reward is received - It's OK
    But when I click the Button ( btnTestAds) second time, Ads shown, but I get double reward....
    when I click the Button ( btnTestAds) third time, Ads shown, but I get triple reward.....
    I think "btnTestAds.onClick.AddListener" - doubles, triples, etc....
    How to do "btnTestAds.onClick.AddListener" to no doubles, triples, etc ?
     
  5. Unity_Quinn

    Unity_Quinn

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    102
    Hi @ab2700kd,

    This is a known issue that occurs only within the Unity Editor. We are currently working on a fix.
    https://forum.unity.com/threads/callbacks-are-called-more-and-more-often.1209253/

    As a potential workaround to make testing in the editor easier, you may want to consider keeping track of whether or not an ad has been shown (and a listener added) and adding the listener as appropriate. See the below code snippet as an example.

    Code (CSharp):
    1. #if UNITY_EDITOR
    2.         if (adsShownThisSession == 0)
    3.         {
    4.             Advertisement.Show(_adUnitId, this);
    5.             adsShownThisSession++;
    6.         }
    7.         else
    8.         {
    9.             Advertisement.Show(_adUnitId);
    10.         }
    11. #else
    12.             Advertisement.Show(_adUnitId, this);
    13. #endif
    Thank you for your patience while we work on fixing this issue!
     
  6. ab2700kd

    ab2700kd

    Joined:
    May 12, 2016
    Posts:
    22
    Thanks, On real device Test Unity Ads work perfectly!
     
Thread Status:
Not open for further replies.