如何在Unity中使用iOS端原生代码请求Admob广告。
先来说一下,这个需求的原因是,因为Admob unity的官方文档更新实在太慢了,我们用unity的admob库确实很简单就能请求实现广告了,但是版本目前还停留在6.6.0,而iOS的最新文档已经是6.9.0版本了,所以我们考虑调用ios端的请求展示代码请求广告,以便于更好的展示广告,分析广告数据等。
首先Unity调用iOS代码
///
/// ios原生代码请求AdmobReward
///
[DllImport("__Internal")]
public static extern void LoadAdmobFromIOS(string adid);
///
/// ios原生代码请求AdmobRewardInter
///
[DllImport("__Internal")]
public static extern void LoadAdmobInterFromIOS(string adid);
///
/// ios原生代码展示Admob
///
[DllImport("__Internal")]
public static extern void ShowAdmobFromIOS(string adid);
///
/// ios原生代码展示Admob Reward Inter
///
[DllImport("__Internal")]
public static extern void ShowAdmobInterFromIOS(string adid);
这里我定义的方法分别是:请求Admob激励视频,请求Admob插屏激励视频,展示激励视频,展示插屏激励视频。
在 xcode写入oc++代码
//Admob在IOS中请求广告逻辑
extern "C" {
UnityAppController* uac = [[UnityAppController alloc] init];
void LoadAdmobFromIOS(const char* value) {
NSString *adid = [NSString stringWithUTF8String:value];
[uac loadRewardedAd: adid];
}
void LoadAdmobInterFromIOS(const char* value) {
NSString *adid = [NSString stringWithUTF8String:value];
[uac loadRewardedInter: adid];
}
void ShowAdmobFromIOS(const char* value) {
NSString *adid = [NSString stringWithUTF8String:value];
GADRewardedAd *rewardedAd = rewardDict[adid];
if (rewardedAd) {
UIViewController *uvc = visibleViewController();
[rewardedAd presentFromRootViewController:uvc userDidEarnRewardHandler:^{
//GADAdReward * reward = rewardedAd.adReward;
UnitySendMessage("IOSCube","ShowAdmobIOSFinish", [adid UTF8String]);
}];
UnitySendMessage("IOSCube","ShowAdmobFromIOS", [adid UTF8String]);
} else {
NSLog(@"Linnea IOS AD 没有准备好:%@", adid);
}
}
void ShowAdmobInterFromIOS(const char* value) {
NSString *adid = [NSString stringWithUTF8String:value];
GADRewardedInterstitialAd *rewardedInterstitialAd = rewardDict[adid];
if (rewardedInterstitialAd) {
UIViewController *uvc = visibleViewController();
//GADAdReward *reward = rewardedInterstitialAd.adReward;
[rewardedInterstitialAd presentFromRootViewController:uvc userDidEarnRewardHandler:^{
UnitySendMessage("IOSCube","ShowAdmobIOSFinish", [adid UTF8String]);
}];
UnitySendMessage("IOSCube","ShowAdmobFromIOS", [adid UTF8String]);
} else {
NSLog(@"Linnea IOS AD 没有准备好:%@", adid);
}
}
}
- 这里是请求和展示的方法,uvc会找不到,我后面会讲怎么获得这个uvc。首先看UnityAppController,因为我们需要用到广告中的事件回调,需要一个委托事件,但是在extern "C"中只是一个方法体,所以我们就必须把方法的请求写在拥有对象的UnityAppController类中,所以请求方法:
NSMutableDictionary *rewardDict = [NSMutableDictionary dictionary];
- (void)loadRewardedAd:(NSString*) adid {
GADRequest *request = [GADRequest request];
[GADRewardedAd
loadWithAdUnitID:adid
request:request
completionHandler:^(GADRewardedAd *ad, NSError *error) {
if (error) {
UnitySendMessage("IOSCube","LoadAdmobIOSFail", [adid UTF8String]);
}else{
UnitySendMessage("IOSCube","LoadAdmobIOSSuccess", [adid UTF8String]);
ad.fullScreenContentDelegate = self;
[rewardDict setObject:ad forKey:adid];
}
}];
}
- (void)loadRewardedInter:(NSString*) adid {
[GADRewardedInterstitialAd
loadWithAdUnitID:adid
request:[GADRequest request]
completionHandler:^(GADRewardedInterstitialAd *_Nullable rewardedInterstitialAd,NSError *_Nullable error) {
if (error) {
UnitySendMessage("IOSCube","LoadAdmobIOSFail", [adid UTF8String]);
}else{
UnitySendMessage("IOSCube","LoadAdmobIOSSuccess", [adid UTF8String]);
rewardedInterstitialAd.fullScreenContentDelegate = self;
[rewardDict setObject:rewardedInterstitialAd forKey:adid];
}
}];
}
- 这里需要在代码前面引入广告库
#import
#import
- 此次定义的rewardDict,在当前类为全局变量,这个类在extern "C"中一样可以得到,所以这里就是把oc++和oc代码都写在已有类下面的好处,不用在其他地方定义变量。
添加广告监听事件
- 这个是一个难点,因为在oc++中是不支持self写法的,而在官方文档中可以看到广泛的用到了self这个关键字。然后就是需要实现接口GADFullScreenContentDelegate,官网的写法根本不可行。不过最终经过实践为还是突破了瓶颈。
在@implementation之前,写入:
@interface UnityAppController()
@end
- 这里是.h中的写法,定义UnityAppController的头文件,注意我们无法在原本的UnityAppController.h 中实现GADFullScreenContentDelegate接口,代码无法编译,因为根本无法导入GADFullScreenContentDelegate的库文件。只有在UnityAppController.m中再次定义UnityAppController的头文件,并且实现接口可行。实现对应的方法:
/// Tells the delegate that the ad failed to present full screen content.
- (void)ad:(nonnull id)ad
didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
NSLog(@"Linnea Ad did fail to present full screen content.");
}
/// Tells the delegate that the ad presented full screen content.
- (void)adDidPresentFullScreenContent:(nonnull id)ad {
NSLog(@"Linnea Ad did present full screen content.");
}
/// Tells the delegate that the ad dismissed full screen content.
- (void)adDidDismissFullScreenContent:(nonnull id)ad {
NSLog(@"Linnea Ad did dismiss full screen content");
UnitySendMessage("IOSCube","CloseAdmobIOSFinish", "close");
}
- 这里是官网中oc代码,到此处编译通过,并且可以监听到每条广告的展示成功/失败,关闭事件。
如何调用广告Show事件,找到uvc。
- 这里遇到问题在展示广告的时候必须传入当前视图,但是我们在UnityAppController中是没有视图的,所以又要另辟蹊径。
UIViewController *getVisibleViewControllerFrom(UIViewController *vc) {
if ([vc isKindOfClass:[UINavigationController class]]) {
return getVisibleViewControllerFrom([((UINavigationController *) vc) visibleViewController]);
} else if ([vc isKindOfClass:[UITabBarController class]]) {
return getVisibleViewControllerFrom([((UITabBarController *) vc) selectedViewController]);
} else {
if (vc.presentedViewController) {
return getVisibleViewControllerFrom(vc.presentedViewController);
} else {
return vc;
}
}
return vc;
}
UIViewController *visibleViewController() {
UIViewController *rootViewController =[[[[UIApplication sharedApplication] delegate] window] rootViewController];
return getVisibleViewControllerFrom(rootViewController);
}
- 这里的方法是找到当前app所在视图,然后返回传递给show方法,广告展示在当前视图中。
到这里,广告请求和展示就完成了,最后写起来几行代码,但是最终变成这个样子不易,经过了很多变形,很多弯路。以后遇到同样问题,按照文中一步步走来,定能引刃而解。