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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Delay alert when waiting for Advertisement.isReady()

Discussion in 'Unity Ads & User Acquisition' started by ajhknight, Mar 19, 2016.

  1. ajhknight

    ajhknight

    Joined:
    Feb 12, 2016
    Posts:
    1
    Hi, this is my first question - so sorry if I am duplicating anything here.

    I am working to show a page where the user opts in to view an ad. I am using the code below to either play the ad when the next page loads or - if there is no internet connection - show an alert page with a friendly message when the page loads. I also initialize Unity Ads on my Menu page.

    This code (below) is working - but I'm concerned that the alert might kick in before .isReady() is true if connection is slow. Please could you tell me if I need to delay the alert, and if so, what's the best code to use. Thanks

    <code>

    using UnityEngine;
    using System.Collections;
    using UnityEngine.Advertisements; // Declare the Unity Ads namespace.
    using UnityEngine.SceneManagement;


    public class RCGShowAd : MonoBehaviour
    {
    // Define gameId as a public field
    // so it can be set from the Inspector.
    public string gameId;

    void Start ()
    {

    if (Advertisement.isSupported) { // If the platform is supported,
    Advertisement.Initialize(gameId); // initialize Unity Ads.
    TryAd();
    }
    }

    public void TryAd()
    {

    if(Advertisement.IsReady())
    {

    Advertisement.Show();
    }


    else
    {
    SceneManager.LoadScene("CS_Alert");
    }

    }

    }
     
  2. solomonli

    solomonli

    Unity Technologies

    Joined:
    Mar 4, 2016
    Posts:
    10
    Hi ajhknight,

    It takes time for UnityAds' initialisation. As you are asking if it is ready immediatly after calling Advertisement.Initialize(), more chances for Advertisement.IsReady() is false, so scene "CS_Alert" should kick in in most cases. Typically you can use a coroutine to check Advertisement.IsReady() every certain time, and Advertisement.Show() is the ad is ready. Then after the player completely watching the video ad, you can load your scene. There is an integration guide for your reference, it's pretty simple and very straight forward.

    Hope it's helpful.