標籤:style blog http os io strong 檔案 for
原地址:http://dong2008hong.blog.163.com/blog/static/4696882720140403119293/
首先閱讀官方文檔https://developers.google.com/mobile-ads-sdk/docs/
按步就班註冊擷取AdMob Publisher ID已及開發SDK包和DEMO工程,確保官方的demo工程能正確運行:
如果沒法運行,再仔細閱讀官方文檔!!
為了省事,就直接在BannerExampleViewController上修改
首先添加兩個方法,1個單列1個用於顯示廣告的方法
+ (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.height, GAD_SIZE_320x50.width, GAD_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] keyWindow] addSubview: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,具體少那些,參見文章第一行。