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
UnityEngine.Advertisements
in the header of your script (see UnityEngine.Advertisements documentation): using UnityEngine.Advertisements;
gameId
: Advertisement.Initialize(string gameId)
With the Service enabled, you can implement the code in any script to display ads.
UnityEngine.Advertisement
in the header of your script (see UnityEngine.Advertisements documentation): using UnityEngine.Advertisements;
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.
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");
}
}
}
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.)