您的位置:威尼斯城vnsc登入平台 > 网络信息 > 威尼斯城vnsc登入平台iOS平台添加Google Admob -1/2(

威尼斯城vnsc登入平台iOS平台添加Google Admob -1/2(

2019-12-01 05:46

iOS平台添加Google Admob -2/2(Unity3D开发之八)

 

威尼斯城vnsc登入平台 1

在上一篇文章中主要是编写了iOS Admob的接口实现。那么现在我们要做的事就是在unity中调用iOS Admob并展示。

一、实现Unity中对外接口,内部负责调用iOS Admob接口。
LHiOSAdmob.cs

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System.Linq;
using System.Collections.Generic;

public enum GADAdSize:int
{
 // iPhone and iPod Touch ad size. Typically 320x50.
 kGADAdSizeBanner = 1,

 // Taller version of kGADAdSizeBanner. Typically 320x100.
 kGADAdSizeLargeBanner,

 // Medium Rectangle size for the iPad (especially in a UISplitView's left pane). Typically 300x250.
 kGADAdSizeMediumRectangle,

 // Full Banner size for the iPad (especially in a UIPopoverController or in
 // UIModalPresentationFormSheet). Typically 468x60.
 kGADAdSizeFullBanner,

 // Leaderboard size for the iPad. Typically 728x90.
 kGADAdSizeLeaderboard,

 // Skyscraper size for the iPad. Mediation only. AdMob/Google does not offer this size. Typically
 // 120x600.
 kGADAdSizeSkyscraper,

 // An ad size that spans the full width of the application in portrait orientation. The height is
 // typically 50 pixels on an iPhone/iPod UI, and 90 pixels tall on an iPad UI.
 kGADAdSizeSmartBannerPortrait,

 // An ad size that spans the full width of the application in landscape orientation. The height is
 // typically 32 pixels on an iPhone/iPod UI, and 90 pixels tall on an iPad UI.
 kGADAdSizeSmartBannerLandscape
}

public class LHiOSAdmob : MonoBehaviour {
 [DllImport(__Internal)]
  private static extern void startRequestAdmob(string admobId, int adSizeId, float pixelX, float pixelY);
 [DllImport(__Internal)]
 private static extern void setAdmobHidden(bool isHidden);

 public static LHiOSAdmob Instance;
 private GADAdSize adSize;

 void Awake()
 {
  // singleton
  if (Instance != null)
  {
   Debug.LogError(Multiple instances of LHiOSAdmob);
  }
  Instance = this;
 }

 // Init google admob
 // It will request the admob after five seconds.
 // It's will auto show admob.
 public void InitAdmob(string admobId, GADAdSize size, Vector2 pos)
 {
  #if UNITY_IPHONE
  int adSizeId = (int)size;
  startRequestAdmob(admobId, adSizeId, pos.x, pos.y);
  #else
  Debug.Log(Admob only run on iPhone platform);
  #endif
 }

 // Set Admob BannerView is visible or not
 public void SetAdmobVisible(bool isVisible)
 {
  #if UNITY_IPHONE
  setAdmobHidden(!isVisible);
  #else
  Debug.Log(Admob only run on iPhone platform);
  #endif
 }

 // Use this for initialization
 void Start () {

 }

 // Update is called once per frame
 void Update () {

 }
}

这是一个单例接口类,内部负责调用iOS Admob接口,并开放对外接口。供其他c#文件调用。
注意:你可以制作一个Prefab,并包含该脚本。将该Prefab拖放到场景中。在需要显示admob的地方调用。

二、测试admob显示,也就是在你需要的地方。

TestAdmob.cs

using UnityEngine;
using System.Collections;

public class TestAdmob : MonoBehaviour {
 // Use this for initialization
 void Start () {
  LHiOSAdmob.Instance.InitAdmob(your admob id, GADAdSize.kGADAdSizeSmartBannerLandscape, new Vector2(0, 150));
 }

 // Update is called once per frame
 void Update () {

 }
}

ok, 现在可以Build iOS工程了。

注意:

Build的iOS工程,要添加对应的编译选项和iOS框架。

1、Other Linker Flags 添加 -ObjC

2、Xcode->Targets->Build Phases->Link Binary With Libraries 添加以下框架:
AdSupport
AudioToolbox
AVFoundation
CoreGraphics
MessageUI
StoreKit
SystemConfiguration

http://www.bkjia.com/IOSjc/862073.htmlwww.bkjia.comtruehttp://www.bkjia.com/IOSjc/862073.htmlTechArticleiOS平台添加Google Admob -2/2(Unity3D开发之八) 在上一篇文章中主要是编写了iOS Admob的接口实现。那么现在我们要做的事就是在unity中调用iOS...

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

 

 

威尼斯城vnsc登入平台 2

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 *)view
didFailToReceiveAdWithError:(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.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,到这里已经完成了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 文件夹中。

 

http://www.bkjia.com/IOSjc/862072.htmlwww.bkjia.comtruehttp://www.bkjia.com/IOSjc/862072.htmlTechArticleiOS平台添加Google Admob -1/2(Unity3D开发之七) Unity调用iOS还是非常简单的,晚上空闲时间写了unity在IOS平台加载Google Admob。需要的朋友可以看...

本文由威尼斯城vnsc登入平台发布于网络信息,转载请注明出处:威尼斯城vnsc登入平台iOS平台添加Google Admob -1/2(

关键词:

  • 上一篇:没有了
  • 下一篇:没有了