iOS平台添加Google Admob -1/2(Unity3D開發之七)

來源:互聯網
上載者:User

iOS平台添加Google Admob -1/2(Unity3D開發之七)

 

 

Unity調用iOS還是非常簡單的,晚上空閑時間寫了unity在IOS平台載入Google Admob。需要的朋友可以看下。

一、首先編寫Admob的iOS的代碼。
請去https://apps.admob.com下載iOS Admob SDK, 並註冊app id。如果這一步不熟悉,請自行Google,網上應該很多。

1、IOSAdmob類,主要實現Admob的建立和載入
IOSAdmob.h

////  IOSAdmob.h//  IOSAdmob////  Created by LiuYanghui on 14-8-4.//  Copyright (c) 2014年 LiuYanghui. All rights reserved.//#import #import GADBannerView.h#import GADBannerViewDelegate.h@interface IOSAdmob : NSObject- (void)startRequestAdmob:(NSString*)admobId AdSize:(GADAdSize)size Position:(CGPoint)pos;- (void)setAdmobHidden:(BOOL)isHidden;@end

IOSAdmob.m

 

////  IOSAdmob.m//  IOSAdmob////  Created by LiuYanghui on 14-8-4.//  Copyright (c) 2014年 LiuYanghui. All rights reserved.//#import IOSAdmob.h@interface IOSAdmob ()    @property (nonatomic) BOOL isHidden;    @property (nonatomic) CGRect adRect;    @property (nonatomic, readonly, strong) UIViewController* rootViewController;    @property (nonatomic, readonly, strong) GADBannerView* adBannerView;@end@implementation IOSAdmob- (id) init{self = [super init];if(self != nil){        _isHidden = NO;        _adRect = CGRectZero;        _rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];        _adBannerView = nil;}return self;}- (void) dealloc{    [_rootViewController release];    [_adBannerView release];    [super dealloc];}- (void) startRequestAdmob:(NSString*)admobId AdSize:(GADAdSize)size Position:(CGPoint)pos{    if (_adBannerView) { return; }        _adBannerView = [[GADBannerView alloc] initWithAdSize:size];//    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {//        //    }else {////    }        _adRect = CGRectMake(pos.x, pos.y, _adBannerView.frame.size.width, _adBannerView.frame.size.height);    _adBannerView.adUnitID = admobId;    _adBannerView.delegate = self;    [_adBannerView setRootViewController:_rootViewController];    _adBannerView.frame = _adRect;    [_rootViewController.view addSubview:_adBannerView];        double delayInSeconds = 5.0;    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){        NSLog(@Retrying to load request);        [_adBannerView loadRequest:[self createRequest]];    });}- (void)setAdmobHidden:(BOOL)isHidden{    _isHidden = isHidden;    if (_adBannerView) {        [_adBannerView setHidden:_isHidden];    }    //        [UIView animateWithDuration:0.5 animations:^ {    //            _adBannerView.frame = _adRect;    //        }];}#pragma mark GADRequest generation// Here we're creating a simple GADRequest and whitelisting the simulator// and two devices for test ads. You should request test ads during development// to avoid generating invalid impressions and clicks.- (GADRequest *)createRequest {    GADRequest *request = [GADRequest request];    return request;}#pragma mark GADBannerViewDelegate impl// Since we've received an ad, let's go ahead and set the frame to display it.- (void)adViewDidReceiveAd:(GADBannerView *)adView {    NSLog(@Received ad);    _adBannerView.frame = _adRect;    [_adBannerView setHidden:_isHidden];}- (void)adView:(GADBannerView *)viewdidFailToReceiveAdWithError:(GADRequestError *)error {    NSLog(@Failed to receive ad with error: %@, [error localizedFailureReason]);    double delayInSeconds = 5.0;    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){        NSLog(@Retrying to load request);        [_adBannerView loadRequest:[self createRequest]];    });}@end

二、Admob對外單例類介面
IOSAdmobManager.h

 

 

////  IOSAdmobManager.h//  IOSAdmob////  Created by LiuYanghui on 14-8-4.//  Copyright (c) 2014年 LiuYanghui. All rights reserved.//#import #import @interface IOSAdmobManager : NSObject+ (IOSAdmobManager *)getInstance;- (void)startRequestAdmob:(NSString*)admobId AdSizeId:(NSInteger)adSizeId Position:(CGPoint)pos;- (void)setAdmobHidden:(BOOL)isHidden;@end

IOSAdmobManager.m

 

 

////  IOSAdmobManager.m//  IOSAdmob////  Created by LiuYanghui on 14-8-4.//  Copyright (c) 2014年 LiuYanghui. All rights reserved.//#import IOSAdmobManager.h#import IOSAdmob.h@interface IOSAdmobManager ()@property (nonatomic, readonly, strong) IOSAdmob* admob;@end@implementation IOSAdmobManager+ (IOSAdmobManager *)getInstance{    static IOSAdmobManager *mgr = nil;    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        mgr = [[self alloc] init];    });    return mgr;}- (id)init{    self = [super init];    if (self) {        _admob = [[IOSAdmob alloc] init];        return self;    }    return nil;}- (void)startRequestAdmob:(NSString*)admobId AdSizeId:(NSInteger)adSizeId Position:(CGPoint)pos{    switch (adSizeId) {        case 1:            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeBanner Position:pos];            break;                    case 2:            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeLargeBanner Position:pos];            break;                    case 3:            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeMediumRectangle Position:pos];            break;                    case 4:            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeFullBanner Position:pos];            break;                    case 5:            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeLeaderboard Position:pos];            break;                    case 6:            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeSkyscraper Position:pos];            break;                    case 7:            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeSmartBannerPortrait Position:pos];            break;                    case 8:            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeSmartBannerLandscape Position:pos];            break;                    default:            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeBanner Position:pos];            break;    }}- (void)setAdmobHidden:(BOOL)isHidden{    [_admob setAdmobHidden:isHidden];}@end

三、為第二步中的單例介面註冊C函數介面,提供給Unity使用
LHAdmob.mm

 

 

////  LHAdmob.mm//  IOSAdmob////  Created by LiuYanghui on 14-8-4.//  Copyright (c) 2014年 LiuYanghui. All rights reserved.//#import IOSAdmobManager.hextern C {    void startRequestAdmob(const char* admobId, int adSizeId, float pixelX, float pixelY)    {        [[IOSAdmobManager getInstance] startRequestAdmob:[NSString stringWithUTF8String:admobId] AdSizeId:adSizeId Position:CGPointMake(pixelX, pixelY)];    }        void setAdmobHidden(bool isHidden)    {        [[IOSAdmobManager getInstance] setAdmobHidden:isHidden];    }}

 

OK,到這裡已經完成了Admob的建立和Unity介面編寫。

注意:
1、Unity匯出的iOS工程預設沒有使用arc,所以以上代碼全部未使用arc。
Xcode->Targets->Build Settings->Objective-C Automatic Reference Counting 設定NO,即關閉arc。

2、針對上訴介面建立什麼類型的xCode工程 ?
你可以建立任意的Xcode工程,編寫以上代碼。也可以建立iOS->Framework & Library->Cocos Touch Static Library 靜態庫工程。

3、以上代碼如何加到Unity?
第一個方法:直接將以上代碼檔案複製到Unity->Assets/Plugins/iOS 檔案夾中,該檔案是Unity指定的外掛程式檔案。
第二個方法:如果你建立的是靜態庫工程,Xcode->Product->Archive 編譯出.a靜態庫檔案,將.a檔案複製到Unity->Assets/Plugins/iOS 檔案夾中。


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.