This guide provides step-by-step instructions to integrate Unity Ads 2.0 into an Android Studio or iOS project.
- Create a Game Project in the Unity Ads Dashboard
- Download Unity Ads 2.0 for iOS or Android.
# To Get Started
Create a Game Project in the Unity Ads Dashboard
Log into the Unity ads dashboard using your Unity3D account.
Create a new game project, and enable test mode
- Click new game, and name the project
- Select "Google Play Store", check the box "This game has not been published yet"
- Select "Not targeted for children under 13", unless your game specifically targets children.
- Click continue
Please note that test mode can be toggled under (add screenshot)
At this point you should see a unique (7-digit) game ID that can be used to initialize Unity Ads in your project.
Download the Unity Ads 2.0 for Android.
#Android Integration
Example project is available here
### 1. Import Unity Ads From the downloaded Unity Ads 2.0 folder, locate **unity-ads.aar** and import using Android Studio's AAR import tool.Or...
- Copy unity-ads.aar to your project's /app/libs folder. (Usually ~/AndroidStudioProjects/YOUR_PROJECT/app/libs)
- In build.gradle, under
dependencies
, addcompile(name:'unity-ads', ext:'aar')
.
dependencies {
// ...
compile(name:'unity-ads', ext:'aar')
}
In MainActivity.java (or any activity that will show ads), add the Unity Ads headers.
import com.unity3d.ads.IUnityAdsListener;
import com.unity3d.ads.UnityAds;
Then, create a IUnityAdsListener class that will listen for callbacks.
public class MainActivity extends AppCompatActivity {
// ...
private class UnityAdsListener implements IUnityAdsListener {
}
}
Mouse over the class declaration to reveal a red lightbulb, then click "Implement methods" to automatically add the required callbacks:
private class UnityAdsListener implements IUnityAdsListener {
@Override
public void onUnityAdsReady(String s) {
//Called when Unity Ads has a video available to show
}
@Override
public void onUnityAdsStart(String s) {
//Called when a video begins playing
}
@Override
public void onUnityAdsFinish(String s, UnityAds.FinishState finishState) {
//Called when a video vinishes playing
}
@Override
public void onUnityAdsError(UnityAds.UnityAdsError unityAdsError, String s) {
//Called when the Unity Ads detects an error
}
}
First, declare a UnityAdsListener in your activity.
public class MainActivity extends AppCompatActivity {
final private UnityAdsListener unityAdsListener = new UnityAdsListener();
// ...
}
Then, initialize Unity Ads within the same scope as your UnityAdsListener declaration, using the game ID from your dashboard.
Note: "YOUR_GAME_ID" is a 7-digit number from your game project in the Unity Ads dashboard. (For example, "1091553")
protected void onCreate(Bundle savedInstanceState) {
UnityAds.initialize(this, "YOUR_GAME_ID", unityAdsListener);
}
public void buttonOnClick(View v) {
if(UnityAds.isReady("rewardedVideo")){ //Make sure a video is available & the placement is valid.
UnityAds.show(this, "rewardedVideo");
}
}
### 5. Reward the Playernote: By default, leaving the placement option blank will show an ad with the default "video" placement. (5-second skip) Find more information on placements in our docs.
In the UnityAdsListener class, there's a method, onUnityAdsFinish
, that is called when a video finishes.
Use onUnityAdsFinish
to reward the player if they watched the entire ad:
private class UnityAdsListener implements IUnityAdsListener {
//...
@Override
public void onUnityAdsFinish(String s, UnityAds.FinishState finishState) {
if (finishState != UnityAds.FinishState.SKIPPED) {
//video was not skipped, reward the player!
rewardPlayer();
}
//...
}
Example project is available here. ADD A LINK HERE
#iOS integration (Swift)
### 1. Import Unity Ads From the downloaded Unity Ads 2.0 folder, locate **UnityAds.framework**.
Drag-and-drop UnityAds.framework into your XCode project (and copy it).
### 2. Add Delegate & Callbacks In **ViewController.m** (or any ViewController that will show ads), import the UnityAds namespace.import UnityAds
Make the ViewController a UnityAdsDelegate and add the four @required
callbacks.
import UnityAds
class ViewController: UIViewController, UnityAdsDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
func unityAdsReady(placementId: String) {
//Called when Unity Ads is ready to show an ad
}
func unityAdsDidStart(placementId: String) {
//Called when Uniy Ads begins playing a video
}
func unityAdsDidFinish(placementId: String, withFinishState state: UnityAdsFinishState) {
//Called when a video completes
}
func unityAdsDidError(error: UnityAdsError, withMessage message: String) {
}
}
Note: "YOUR_GAME_ID" is a 7-digit number from the Unity Ads dashboard. (For example, "1091553")
override func viewDidLoad() {
super.viewDidLoad()
UnityAds.initialize("YOUR_GAME_ID", delegate: self)
}
@IBAction func AdButtonPressed() {
if(UnityAds.isReady("rewardedVideo")){ //check that a video is ready & the placement is valid
UnityAds.show(self, placementId: "rewardedVideo")
}
}
### 5. Reward the Player The callback `unityAdsDidFinish(...)` is called when a video finishes.note: By default, leaving the placement option blank will show an ad with the default "video" placement. (5-second skip) Find more information on placements in our docs.
Use unityAdsDidFinish(...)
to reward the player if they watched the entire ad.
func unityAdsDidFinish(placementId: String, withFinishState state: UnityAdsFinishState) {
if(state != .Skipped){
//video was not skipped, reward the player!
rewardUserForWatchingAnAd()
}
}
Example project is available here