First, set the build targets and enable Unity Ads in the Services Panel.
- Open your game project, or create a new Unity project.
- Select
Edit > Build Settings
and set the platform to iOS
or Android
- Enable Ads in the Unity Services window.
Once that's done, select Window > Services
. Select an Organization from the drop down menu: Click Create
.
Click Ads
, and enable the SDK in your project:
Add the code
First, declare the Unity Ads namespace in the header of your script
using UnityEngine.Advertisements;
Then, you can display an ad by calling the following method
Advertisement.Show();
Example Code
Add a button to your scene that plays an ad, then handles status and callbacks.
Step 1: Select Game Object > UI > Button
to add a Button in your scene
Step 2: Add the following script to the button:
using UnityEngine;
using UnityEngine.Advertisements;
public class UnityAdsExample : MonoBehaviour
{
public void ShowRewardedAd()
{
if (Advertisement.IsReady("rewardedVideo"))
{
var options = new ShowOptions { resultCallback = HandleShowResult };
Advertisement.Show("rewardedVideo", options);
}
}
private void HandleShowResult(ShowResult result)
{
switch (result)
{
case ShowResult.Finished:
Debug.Log("The ad was successfully shown.");
//
// YOUR CODE TO REWARD THE GAMER
// Give coins etc.
break;
case ShowResult.Skipped:
Debug.Log("The ad was skipped before reaching the end.");
break;
case ShowResult.Failed:
Debug.LogError("The ad failed to be shown.");
break;
}
}
}
Then simply press the editor Play button to test the Unity Ads Button integration.