This guide covers both methods for integrating Unity Ads in the Unity engine:
Configure your project for a supported platform using the Build Settings window.
Set the platform to iOS or Android, then click Switch Platform.
This process varies slightly depending on your integration preference; the Services Window method or the Asset Package method.
To enable Ads, you need to configure your project for Unity Services. This requires setting an Organization and Project Name. See documentation on setting up Services.
With Services set up, you can enable Unity Ads:
Before integrating Ads the Asset Package, you need to create a Unity Ads Game ID, as described below.
Create a Unity Ads Game ID
In your web browser, navigate to the Unity Ads Dashboard, using your Unity Developer Network UDN Account, and select Add new project.
Select applicable platforms (iOS, Android, or both).
Locate the platform-specific Game ID
and copy it for later.
Integrate Ads to the Asset Package
Declare the Unity Ads namespace, UnityEngine.Advertisements
in the header of your script (see UnityEngine.Advertisements documentation):
using UnityEngine.Advertisements;
Inititalize Unity Ads early in the game’s runtime lifecycle, preferably at launch, using the copied Game ID string, gameId
:
Advertisement.Initialize(string gameId)
With the Service enabled, you can implement the code in any script to display ads.
Declare the Unity Ads namespace, UnityEngine.Advertisement
in the header of your script (see UnityEngine.Advertisements documentation):
using UnityEngine.Advertisements;
Call the Show()
function to display an ad:
Advertisement.Show()
Rewarding players for watching ads increases user engagement, resulting in higher revenue. For example, games may reward players with in-game currency, consumables, additional lives, or experience-multipliers.
To reward players for completing a video ad, use the HandleShowResult
callback method in the example below. Be sure to check that the result
is equal to ShowResult.Finished
, to verify that the user hasn’t skipped the ad.
Show()
.Show()
with the "rewardedVideo"
placement to make the video unskippable.
Note: See Unity Ads documentation for more detailed information on placements
.
void ShowRewardedVideo ()
{
var options = new ShowOptions();
options.resultCallback = HandleShowResult;
Advertisement.Show("rewardedVideo", options);
}
void HandleShowResult (ShowResult result)
{
if(result == ShowResult.Finished) {
Debug.Log("Video completed - Offer a reward to the player");
// Reward your player here.
}else if(result == ShowResult.Skipped) {
Debug.LogWarning("Video was skipped - Do NOT reward the player");
}else if(result == ShowResult.Failed) {
Debug.LogError("Video failed to show");
}
}
Use the code below to create a rewarded ads button. The ads button displays an ad when pressed as long as ads are available.
Select Game Object > UI > Button to add a button to your Scene.
Select the button you added to your Scene, then add a script component to it using the Inspector. (In the Inspector, select Add Component > New Script .)
Open the script and add the following code:
Note: The two sections of code that are specific to Asset Package integration are called out in comments.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
//---------- ONLY NECESSARY FOR ASSET PACKAGE INTEGRATION: ----------//
#if UNITY_IOS
private string gameId = "1486551";
#elif UNITY_ANDROID
private string gameId = "1486550";
#endif
//-------------------------------------------------------------------//
ColorBlock newColorBlock = new ColorBlock();
public Color green = new Color(0.1F, 0.8F, 0.1F, 1.0F);
[RequireComponent(typeof(Button))]
public class UnityAdsButton : MonoBehaviour
{
Button m_Button;
public string placementId = "rewardedVideo";
void Start ()
{
m_Button = GetComponent<Button>();
if (m_Button) m_Button.onClick.AddListener(ShowAd);
if (Advertisement.isSupported) {
Advertisement.Initialize (gameId, true);
}
//---------- ONLY NECESSARY FOR ASSET PACKAGE INTEGRATION: ----------//
if (Advertisement.isSupported) {
Advertisement.Initialize (gameId, true);
}
//-------------------------------------------------------------------//
}
void Update ()
{
if (m_Button) m_Button.interactable = Advertisement.IsReady(placementId);
}
void ShowAd ()
{
var options = new ShowOptions();
options.resultCallback = HandleShowResult;
Advertisement.Show(placementId, options);
}
void HandleShowResult (ShowResult result)
{
if(result == ShowResult.Finished) {
Debug.Log("Video completed - Offer a reward to the player");
}else if(result == ShowResult.Skipped) {
Debug.LogWarning("Video was skipped - Do NOT reward the player");
}else if(result == ShowResult.Failed) {
Debug.LogError("Video failed to show");
}
}
}
Press Play in the Unity Editor to test the Ads button integration.
For further guidance, see the Unity Ads forum.
Use settings to modify placements and other game-specific settings in your project. (See Unity Ads documentation for further information on placements.)
In your web browser, navigate to the Unity Ads Dashboard, using your Unity Developer Network UDN Account, and locate the project for your game.
Select an applicable platform (iOS or Android).
Select placement. (See Unity Ads documentation.)
Did you find this page useful? Please give it a rating: