Search Unity

Unity Ads testing work well on Editor but not on Device ?

Discussion in 'Unity Ads & User Acquisition' started by m-y, Jun 10, 2019.

  1. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    472
    Hello All
    i started to implement Unity Ads for my new game coming soon
    i implement everything with a script
    Testing working well on the Editor but when i try to Test on mobile Device
    it doesn't work
    so what i am missing ?
    that my script
    Code (CSharp):
    1. using UnityEngine.Advertisements;
    2.  
    3. using UnityEngine.Monetization;
    4. using UnityEngine;
    5. using System.Collections.Generic;
    6. using System.Collections;
    7.  
    8. public class TestADs : MonoBehaviour {
    9.     private string store_id = "3178972";
    10.     string  video_ad = "video";
    11.     string rewarded_videoad = "rewardedVideo";
    12.     string banner_ad = "Battlestarted";
    13.     public static  TestADs instance;
    14.     private void Awake()
    15.     {
    16.             if(instance !=null)
    17.         {
    18.             Destroy(gameObject);
    19.         }
    20.         else
    21.         {
    22.             instance = this;
    23.             DontDestroyOnLoad(gameObject);
    24.  
    25.         }
    26.     }
    27.     private void Start()
    28.     {
    29.         Monetization.Initialize(store_id, true);
    30.         StartCoroutine(showVideo());  
    31.     }
    32.     private void Update()
    33.     {
    34.      
    35.        
    36.     }
    37.     IEnumerator showVideo ()
    38.     {
    39.         yield return new WaitForSeconds(1f);
    40.  
    41.         if (Monetization.IsReady(video_ad))
    42.         {
    43.             ShowAdPlacementContent ad = null;
    44.             ad = Monetization.GetPlacementContent(video_ad) as ShowAdPlacementContent;
    45.             if (ad != null)
    46.             {
    47.                 ad.Show();
    48.             }
    49.         }
    50.     }
    51.  
    52.  
    53. }
    54.  
     
  2. Callie-Zhu

    Callie-Zhu

    Unity Technologies

    Joined:
    Mar 2, 2016
    Posts:
    42
    Are you using Services Window or Asset Package?
    What device are you testing on?
     
  3. tashfiq103

    tashfiq103

    Joined:
    Mar 4, 2016
    Posts:
    15
    Would you mind trying the following script on your side?

    # Make sure you have downloaded the unity package from "Package Manager" and not enabling the service from the "Service" windows inside from the UnityEditor.

    Here is the code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Advertisements;
    5. using UnityEngine.Events;
    6. using UnityEngine.UI;
    7.  
    8. public class UnityAdsReward : MonoBehaviour {
    9.  
    10.     #region PUBLIC VARIABLES
    11.  
    12.     public static UnityAdsReward Instance;
    13.  
    14.     [Header ("RewardVideoAds Property")]
    15.     public bool enableRewardVideoAds;
    16.     public bool rewardedVideoAdsSkipCallback;
    17.     public bool rewardedVideoAdsFailedCallBack;
    18.  
    19.     public UnityEvent OnRewardVideoAdsFinished;
    20.     public UnityEvent OnRewardVideoAdsSkipped;
    21.     public UnityEvent OnRewardVideoAdsFailed;
    22.  
    23.     [Header ("VideoAds Property")]
    24.     public bool enableVideoAds;
    25.     public bool videoAdsSkipCallback;
    26.     public bool videoAdsFailedCallback;
    27.  
    28.     public UnityEvent OnVideoAdsFinished;
    29.     public UnityEvent OnVideoAdsSkipped;
    30.     public UnityEvent OnVideoAdsFailed;
    31.  
    32.     //--------------------------------
    33.     public bool unityTestMode;
    34.     public bool testModeGUI;
    35.  
    36.     public string gameID;
    37.     public string gameID_Android;
    38.     public string gameID_iOS;
    39.  
    40.     #endregion
    41.  
    42.     #region PRIVATE VARIABLES
    43.  
    44.     private bool mCustomCall;
    45.  
    46.     private UnityAction OnAdsFinished;
    47.     private UnityAction OnAdsSkipped;
    48.     private UnityAction OnAdsFailed;
    49.  
    50.     #endregion
    51.  
    52.     void Awake () {
    53.  
    54.         mPreProcess ();
    55.     }
    56.  
    57.     #region Pre-Process/Process/Post-Process
    58.  
    59.     void OnGUI () {
    60.  
    61.         if (testModeGUI) {
    62.  
    63.             float width = 0;
    64.             float height = 0;
    65.  
    66.             if (Screen.width > Screen.height) {
    67.                 //Landscape
    68.                 width = Screen.width / 3;
    69.                 height = Screen.height / 16;
    70.  
    71.             } else {
    72.                 //Portrait
    73.                 width = Screen.width / 2;
    74.                 height = Screen.height / 12;
    75.             }
    76.  
    77.             if (Advertisement.isSupported) {
    78.  
    79.                 GUI.TextField (new Rect (0, 0, width, height),
    80.                     "Unity Ads Supported");
    81.             }
    82.  
    83.             if (GUI.Button (new Rect (0, height, width, height),
    84.                     "Show Ads")) {
    85.  
    86.                 ShowAds ();
    87.             }
    88.  
    89.             if (Advertisement.IsReady ("rewardedVideo")) {
    90.  
    91.                 GUI.TextField (new Rect (width, 0, width, height),
    92.                     "rewardVideo (Ready)");
    93.             } else {
    94.  
    95.                 GUI.TextField (new Rect (width, 0, width, height),
    96.                     "rewardVideo\n(NOT! Ready)");
    97.             }
    98.  
    99.             if (Advertisement.IsReady ("video")) {
    100.  
    101.                 GUI.TextField (new Rect (width, height, width, height),
    102.                     "video (Ready)");
    103.             } else {
    104.  
    105.                 GUI.TextField (new Rect (width, height, width, height),
    106.                     "video (NOT! Ready)");
    107.             }
    108.  
    109.             if (GUI.Button (new Rect (0, height * 2, width, height),
    110.                     "Show Ads\n(RwVAds)")) {
    111.  
    112.                 ShowRewardVideoAds ();
    113.             }
    114.  
    115.             if (GUI.Button (new Rect (width, height * 2, width, height),
    116.                     "Show Ads\n(VAds)")) {
    117.  
    118.                 ShowVideoAds ();
    119.             }
    120.  
    121.             if (GUI.Button (new Rect (0, height * 3, width * 2, height),
    122.                     "Show Ads\n(BannerAds)")) {
    123.  
    124.                 ShowBannerAd ();
    125.             }
    126.         }
    127.     }
    128.  
    129.     private void mPreProcess () {
    130.  
    131.         mCustomCall = false;;
    132.  
    133.         mResetCustomAdsCallBack ();
    134.  
    135.         if (Instance == null) {
    136.  
    137.             Instance = this;
    138.             DontDestroyOnLoad (Instance.gameObject);
    139.         } else if (Instance != this) {
    140.  
    141.             Destroy (this.gameObject);
    142.         }
    143.  
    144. #if UNITY_ANDROID
    145.  
    146.         gameID = gameID_Android;
    147.  
    148. #elif UNITY_IOS
    149.  
    150.         gameID = gameID_iOS;
    151.  
    152. #endif
    153.  
    154.        
    155.     }
    156.  
    157.     private void mResetCustomAdsCallBack () {
    158.  
    159.         OnAdsFinished = null;
    160.         OnAdsSkipped = null;
    161.         OnAdsFailed = null;
    162.     }
    163.  
    164.     #endregion
    165.  
    166.     //-----------------------------------------------------------------------------
    167.     #region Public Callback
    168.  
    169.     public void InitializedUnityAds (bool t_ForceInitilization) {
    170.  
    171.         if ( t_ForceInitilization || (Advertisement.isSupported && !IsUnityAdsInitialized())) {
    172.  
    173.             Advertisement.Initialize (gameID, unityTestMode);
    174.             Debug.Log ("UnityAdsLog | " + "Unity Ads Initialized");
    175.  
    176.  
    177.         }
    178.  
    179.     }
    180.  
    181.     public bool IsUnityAdsInitialized () {
    182.  
    183.         return Advertisement.isInitialized;
    184.     }
    185.  
    186.     #endregion
    187.  
    188.     //-----------------------------------------------------------------------------
    189.  
    190.     #region RewardedVide Ads
    191.  
    192.     public bool IsRewardedVideoAdsReady () {
    193.  
    194.         if (Advertisement.IsReady ("rewardedVideo"))
    195.             return true;
    196.         else
    197.             return false;
    198.     }
    199.  
    200.     public void ShowRewardVideoAds () {
    201.  
    202.         if (Advertisement.IsReady ("rewardedVideo")) {
    203.  
    204.             Advertisement.Show ("rewardedVideo", new ShowOptions () {
    205.  
    206.                 resultCallback = HandleRewardAdResult
    207.             });
    208.         }
    209.     }
    210.  
    211.     public void ShowRewardedVideoAdsWithEvent (UnityAction OnAdsFinished) {
    212.  
    213.         mResetCustomAdsCallBack ();
    214.  
    215.         this.OnAdsFinished = OnAdsFinished;
    216.         mCustomCall = true;
    217.  
    218.         ShowRewardVideoAds ();
    219.     }
    220.  
    221.     public void ShowRewardedVideoAdsWithEvent (UnityAction OnAdsFinished, UnityAction OnAdsFailed) {
    222.  
    223.         mResetCustomAdsCallBack ();
    224.  
    225.         this.OnAdsFinished = OnAdsFinished;
    226.         this.OnAdsFailed = OnAdsFailed;
    227.         mCustomCall = true;
    228.  
    229.         ShowRewardVideoAds ();
    230.     }
    231.  
    232.     public void ShowRewardedVideoAdsWithEvent (UnityAction OnAdsFinished, UnityAction OnAdsSkipped, UnityAction OnAdsFailed) {
    233.  
    234.         mResetCustomAdsCallBack ();
    235.  
    236.         this.OnAdsFinished = OnAdsFinished;
    237.         this.OnAdsSkipped = OnAdsSkipped;
    238.         this.OnAdsFailed = OnAdsFailed;
    239.         mCustomCall = true;
    240.  
    241.         ShowRewardVideoAds ();
    242.     }
    243.  
    244.     private void HandleRewardAdResult (ShowResult result) {
    245.  
    246.         switch (result) {
    247.  
    248.             case ShowResult.Failed:
    249.                 if (mCustomCall) {
    250.  
    251.                     if (OnAdsFailed != null)
    252.                         OnAdsFailed.Invoke ();
    253.  
    254.                     mCustomCall = false;
    255.                 } else {
    256.  
    257.                     OnRewardVideoAdsFailed.Invoke ();
    258.                 }
    259.                 //Debug.Log ("Failed To Load The RewardAds");
    260.                 break;
    261.             case ShowResult.Skipped:
    262.                 if (mCustomCall) {
    263.  
    264.                     if (OnAdsSkipped != null)
    265.                         OnAdsSkipped.Invoke ();
    266.  
    267.                     mCustomCall = false;
    268.                 } else {
    269.  
    270.                     OnRewardVideoAdsSkipped.Invoke ();
    271.                 }
    272.                 //Debug.Log ("Skip RewardAds");
    273.                 break;
    274.             case ShowResult.Finished:
    275.                 if (mCustomCall) {
    276.  
    277.                     if (OnAdsFinished != null)
    278.                         OnAdsFinished.Invoke ();
    279.  
    280.                     mCustomCall = false;
    281.                 } else {
    282.  
    283.                     OnRewardVideoAdsFinished.Invoke ();
    284.                 }
    285.                 //Debug.Log ("Finished RewardAds");
    286.                 break;
    287.         }
    288.     }
    289.  
    290.     #endregion
    291.  
    292.     //-----------------------------------------------------------------------------
    293.  
    294.     #region VideoAds
    295.  
    296.     public bool IsVideoAdsReady () {
    297.  
    298.         if (Advertisement.IsReady ("rewardedVideo"))
    299.             return true;
    300.         else
    301.             return false;
    302.     }
    303.  
    304.     public void ShowVideoAds () {
    305.  
    306.         if (Advertisement.IsReady ("video")) {
    307.  
    308.             Advertisement.Show ("video", new ShowOptions () {
    309.  
    310.                 resultCallback = HandleVideoAdResult
    311.             });
    312.         }
    313.     }
    314.  
    315.     public void ShowVideoAdsWithEvent (UnityAction OnAdsFinished) {
    316.  
    317.         mResetCustomAdsCallBack ();
    318.  
    319.         this.OnAdsFinished = OnAdsFinished;
    320.         mCustomCall = true;
    321.  
    322.         ShowVideoAds ();
    323.     }
    324.  
    325.     public void ShowVideoAdsWithEvent (UnityAction OnAdsFinished, UnityAction OnAdsFailed) {
    326.  
    327.         mResetCustomAdsCallBack ();
    328.  
    329.         this.OnAdsFinished = OnAdsFinished;
    330.         this.OnAdsFailed = OnAdsFailed;
    331.         mCustomCall = true;
    332.  
    333.         ShowVideoAds ();
    334.     }
    335.  
    336.     public void ShowVideoAdsWithEvent (UnityAction OnAdsFinished, UnityAction OnAdsSkipped, UnityAction OnAdsFailed) {
    337.  
    338.         mResetCustomAdsCallBack ();
    339.  
    340.         this.OnAdsFinished = OnAdsFinished;
    341.         this.OnAdsSkipped = OnAdsSkipped;
    342.         this.OnAdsFailed = OnAdsFailed;
    343.         mCustomCall = true;
    344.  
    345.         ShowVideoAds ();
    346.     }
    347.  
    348.     private void HandleVideoAdResult (ShowResult result) {
    349.  
    350.         switch (result) {
    351.  
    352.             case ShowResult.Failed:
    353.                 if (mCustomCall) {
    354.  
    355.                     if (OnAdsFailed != null)
    356.                         OnAdsFailed.Invoke ();
    357.  
    358.                     mCustomCall = false;
    359.                 } else {
    360.  
    361.                     OnVideoAdsFailed.Invoke ();
    362.                 }
    363.                 //Debug.Log ("Failed To Load The VideoAds");
    364.                 break;
    365.             case ShowResult.Skipped:
    366.                 if (mCustomCall) {
    367.  
    368.                     if (OnAdsSkipped != null)
    369.                         OnAdsSkipped.Invoke ();
    370.  
    371.                     mCustomCall = false;
    372.                 } else {
    373.  
    374.                     OnVideoAdsSkipped.Invoke ();
    375.                 }
    376.                 //Debug.Log ("Skip VideoAds");
    377.                 break;
    378.             case ShowResult.Finished:
    379.                 if (mCustomCall) {
    380.  
    381.                     if (OnAdsFinished != null)
    382.                         OnAdsFinished.Invoke ();
    383.  
    384.                     mCustomCall = false;
    385.                 } else {
    386.  
    387.                     OnVideoAdsFinished.Invoke ();
    388.                 }
    389.                 //Debug.Log ("Finished VideoAds");
    390.                 break;
    391.         }
    392.     }
    393.  
    394.     #endregion
    395.  
    396.     //-----------------------------------------------------------------------------
    397.  
    398.     #region BannerAd
    399.  
    400.     private bool m_IsBannerAdControllerRunning;
    401.  
    402.     public bool IsBannerAdReady () {
    403.         if (Advertisement.IsReady ("bannerad")) {
    404.             return true;
    405.         } else {
    406.             return false;
    407.         }
    408.     }
    409.  
    410.     public bool IsBannerAdLoaded(){
    411.         if(Advertisement.Banner.isLoaded){
    412.             return true;
    413.         }else{
    414.             return false;
    415.         }
    416.     }
    417.  
    418.     public void ShowBannerAd () {
    419.  
    420.         if (!m_IsBannerAdControllerRunning) {
    421.  
    422.             m_IsBannerAdControllerRunning = true;
    423.             StartCoroutine (ShowBannerAdWhenReady ());
    424.         }
    425.     }
    426.  
    427.     public void HideBannerAd () {
    428.         m_IsBannerAdControllerRunning = false;
    429.         Advertisement.Banner.Hide (true);
    430.     }
    431.  
    432.     public void DestroyBannerAd(){
    433.         m_IsBannerAdControllerRunning = false;
    434.         Advertisement.Banner.Hide(false);
    435.     }
    436.  
    437.     private IEnumerator ShowBannerAdWhenReady () {
    438.  
    439.         BannerLoadOptions t_BannerLoadOption = new BannerLoadOptions();
    440.         t_BannerLoadOption.loadCallback += OnBannerAdSucessfullyLoaded;
    441.         t_BannerLoadOption.errorCallback+= ((string errorMessage) => {
    442.             Debug.LogError( "UnityAdsLog | " + "Failed to load bannerAd : " + errorMessage);
    443.         });
    444.  
    445.         WaitForSeconds t_CycleDelay = new WaitForSeconds (5.0f);
    446.  
    447.         while (m_IsBannerAdControllerRunning) {
    448.  
    449.             if (IsBannerAdReady () && IsBannerAdLoaded()) {
    450.                 Advertisement.Banner.Show ("bannerad");
    451.                 break;
    452.             }else{
    453.                 Advertisement.Banner.Load("bannerad",t_BannerLoadOption);
    454.             }
    455.  
    456.             yield return t_CycleDelay;
    457.         }
    458.  
    459.         StopCoroutine (ShowBannerAdWhenReady ());
    460.     }
    461.  
    462.     private void OnBannerAdSucessfullyLoaded(){
    463.  
    464.         m_IsBannerAdControllerRunning = false;
    465.         Advertisement.Banner.Show ("bannerad");
    466.     }
    467.  
    468.     #endregion
    469.  
    470.     //-----------------------------------------------------------------------------
    471.  
    472.     #region ShowAds (With Priority of Rewarded Video Ads -> Video Ads)
    473.  
    474.     public bool IsAdsReady () {
    475.  
    476.         if (Advertisement.IsReady ())
    477.             return true;
    478.         else
    479.             return false;
    480.     }
    481.  
    482.     public void ShowAds () {
    483.  
    484.         if (Advertisement.IsReady ("rewardedVideo")) {
    485.  
    486.             Advertisement.Show ("rewardedVideo", new ShowOptions () {
    487.  
    488.                 resultCallback = HandleRewardAdResult
    489.             });
    490.         } else if (Advertisement.IsReady ("video")) {
    491.  
    492.             Advertisement.Show ("video", new ShowOptions () {
    493.  
    494.                 resultCallback = HandleVideoAdResult
    495.             });
    496.         }
    497.     }
    498.  
    499.     public void ShowAdsWithEvent (UnityAction OnAdsFinished) {
    500.  
    501.         mResetCustomAdsCallBack ();
    502.         this.OnAdsFinished = OnAdsFinished;
    503.         ShowAds ();
    504.     }
    505.  
    506.     public void ShowAdsWithEvent (UnityAction OnAdsFinished, UnityAction OnAdsFailed) {
    507.  
    508.         mResetCustomAdsCallBack ();
    509.         this.OnAdsFinished = OnAdsFinished;
    510.         this.OnAdsFailed = OnAdsFailed;
    511.         ShowAds ();
    512.     }
    513.  
    514.     public void ShowAdsWithEvent (UnityAction OnAdsFinished, UnityAction OnAdsSkipped, UnityAction OnAdsFailed) {
    515.  
    516.         mResetCustomAdsCallBack ();
    517.         this.OnAdsFinished = OnAdsFinished;
    518.         this.OnAdsSkipped = OnAdsSkipped;
    519.         this.OnAdsFailed = OnAdsFailed;
    520.         ShowAds ();
    521.     }
    522.  
    523.     #endregion
    524. }
     
  4. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,519
    @m-y

    The issue is likely with your showVideo coroutine. In that method, you are waiting for 1 second, then checking if the placement is ready. If it is ready, then it will be shown. If it is not ready, then nothing happens.

    One alternate approach would be to have a loop in your coroutine that checks if the placement is ready, and if not, then it waits for a second.

    I would recommend taking a look at some of the example scripts in our documentation:
    https://unityads.unity3d.com/help/unity/integration-guide-unity#basic-ads-implementation