Unity3D IOS IPhone添加Admob的方法

原地址:http://dong2008hong.blog.163.com/blog/static/4696882720140403119293/

首先阅读官方文档https://developers.google.com/mobile-ads-sdk/docs/

按步就班注册获取AdMob Publisher ID已及开发SDK包和DEMO工程,确保官方的demo工程能正确运行:

点击浏览下一页

如果没法运行,再仔细阅读官方文档!!

为了省事,就直接在BannerExampleViewController上修改

首先添加两个方法,11个用于示广告的方法

+ (BannerExampleViewController* )shareBannerView;

- (id)showAdmob;

- (void)viewDidLoad方法内容移到- (id)showAdmob;

中,并稍作修改:

- (void)viewDidLoad {

[super viewDidLoad];

}

- (id)showAdmob

{

//Initialize the banner off the screen so that it animates up when displaying

self.adBanner = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0,

self.view.frame.size.heightGAD_SIZE_320x50.widthGAD_SIZE_320x50.height)];

// Note: Edit SampleConstants.h to provide a definition for kSampleAdUnitID

// before compiling.

self.adBanner.adUnitID = @"a15049aa7aa110e";

self.adBanner.delegate = self;

[self.adBanner setRootViewController:self];

//[self.view addSubview:self.adBanner];

[[[UIApplication sharedApplication] keyWindowaddSubview:adBanner_];

[self.adBanner loadRequest:[self createRequest]];

return self.adBanner;

}

接下来实现单列:

static BannerExampleViewController* shareBannerView = nil;

+ (BannerExampleViewController *)shareBannerView

{

if (shareBannerView == nil) {

shareBannerView= [[BannerExampleViewController alloc] init];

}

return shareBannerView;

}

最后实现unity需要的类,创建一个UMob.mm类,该类灰常简单,只有一个方法(用于unity中C#调用),

#import "UMob.h"

#import "BannerExampleViewController.h"

@implementationUMob

extern "C"

{

void _adMob()

{

[[BannerExampleViewController shareBannerView] showAdmob];

}

}

@end

Xcode部分完成。把需要的文件拷贝到unity中,注意文件结构

Unity部分就更easy了。创建一个调用admob的类挂到场景中。

usingSystem.Collections;

usingSystem.Runtime.InteropServices;

public class AdmobCall {

[DllImport ("__Internal")]

private static extern void _adMob();

void Start () {

_adMob();

}

}

搞定!build,如果在xcode中运行出错的话,应该是缺少一些framework,具体少那些,参见文章第一行。

原文地址:https://www.cnblogs.com/123ing/p/3912024.html