Search Unity

Ad not ready and data connection !

Discussion in 'Unity Ads & User Acquisition' started by Dragonic89, Jan 24, 2016.

  1. Dragonic89

    Dragonic89

    Joined:
    Jan 3, 2013
    Posts:
    48
    Hello,

    I plan to upload an Android application soon, and i'm using Unity Ads for the first time with Unity 5.3.1(f1).

    Integration was really easy, but I have some issues when it comes to test on my Android device (Zenfone 2, Android 5.0). With "Test mode" enabled no problem, the Unity Ads is showing if my data connection is active.

    Without "Test mode", I have two kinds of problems for now :

    - I got the ad if my datas connection was active. But if it wasn't on the first request, even if I activate my data connection after that, Advertisement.IsReady(id) always return false. I had to close and restart the game to get an ad ! This is I think the most critical problem.

    - (Edit : this second point is now working after 6~7h, don't know why) : created two more video placement on the admin panel ("videoMute" and "rewardedVideoMute"). I don't know if it's because of that but Advertisement.IsReady(id) always return false for now, with or without data connection activated (and even with a placement that was working yesterday) !

    I don't think I made a mistake in the code :

    Code (CSharp):
    1. public void ShowAd()
    2.     {
    3.         //audioAdsActivated : boolean controlled by button switch
    4.         if (audioAdsActivated && Advertisement.IsReady("rewardedVideo"))
    5.         {
    6.             var options = new ShowOptions { resultCallback = HandleShowResult };
    7.             Advertisement.Show("rewardedVideo", options);
    8.         }
    9.         else if (!audioAdsActivated && Advertisement.IsReady("rewardedVideoMute"))
    10.         {
    11.             var options = new ShowOptions { resultCallback = HandleShowResult };
    12.             Advertisement.Show("rewardedVideoMute", options);
    13.         }
    14.         else
    15.         {
    16.             debugTextAds.color = Color.red;
    17.             debugTextAds.text = "The ad isn't ready.\n Check your datas connection !";
    18.         }
    19.     }
    20.  
    21.     private void HandleShowResult(ShowResult result)
    22.     {
    23.         switch (result)
    24.         {
    25.             case ShowResult.Finished:
    26.                 debugTextAds.color = new Color32(20,150,20,255);
    27.                 debugTextAds.text = "The ad was successfully shown.";
    28.                 rewardUser();
    29.                 break;
    30.             case ShowResult.Skipped:
    31.                 debugTextAds.color = Color.red;
    32.                 debugTextAds.text = "The ad was skipped before reaching the end.";
    33.                 break;
    34.             case ShowResult.Failed:
    35.                 debugTextAds.color = Color.red;
    36.                 debugTextAds.text = "The ad failed to be shown.";
    37.                 break;
    38.         }
    39.     }
    Placement ID checked ("rewardedVideo", "rewardedVideoMute"), Android GameID checked on both side (Unity and admin panel). Working with "Test mode".
     
    Last edited: Jun 16, 2016
  2. mikaisomaa

    mikaisomaa

    Unity Technologies

    Joined:
    Sep 14, 2015
    Posts:
    365
    Answered in a private conversation to inquire further details. Will post answer here later.
     
  3. mikaisomaa

    mikaisomaa

    Unity Technologies

    Joined:
    Sep 14, 2015
    Posts:
    365
    Workaround for recovering from flight mode:

    Before initializing Unity Ads, check for Internet connectivity in e.g. a coroutine and manually Initialize Unity Ads after verifying an Internet connection exists.

    For the Services window -based integrations: To disable the initialization of Unity Ads on startup, create a new C# script called UnityAdsBuildProcessor, and place it within the Editor folder in your project. Then copy the following code into the script:

    Code (CSharp):
    1.  
    2. using UnityEditor;
    3. using UnityEditor.Callbacks;
    4. using UnityEditor.Advertisements;
    5.  
    6. public class UnityAdsBuildProcessor : Editor
    7. {
    8.     [PostProcessScene]
    9.     public static void OnPostprocessScene ()
    10.     {
    11.         AdvertisementSettings.enabled = true;
    12.         AdvertisementSettings.initializeOnStartup = false;
    13.     }
    14. }
    15.  
    More information can be found at the Unity Ads knowledge base.
     
    Shahdee and lassade like this.
  4. Dragonic89

    Dragonic89

    Joined:
    Jan 3, 2013
    Posts:
    48
    Thank you for your help in private messages mikaisomaa !

    So yes the problem is that UnityAdsService failed to initialize properly at the start of the application if there is no internet connection.

    My workaround isn't really diffcult to implement:
    - first I have to disable the initialization of Unity Ads on startup (check previous post from mikaisomaa for this part)
    - I launched at start a coroutine which check for "google.com" every two seconds while it's not connected, and I manually start the initialization of UnityAds with "Advertisement.Initialize(gameId);" when a connection is detected. Here is the code :

    Code (CSharp):
    1. private string androidGameID = "##SECRET##";
    2. private string iosGameID = "##SECRET##";
    3. private bool unityAdsInitialized = false;//can be used for informing the user about the status
    4.  
    5. public IEnumerator checkInternetConnection()
    6.     {
    7.         float timeCheck = 2.0f;//will check google.com every two seconds
    8.         float t1;
    9.         float t2;
    10.         while(!unityAdsInitialized)
    11.         {
    12.             WWW www = new WWW("http://google.com");
    13.             t1 = Time.fixedTime;
    14.             yield return www;
    15.             if (www.error == null)
    16.             {
    17.                 #if UNITY_ANDROID
    18.                     Advertisement.Initialize(androidGameID); // initialize Unity Ads.
    19.                 #elif UNITY_IOS
    20.                     Advertisement.Initialize(iosGameID); // initialize Unity Ads.
    21.                 #endif
    22.  
    23.                 unityAdsInitialized = true;
    24.  
    25.                 break;//will end the coroutine
    26.             }
    27.             t2 = Time.fixedTime - t1;
    28.             if(t2 < timeCheck)
    29.                 yield return new WaitForSeconds(timeCheck - t2);
    30.         }
    31.     }
    Ads are now working with an internet connection even if the internet connection isn't active at the start of the application !

    Note : you can't only try to check for internet connection and initialize UnityAds when you want to do your "Advertisement.IsReady()", because it will take some time to request the ad, and it will not be ready.
     
    Last edited: Apr 20, 2016
    lassade and unity-nikkolai like this.
  5. jayanthg

    jayanthg

    Joined:
    May 26, 2013
    Posts:
    48
    Thanks! This is what I need.

    But actually, the game IDs need not be a secret since if I use your game ID, you would be getting all the money :)
     
  6. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    261
    Hi, this either doesnt work for me, or i must be misunderstanding something

    1) Ive put UnityAdsBuildProcessor.cs with the above code inside Assets/Editor
    2) Ive created empty GameObject, added it to scene, attached simple script that checks Advertisement.isInitialized on Start() - it traces TRUE.
    Also console traces: UnityAdsEditor: Initialize({ProjectId}, True);

    Unity 5.3.4f1

    Am i missing something?

    EDIT: answer here: http://forum.unity3d.com/threads/cannot-disable-unityads-auto-initialization.402451/
     
    Last edited: Jun 16, 2016
  7. shaq145

    shaq145

    Joined:
    Jan 23, 2017
    Posts:
    8
    Dragonic89 where did you launch your coroutine to check for google.com? did you make a new script for it?
    I made a new script and I tried your code, but when I tested it on my mobile device Advertisement.Initialize(gameID); is not returning to true or not Initialize even if I'm already connected to an internet.

    Thank you in advance for your help.
    btw, I'm using Unity 5.4.2f2.
     
  8. Dragonic8926

    Dragonic8926

    Joined:
    Mar 10, 2014
    Posts:
    34
    That was one year ago. I don't know if this script can still be used like that. Since this time UnityAds has evolved !

    I launched this coroutine from my main script in my game. But this could be done in any script as long as the script with the coroutine is attached to an active Gameobject I think.
     
  9. shaq145

    shaq145

    Joined:
    Jan 23, 2017
    Posts:
    8
    I manage to Initialize the Unity Ads but the problem now is that, the Advertisement.isReady() was not going to true.
    Here's the script I'm using. I don't think made some mistake.

    Thanks in advance.
    Code (CSharp):
    1. void Update () {
    2.  
    3.                 if (coolDownEffect) {
    4.  
    5.                         Invoke ("ResetCooldown", 20);
    6.                 }
    7.  
    8.                 if (thePlayer.deathCheck) { // to check if the player's character is dead
    9.  
    10.                         ShowAd ();
    11.                 }
    12.     }
    13.  
    14.         public void ShowAd(){
    15.                    
    16.                 if (buttonCooldown == false) {
    17.                        
    18.                         if (Advertisement.IsReady ()) {
    19.  
    20.                                 theButton.SetActive (true);  //button to watch the ads
    21.                         }
    22.                 }
    23.         }
    24.  
    25.  
    26.         public void WatchAds(){
    27.  
    28.                 if (buttonCooldown == false) {
    29.                        
    30.                         Advertisement.Show ("rewardedVideo", new ShowOptions (){ resultCallback = HandleResult });
    31.                         theButton.SetActive (false);
    32.  
    33.                         coolDownEffect = true;
    34.                         buttonCooldown = true;
    35.                 }
    36.         }
    37.            
    38.  
    39.         void ResetCooldown(){
    40.  
    41.                 buttonCooldown = false;
    42.         }
    43.  
    44.  
    45.  
    46.         private void HandleResult(ShowResult result){
    47.  
    48.                 switch (result)
    49.                 {
    50.                 case ShowResult.Finished:
    51.                         Debug.Log ("Player gains +30 coins");
    52.                         break;
    53.                 case ShowResult.Skipped:
    54.                         Debug.Log ("Player Skipped the Ads");
    55.                         break;
    56.                 case ShowResult.Failed:
    57.                         Debug.Log ("Player failed to launch the ad ?Internet?");
    58.                         break;
    59.                 }
    60.         }