Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Have to click a few times to get an ad loaded

Discussion in 'Unity Ads & User Acquisition' started by matthewdjw, Jan 2, 2015.

  1. matthewdjw

    matthewdjw

    Joined:
    Jun 17, 2014
    Posts:
    6
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.Advertisements;
    5. public class AdStuff : MonoBehaviour {
    6.  
    7.     void Awake() {
    8.  
    9.         if (Advertisement.isSupported) {
    10.             Advertisement.allowPrecache = true;
    11.             Advertisement.Initialize ("22117",true);
    12.         } else {
    13.             Debug.Log("Platform not supported");
    14.         }
    15.     }
    16.     public void ButtonClick(){
    17.         PlayVideo ();
    18.     }
    19.  
    20.         void PlayVideo(){
    21.                     Advertisement.Show (@"defaultVideoAndPictureZone", new ShowOptions {
    22.                     pause = false,
    23.                     // Handle the various result callback states.
    24.                     resultCallback = result => {
    25.                         switch (result)
    26.                         {
    27.                         case ShowResult.Finished:
    28.                         PlayerPrefs.SetInt ("Points", PlayerPrefs.GetInt ("Points") + 125);
    29.                          
    30.                             break;
    31.                         case ShowResult.Skipped:
    32.                             ///Your code if it skipped
    33.                             break;
    34.                         case ShowResult.Failed:
    35.                             ///Your code if it fails
    36.                             break;
    37.                         }
    38.                     }
    39.                 });
    40.                 }
    41.          
    42. }
    43.  


    The following is my code. How come I have to click again to get the ad to load. For example the ad takes like 3 seconds to initialize in the editor for some reason, not quite sure why seeing as it's on test mode. But if I clicked it earlier nothing happens, it doesn't load after its ready you have to click the button again. I need it so when ButtonClick() is called it will load that ad, not so I have to click it a few times. Is this really obvious? What am I missing?
     
    Last edited: Jan 2, 2015
  2. dvirus1023

    dvirus1023

    Joined:
    Mar 4, 2013
    Posts:
    51
    One thing that you are missing is the IsReady check.

    Code (csharp):
    1.  
    2. if( Advertisement.isReady()){            
    3. // Show with default zone, pause engine and print result to debug log            
    4. Advertisement.Show(null, new ShowOptions {pause = true, resultCallback = result => {
    5. Debug.Log(result.ToString()); }});        
    6. }
    7.  
    Also, make sure to use code blocks to make code easier to read.
     
  3. matthewdjw

    matthewdjw

    Joined:
    Jun 17, 2014
    Posts:
    6
    Thank-you but that doesn't change that it isn't loading at first and you must click it a few times. Would the best solution be to enable a boolean with the button to check if the ad has loaded through the update function then launch the ad once it is ready? I assumed there would be a better way but I guess not, thanks a ton for the help!
     
  4. dvirus1023

    dvirus1023

    Joined:
    Mar 4, 2013
    Posts:
    51