Search Unity

Oops, my game breaks when the ads can't play.

Discussion in 'Unity Ads & User Acquisition' started by cephalo2, Apr 21, 2019.

  1. cephalo2

    cephalo2

    Joined:
    Feb 25, 2016
    Posts:
    263
    I was on a camping trip this weekend and I realized my game breaks when the device is offline and can't play the ads. Is there a method I can call to check if a device is offline?

    How should I handle an offline situation, do you let a free game go on without the ads? It seems if you let the game continue, then people could just turn off their data and bypass the ads. Is that OK? How is this normally handled?
     
  2. unity_akeem

    unity_akeem

    Unity Technologies

    Joined:
    Apr 25, 2019
    Posts:
    20
    Hi there. You can use the Scripting Api to check on network connectivity. Here is a link that includes a code example.

    You can handle offline situations in a number of ways. If you scroll to the Implementing basic (non-rewarded) ads section of this link, you will find a function named ShowAdWhenReady. You could implement this method, and include a timeout so that the method will time out eventually, which would happen if a device doesn't have network connectivity. I included this excerpt below as well, and added a comment to indicate a good place to implement the timeout.

    Code (CSharp):
    1. using UnityEngine.Monetization;
    2.  
    3. public class UnityAdsPlacement : MonoBehaviour {
    4.  
    5.     public string placementId = "video";
    6.  
    7.     public void ShowAd () {
    8.         StartCoroutine (ShowAdWhenReady ());
    9.     }
    10.  
    11.     private IEnumerator ShowAdWhenReady () {
    12.         while (!Monetization.IsReady (placementId)) {
    13.             yield return new WaitForSeconds(0.25f);
    14.             // ADD TIMEOUT HERE
    15.         }
    16.  
    17.         ShowAdPlacementContent ad = null;
    18.         ad = Monetization.GetPlacementContent (placementId) as ShowAdPlacementContent;
    19.  
    20.         if(ad != null) {
    21.             ad.Show ();
    22.         }
    23.     }
    24. }