integration guide ios - unity-cn/unityads-help-cn GitHub Wiki
/*
Title: Integration Guide for iOS
Description: A guide to integrating the Unity Ads SDK for iOS
Sort: 11
*/
注意: 这篇是针对Unity Ads 1.5.x集成文档, Unity Ads SDK 2.0 已经推出, 1.5.x将只会在未来有限的时间内获得支持. Unity Ads 2.0 的文档请参考这里
Unity Ads iOS 集成包括下边2个方面的操作:
- 集成 Unity Ads SDK 到你的 iOS 游戏
- 在 Unity Ads Publisher Dashboard 上配置游戏
注意: 可以从Github上下载 Unity Ads SDK.
集成Unity Ads非常简单:
- 从SDK根目录找到
UnityAds.framework
andUnityAds.bundle
导入 (drag and drop) 到iOS项目里. - 使用Game ID初始化Unity Ads
- 检查是否有可用视频
- 向玩家展示Unity Ads
- 等待玩家看完视频, 给玩家游戏内奖励
项目里需要加入以下依赖
AdSupport.framework
, AVFoundation.framework
, CFNetwork.framework
, CoreFoundation.framework
CoreMedia.framework
, CoreTelephony.framework
, StoreKit.framework
, SystemConfiguration.framework
- 选择 Project Settings > Build Phases
- 展开 "Link Binary with Libraries" section
- 点击加号 "+" 依次加入上面列出来的framework
下边实现了一段基本的代码层面集成示例, 如果遇到任何问题, 可以发送邮件给 [email protected].
第一步要在 app delegate 加入 UnityAds . 首先引入 Unity Ads 头文件:
#import <UnityAds/UnityAds.h>
然后在 -application:didFinishLaunchingWithOptions: -method
加入初始化啊代码, 在这端代码中需要传入GameID和一个ViewController.
// Initialize Unity Ads
[[UnityAds sharedInstance] startWithGameId:@"YOUR_GAME_ID_HERE" andViewController:myViewController]; //replace game ID with your own
注意: 把YOUR_GAME_ID_HERE替换成你的GameID. 你需要的GameID可以在 Unity Ads Publisher Dashboard 上找到, 如果没有的话可能需要新建一个.
如果有需要的话, 也可以把初始化和设置ViewController分开.
在想使用 Unity Ads 之前, 一定要设好ViewController:
Example:
[[UnityAds sharedInstance] startWithGameId:1003843];
// Other initialization code
// ...
// Add the view controller
[[UnityAds sharedInstance] setViewController:myViewController];
[[UnityAds sharedInstance] setDelegate:self];
作为Unity Ads delegate, 至少要实现下边这个方法:
- (void)unityAdsVideoCompleted:(NSString *)rewardItemKey skipped:(BOOL)skipped
如果要使用奖励式的视频来给玩家游戏内奖励, skipped
应该是 false
.
完成了初始化和delegate设置之后, 就可以开始使用Unity Ads啦.
在显示广告之前, 我们需要使用canShow
方法检查是否有广告可用. 然后就可以使用show
方法来播放广告啦.
广告位可以在 Unity Ads Publisher Dashboard 上进行设置.
想要设置一个广告位, 需要在调用show
之前调用setZone
.
zoneId
是 Unity Ads Publisher Dashboard 默认的或自己创建的Integration ID.
// Set the zone before checking readiness or attempting to show.
[[UnityAds sharedInstance] setZone:@"rewardedVideo"];
// Use the canShow method to check for zone readiness,
// then use the canShowAds method to check for ad readiness.
if ([[UnityAds sharedInstance] canShow])
{
// If both are ready, show the ad.
[[UnityAds sharedInstance] show];
}
广告播放完毕后, 就会从ViewController上移除. 如果想手工移除的话, 可以调 hide
方法:
[[UnityAds sharedInstance] hide]
如果你不需要给玩家奖励的话, 可以跳过这一节.
玩家观看完视频后, Unity Ads SDK 会调用 -unityAdsVideoCompleted:rewardItemKey:skipped:
delegate 方法. 这个时候可以根据广告位奖励玩家.
unityAdsVideoCompleted:rewardItemKey:skipped:
有一个boolean参数来指明视频广告是否被跳过. 不应该 获得奖励, 但到底要怎么做还是取决于开发者.
注意: 在被跳过或关掉的情况下,
-unityAdsVideoCompleted:rewardItemKey:skipped:
方法 并不保证被调用, 这种情况下, 请使用下边这个方法-unityAdsWillHide:
.
您应该开启测试模式(test mode)来测试 Unity Ads 的集成. 在初始化(startWithGameId
)之前发送消息到 setTestMode:(BOOL)testModeEnabled
// TEST MODE: Do not use in production apps
[[UnityAds sharedInstance] setTestMode:YES];
[[UnityAds sharedInstance] setDebugMode:YES];
[[UnityAds sharedInstance] startWithGameId:@"YOUR_GAME_ID_HERE" andWithViewController:myViewController];
从 Unity Ads Publisher Dashboard 来配置相应的游戏.
您可以在 Unity Ads Publisher Dashboard 上链接已经发布的游戏. 这样做需要一个 iTunes application ID 或者 iTunes URL. 就像这样:
或者