-
Notifications
You must be signed in to change notification settings - Fork 7
UnityAdsHelper Showing Ads

With the Unity UI system all setup, let's write a script that can be used to show an ad with the UI Button's OnClick UnityEvent, and make the UI Button interactable only when ads are ready.
Step 1: Create a new script called ButtonExample and define a ShowAd method.
Defining the zoneId as a public variable allows us to easily update it from the Inspector. If a zone ID is not specified, the default zone will be used instead.
Note: A full list of available zone IDs can be found in the Unity Ads Admin under the Monetization Settings tab of your game profile.
C# Example – ButtonExample.cs
using UnityEngine;
using System.Collections;
public class ButtonExample : MonoBehaviour
{
public string zoneId;
public void ShowAd ()
{
UnityAdsHelper.ShowAd(zoneId);
}
}JavaScript Example – ButtonExample.js
#pragma strict
public class ButtonExample extends MonoBehaviour
{
public var zoneId : String;
public function ShowAd () : void
{
UnityAdsHelper.ShowAd(zoneId);
}
}Step 2: Add the ButtonExample script to the UI Button in your scene.
- Locate and select the ShowAdButton GameObject.
- Select Component > Scripts > Button Example from the Unity Editor menu.
Step 3: Configure the UI Button's OnClick UnityEvent.
- Locate and select the ShowAdButton GameObject.
- Locate the OnClick section of the Button component.
- Press the + button under the OnClick section to add a reference.
- Select and drag ShowAdButton from the Hierarchy to the target GameObject field.
- Select ButtonExample > ShowAd () method from the dropdown menu.

Note: At this point you can run the game to test out the button. But keep in mind, we're not yet checking to see if ads are ready. We'll do this in the following steps.
Step 4: Update the ButtonExample script to handle the interactable state of the UI Button.
C# Example – ButtonExample.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ButtonExample : MonoBehaviour
{
public Text textReady;
public Text textWaiting;
public string zoneId;
private Button _button;
void Start ()
{
_button = GetComponent<Button>();
}
void Update ()
{
if (_button)
{
_button.interactable = UnityAdsHelper.IsReady(zoneId);
if (textReady) textReady.enabled = _button.interactable;
if (textWaiting) textWaiting.enabled = !_button.interactable;
}
}
public void ShowAd ()
{
UnityAdsHelper.ShowAd(zoneId);
}
}JavaScript Example – ButtonExample.js
#pragma strict
import UnityEngine.UI;
public class ButtonExample extends MonoBehaviour
{
public var textReady : Text;
public var textWaiting : Text;
public var zoneId : String;
private var _button : Button;
function Start () : void
{
_button = GetComponent.<Button>();
}
function Update () : void
{
if (_button)
{
_button.interactable = UnityAdsHelper.IsReady(zoneId);
if (textReady) textReady.enabled = _button.interactable;
if (textWaiting) textWaiting.enabled = !_button.interactable;
}
}
public function ShowAd () : void
{
UnityAdsHelper.ShowAd(zoneId);
}
}Step 5: Add UI Text components to the Text Ready and Text Waiting fields.
- Locate and select the ShowAdButton GameObject.
- Expand ShowAdButton in the Hierarchy to view child objects.
- Select and drag the ReadyText GameObject to the Text Ready field.
- Select and drag the WaitingText GameObject to the Text Waiting field.

Now let's press the Play button in the Unity Editor toolbar to run the scene.
Once Unity Ads is initialized, and ad is ready to be shown, the ShowAdButton will become interactable. Pressing the ShowAdButton to show an ad will show a blue placeholder image while in the Unity Editor.
Video ads are only shown when running on iOS or Android devices.
Note: When running on device, the Unity player is paused while an ad is shown, then un-paused when the ad is hidden. However, in the Unity Editor, the Unity player is not paused.
To get the same effect in the Unity Editor, you would need to pause the AudioListener and set
Time.timeScale = 0while the placeholder is shown. Then restore theTime.timeScalevalue and un-pause the AudioListener when the placeholder is hidden.You can use
UnityAdsHelper.isShowingto determine when to pause and un-pause your game while in the Unity Editor.#if UNITY_EDITOR AudioListener.pause = UnityAdsHelper.isShowing; Time.timeScale = UnityAdsHelper.isShowing ? 0f : 1f; #endif