【原】Unity3D调用Admob广告服务

unity调用Admob广告服务和IOS调用没有太大区别,几乎80%的工作都是OC完成,Unity仅需要以插件形式调用OC函数就成,所以此文需要一定的OC基础,有了OC的基础,仅需要注意一点,unity导出的xcode工程与普通Xcode不同之处在于,unity导出的iPhone_target2AppDelegate文件不起作用,他把所有的代码都放到AppController里。祝大家好运!

Admob1.h文件
#import <UIKit/UIKit.h>
#import "GADBannerViewDelegate.h"

@class GADBannerView,GADRequest;

@interface Admob1 : UIViewController<GADBannerViewDelegate>{
    GADBannerView *adBanner_;
    UIView *shareView;
}
@property(nonatomic,retain)GADBannerView *adBanner;
@property(nonatomic,retain)UIView *shareView;
+(Admob1*)returnAdmobView;
-(GADRequest*)createRequest;
-(id)admobShow;
-(void)refreshAd;
@end
Admob1.m文件
#import "Admob1.h"
#import "GADBannerView.h"
#import  "GADRequest.h"

#define AdmobID @"****************"//admob的ID

@implementation Admob1

@synthesize adBanner=adBanner_;
@synthesize shareView;
static Admob1* admob;

+(Admob1*)returnAdmobView{
   
    if(admob==NULL){
       admob=[[Admob1 alloc]init];
    }
    return  admob;
}

-(id)admobShow{
    CGRect frame=CGRectMake(0.0,0.0,//self.view.frame.size.height-GAD_SIZE_320x50.height
                        GAD_SIZE_320x50.width,
                        GAD_SIZE_320x50.height);
    UIView *tempView=[[[UIView alloc]initWithFrame:frame]autorelease];
    adBanner_=[[[GADBannerView alloc]initWithFrame:frame] retain];
    adBanner_.adUnitID=AdmobID;
    adBanner_.delegate=self;
    [adBanner_ setRootViewController:self];
    [[[UIApplication sharedApplication] keyWindow]  addSubview:tempView];//把新建的View添加到整个视图的window上
    [tempView addSubview:adBanner_];//把广告view添加到新建的view上
    [adBanner_ loadRequest:[self createRequest]];

    tempView.transform = CGAffineTransformMakeRotation(M_PI_2);//把新建的view选装90度,也就意味着广告被选装90度,这个可以根据需求来,(注意:读者可能很纳闷,为什么要无缘无故多创建一个tempView,因为要对广告做旋转,直接操作adBanner_会有错误,所以必须的创建tempView,通过他来旋转。不做旋转的朋友可以直接[[[UIApplication sharedApplication] keyWindow]  addSubview:adBanner_]),省略对tempView的操作)

    [[NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(refreshAd) userInfo:nil repeats:YES]retain];//添加循环计数器,每隔15秒调用一次刷新函数
    tempView.hidden=YES;//因为不想让游戏一起来,就看到广告 所以对广告隐藏,需要的时候 设置他显示
    return tempView;
}

-(void)refreshAd
{ 
    [adBanner_ loadRequest:[self createRequest]];//刷新广告
}

     -(void)dealloc{
        adBanner_.delegate=nil;
         [adBanner_ release];
         [super dealloc];
     }

#pragma mark GADRequest generation

// Here we're creating a simple GADRequest and whitelisting the application
// 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

-(void)adViewDidReceiveAd:(GADBannerView *)adview{
//    [UIView animateWithDuration:1.0 animations:^{
//        adview.frame=CGRectMake(0.0,50.0,//self.view.frame.size.height-adview.frame.size.height
//                                adview.frame.size.width,
//                                adview.frame.size.height);
//    }];
}

-(void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error{
    NSLog(@"failed to receive ad with error :%@",[error localizedFailureReason]);
}
@end
.mm文件

#import "Admob1.h"
extern "C" {

     void _Admob() //C#中以插件的形式调用  此函数在start函数里面调用
    {
        [Admob1 returnAdmobView].shareView=[[Admob1 returnAdmobView] admobShow];
    }
   void _setAdHide(int bHide, int x, int y, int width, int height)//调用这个来设置广告的位置 大小 是否可见
    {
      if([Admob1 returnAdmobView].shareView!=NULL)
      {
        if(bHide==1)
        {
            [[Admob1 returnAdmobView].shareView setHidden:NO];
            ((UIView*)[[Admob1 returnAdmobView].shareView ]).frame=CGRectMake(x,y,width,height);
        }
        else
        {
        [[Admob1 returnAdmobView].shareView setHidden:YES];
        }
      }
}

 

Admob SDK 下载地

posted @ 2012-07-10 23:21  U_探索  阅读(2617)  评论(2编辑  收藏  举报