Unity3D IOS IPhone添加Admob的方法

來源:互聯網
上載者:User

標籤: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上修改

首先添加兩個方法,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,具體少那些,參見文章第一行。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.