Skip to content

Instantly share code, notes, and snippets.

@wcoastsands
Last active May 8, 2021 20:37
Show Gist options
  • Save wcoastsands/22d2481dcbc6400dc5e4 to your computer and use it in GitHub Desktop.
Save wcoastsands/22d2481dcbc6400dc5e4 to your computer and use it in GitHub Desktop.
Getting Started with Unity Ads in Unity 5.2 or Later

Getting Started with Unity Ads in Unity 5.2 or Later

Step 1: Set the Platform to either iOS or Android.

  1. Select File > Build Settings... from the Unity menu.
  2. Select iOS or Android from the Platform list.
  3. Select Switch Platform.

Step 2: Enable Unity Ads through the Services window.

  1. Select Window > Services to open the Services window.
  2. Select the Ads service to configure settings.
  3. Select the toggle switch to enable Ads (upper-right of Ads in the Servies window).
  4. Indicate whether your game is designed specifically for children under the age of 13, or not.
  5. Select Save Changes to finish the setup.

When you indicate whether your game is designed for children under the age of 13, ads will not be behaviorally targeted to users in your game. Behavioral targeting can yield higher eCPMs by showing ads that are more relevant to each user, but cannot be done for users under the age of 13 due to COPPA regulations.

Step 3: Select GameObject > UI > Button to create a Unity UI button.

Step 4: Add the UnityAdsButton script to the Unity UI button.

Step 5: Run the game and select the Unity UI button when it becomes interactable.


With Ads enabled through the Services window, Unity Ads will automatically be initialized when the game starts. You can view the game IDs used to initialize Unity Ads under the Advanced section of Ads in the Services window.

Test Mode for Unity Ads is enabled by default through the Services window. While this option is enabled, only test ads will be shown in game. Test ads are not limited to the standard limit of 25 ads per day, and will not affect stats or accidently generate revenue when clicked on, which is ideal for testing.

When you get ready to publish your game, Test Ads should be disabled. You can either disable it from the Services window, or you can disable it from the Unity Ads dashboard for your project under under the Settings tab of either platform.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
#if UNITY_ADS
using UnityEngine.Advertisements;
#endif
public class UnityAdsButton : MonoBehaviour
{
public string zoneId;
#if UNITY_ADS
private Button _button;
void Start ()
{
_button = GetComponent<Button>();
if (_button) _button.onClick.AddListener(ShowAdPlacement);
}
void Update ()
{
if (_button)
{
if (string.IsNullOrEmpty(zoneId)) zoneId = null;
_button.interactable = Advertisement.IsReady(zoneId);
}
}
public void ShowAdPlacement ()
{
if (string.IsNullOrEmpty(zoneId)) zoneId = null;
if (!Advertisement.IsReady(zoneId))
{
Debug.LogWarningFormat("Unable to show ad placement. Zone is not ready ({0}).", zoneId);
return;
}
ShowOptions options = new ShowOptions();
options.resultCallback = HandleShowResult;
Advertisement.Show(zoneId, options);
}
void HandleShowResult (ShowResult result)
{
switch (result)
{
case ShowResult.Finished:
Debug.Log("Video completed. Offer a reward to the player.");
break;
case ShowResult.Skipped:
Debug.LogWarning("Video was skipped.");
break;
case ShowResult.Failed:
Debug.LogError("Video failed to show.");
break;
}
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment