Skip to content
This repository has been archived by the owner on Jul 28, 2020. It is now read-only.

Applifier/Unity-Ads-SDK-2.0-Integration-Examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Integration Walkthrough for iOS and Android

This guide provides step-by-step instructions to integrate Unity Ads 2.0 into an Android Studio or iOS project.

###To Get Started

  1. Create a Game Project in the Unity Ads Dashboard
  2. Download Unity Ads 2.0 for iOS or Android.

###Android Integration

  1. Import Unity Ads
  2. Add listener & callbacks
  3. Initialize Unity Ads
  4. Show an Ad
  5. Add rewarded integration

###iOS Integration

  1. Import Unity Ads
  2. Add delegate & callbacks
  3. Initialize Unity Ads
  4. Show an Ad
  5. Add rewarded integration

# 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

  1. Click new game, and name the project
  2. Select "Google Play Store", check the box "This game has not been published yet"
  3. Select "Not targeted for children under 13", unless your game specifically targets children.
  4. 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...

  1. Copy unity-ads.aar to your project's /app/libs folder. (Usually ~/AndroidStudioProjects/YOUR_PROJECT/app/libs)
  2. In build.gradle, under dependencies, add compile(name:'unity-ads', ext:'aar').
dependencies {
    // ...
    compile(name:'unity-ads', ext:'aar')
}
### 2. Add Listener & Callbacks

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:

Alt text

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
  }
}
### 3. Initialize Unity Ads

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);
}
### 4. Show an Ad In my example, I use a button to show an ad.
public void buttonOnClick(View v) {
  if(UnityAds.isReady("rewardedVideo")){ //Make sure a video is available & the placement is valid.
    UnityAds.show(this, "rewardedVideo");
  }
}

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.

### 5. Reward the Player

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) {
  }
}
### 3. Initialize Unity Ads Initialize Unity Ads by calling `UnityAds.initialize("YOUR_GAME_ID", delegate: self)`

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)
  }
### 4. Show an Ad In the example project, a button is used to show an ad.
@IBAction func AdButtonPressed() {
  if(UnityAds.isReady("rewardedVideo")){ //check that a video is ready & the placement is valid
    UnityAds.show(self, placementId: "rewardedVideo")
  }
}

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.

### 5. Reward the Player The callback `unityAdsDidFinish(...)` is called when a video finishes.

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


About

Android and iOS integration examples for SDK 2.0

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published