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

How to show Ad every so often?

Discussion in 'Unity Ads & User Acquisition' started by chesterhilly, Mar 11, 2016.

  1. chesterhilly

    chesterhilly

    Joined:
    May 4, 2015
    Posts:
    6
    Ok so I have managed to integrate ads into my project but I don't have alot of experience with c#.

    So I just wanted to know if anyone would know how to change a script that creates an button (to show an ad) so the ad would appear every amount of time set. e.g 5 minutes.

    Here is the script example:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Advertisements;
    4.  
    5. public class ShowAds : MonoBehaviour
    6. {
    7.     void OnGUI ()
    8.     {
    9.         Rect buttonRect = new Rect (10, 10, 150, 50);
    10.         string buttonText = Advertisement.IsReady () ? "Show Ad" : "Waiting...";
    11.  
    12.         if (GUI.Button (buttonRect, buttonText)) {
    13.             Advertisement.Show ();
    14.         }
    15.     }
    16. }
     
  2. rasmus-unity

    rasmus-unity

    Unity Technologies

    Joined:
    Aug 15, 2014
    Posts:
    1,312
    Not sure your users will necessarily watch an ad, just because a button is shown in your game ;) See e.g. http://blogs.unity3d.com/2015/04/15/a-designers-guide-to-using-video-ads/ for suggestions on how to integrate ads into a game.

    Anyways, using a variable to keep track of when an ad was last shown, could be used to achieve what you ask for. Example code (have not tested, even tried to compile it though):

    Code (CSharp):
    1.     using UnityEngine;
    2.     using System.Collections;
    3.     using UnityEngine.Advertisements;
    4.    
    5.     public class ShowAds : MonoBehaviour
    6.     {
    7.         private float timeLastAdWasShown;
    8.  
    9.         void OnGUI ()
    10.         {
    11.             if (Time.time - timeLastAdWasShown < 5 * 60)  // in seconds
    12.                 return;
    13.  
    14.             Rect buttonRect = new Rect (10, 10, 150, 50);
    15.             string buttonText = Advertisement.IsReady () ? "Show Ad" : "Waiting...";
    16.    
    17.             if (GUI.Button (buttonRect, buttonText)) {
    18.                 timeLastAdWasShown = Time.time;
    19.                 Advertisement.Show ();
    20.             }
    21.         }
    22.     }
     
  3. solomonli

    solomonli

    Unity Technologies

    Joined:
    Mar 4, 2016
    Posts:
    10
    Last edited: Mar 16, 2016