Monkey Original, welcome reprint. Reprint Please specify: reproduced from COCOS2D Development network –cocos2dev.com, thank you!
Original address: http://www.cocos2dev.com/?p=567
Unity calls iOS is still very simple, night free time to write unity on the iOS platform to load Google Admob. Need a friend to look down.
First, write the code for the AdMob iOS.
Go to https://apps.admob.com to download the iOS AdMob SDK and register your app ID. If this step is not familiar, please google yourself, online should be a lot.
1, Iosadmob class, mainly to achieve the creation and loading AdMob
IOSAdmob.h
iosadmob.h// iosadmob//// Created by Liuyanghui on 14-8-4.// Copyright (c) 2014 Liuyanghui. All rights reserved.//#import <Foundation/Foundation.h> #import "GADBannerView.h" #import " GADBannerViewDelegate.h "@interface iosadmob:nsobject<gadbannerviewdelegate>-(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) {retur N } _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 both devices for test ads. You should request test ads during development//to avoid generating invalid impressions and clicks.-(Gadrequest *) creat erequest {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.-(VO ID) Adviewdidreceivead: (Gadbannerview *) AdView {NSLog (@ "Received ad"); _adbannerview.frame = _adrect; [_adbannerview Sethidden:_ishidden];} -(void) AdView: (Gadbannerview *) Viewdidfailtoreceiveadwitherror: (Gadrequesterror *) error {NSLog (@ "Failed to receive a D 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
Second, AdMob external single-instance class interface
IOSAdmobManager.h
iosadmobmanager.h// iosadmob//// Created by Liuyanghui on 14-8-4.// Copyright (c) 2014 Liuyanghui. All rights reserved.//#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @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{stat IC 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
third, for the single interface in the second step to register the C function interface, provided for unity to use
lhadmob.mm
lhadmob.mm// iosadmob//// Created by Liuyanghui on 14-8-4.// Copyright (c) 2014 Liuyanghui. All rights reserved.//#import "IOSAdmobManager.h" extern "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, this is where AdMob was created and the Unity interface was written.
Attention:
1. The Unity-exported iOS project does not use ARC by default, so none of the above code uses arc.
Xcode->targets->build settings->objective-c Automatic Reference Counting set No, that is, turn off arc.
2. What type of Xcode project is created for the appeal interface?
You can create any Xcode project and write the code above. You can also create Ios->framework & Library->cocos Touch Static Library project.
3. How does the above code add to unity?
The first method: Copy the above code file directly into the Unity->assets/plugins/ios folder, which is the plug-in file specified by unity.
The second method: If you are creating a static library project, Xcode->product->archive compiles the. A static library file and copies. A files to the Unity->assets/plugins/ios folder.