This Swift project provides two examples for integrating the Unity Ads SDK into a SpriteKit or Cocos2D game
- Simple Example: One scene, minimumal integration to play a video ad
- Complete Example: Two scenes that can play ads, incentivized callbacks, rewarded placements
Planet illustration provided by NASA - Original image by ESA/Hubble (M. Kornmesser) - https://www.spacetelescope.org/
Create a Game Project on the Unity Ads Dashboard
- Log into the dashboard using your UDN Account
- Create a new game project
- Look for your iOS Game ID in the project, a 7-digit number that you will use in your integration
Download the Unity SDK from https://github.com/Applifier/unity-ads-sdk
- Download the SDK zip file
- Unzip the project, and locate UnityAds.framework and UnityAds.bundle
Import UnityAds.framework and UnityAds.bundle into your project
- Drag and drop the files into your project's file manager
- Select the box next to "Copy items if needed"
Make sure the following dependancies are enabled in your project
AdSupport.framework
AVFoundation.framework
CFNetwork.framework
CoreFoundation.framework
CoreMedia.framework
CoreTelephony.framework
StoreKit.framework
SystemConfiguration.framework
- Click your project settings
- select Build Phases > Link Binary With Library
- Click the + button > select the Framework > Click Add
Add a bridging header for UnityAds.framework
- Create a new file in your project called UnityAds-Bridging-Header.h
- In the file, add the following line:
#import <UnityAds/UnityAds.h>
Add UnityAds to your AppDelegate
- Open AppDelegate.swift
- Create a shared instance of Unity ads by adding the following code to your AppDelegate class
class AppDelegate: UIResponder, UIApplicationDelegate {
static let unityAds = UnityAds() //Create a shared instance of Unity Ads
Initialize Unity Ads in your root ViewController
- Open your root ViewController
- In viewDidLoad(), add the following code to initialize the SDK
override func viewDidLoad() {
super.viewDidLoad()
UnityAds.sharedInstance().delegate = self
UnityAds.sharedInstance().setTestMode(true) //enable client-side test mode
UnityAds.sharedInstance().startWithGameId("1003843", andViewController: self)
NOTE: The game ID in the example project is 1003843, you need to replace this number with your own game ID
Add the @required callback to your root ViewController
func unityAdsVideoCompleted(rewardItemKey: String!, skipped: Bool {
if (!skipped) {
//Provide ingame reward, give coins, report to analytics, etc...
}
}
Note: rewardItemKey was deprecated; Use custom zones in the dashboard to track reward types
In the root View Controller, the following function will play a video ad
func playAd(placement: String) {
if (UnityAds.sharedInstance().canShow(placement)) {
UnityAds.sharedInstance().show(placement)
}
}
To call an ad from another ViewController (including a SpriteKit or Cocos2D scene)
let vc = self.view!.window!.rootViewController as! YourRootViewController
vc.playAd("video")
For more information, check out the iOS Integration Guide, the support Forum, or contact unityads-sales@unity3d.com