如何将admob广告加载到unity 5项目中?

时间:2015-05-05 20:38:41

标签: c# ios unity3d admob

基本上我尝试将横幅广告加载到Unity 5项目并导出到iOS。

这是我在Unity内部调用的代码,它附加在游戏对象上:

using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
using System;

public class AdController : MonoBehaviour {

    InterstitialAd interstitial;
    BannerView bannerView;

    void Start () {

        //------ Banner Ad -------
        // Create a 320x50 banner at the top of the screen.
        // Put your Admob banner ad id here
        bannerView = new BannerView(
            "ca-app-pub-xxxxxxxxxxxxxxxx", AdSize.SmartBanner, AdPosition.Top);
        // Create ad request
        AdRequest request = new AdRequest.Builder().Build();
        // Load the banner with the request.
        bannerView.LoadAd(request);        
        bannerView.Show();

        //---- Interstitial Ad -----
        // Initialize an InterstitialAd.
        // Put your admob interstitial ad id here:
        interstitial = new InterstitialAd("ca-app-pub-xxxxxxxxxxxxxxx");

        //Add callback for when ad is loaded
        interstitial.AdLoaded += HandleAdLoaded;

        // Create an ad request.
        AdRequest requestInterstitial = new AdRequest.Builder().Build();
        // Load the interstitial with the request.
        interstitial.LoadAd(requestInterstitial);
    }



    public void HandleAdLoaded(object sender, EventArgs args) {

        interstitial.Show ();
    }


    void OnDestroy(){
        if (interstitial!=null) {
            interstitial.AdLoaded -= HandleAdLoaded;
            interstitial.Destroy ();
        }
        if(bannerView!=null){
            bannerView.Destroy ();
        }
    }

}

我正在使用:

  • Unity 5.0.1f1
  • Xcode 6.3
  • Google Unity插件2.2.1
  • Google Ads SDK 7.2.1

有人有这个广告吗?注意:我确实用正确的广告单元ID替换了xxxxx。

2 个答案:

答案 0 :(得分:1)

GitHub上有一个项目:

Unity Admob Plugin

它易于使用,我在此方面取得了成功。

using admob;
...
Admob.Instance().initAdmob("admob banner id", "admob interstitial id");//admob id with format ca-app-pub-2796046890663330/756767388
Admob.Instance().showBannerRelative(AdSize.Banner, AdPosition.BOTTOM_CENTER, 0);

答案 1 :(得分:0)

  1. 在Unity项目中添加一些代码
    • 将此c#类添加到Unity项目
    • 从项目的某个地方调用此类的功能
  2. 尽早致电构造函数“AD_AdsiOS”并传递您的AdMob广告ID
  3. 调用“ShowInterstitialAd”以显示全屏广告
  4. 调用“ShowBannerAdTopRight”以在右上方显示横幅广告
  5. 调用“HideBannerAds”隐藏横幅广告
  6. #if UNITY_IPHONE
    using UnityEngine;
    using System.Collections;
    using System.Runtime.InteropServices;
     
    public class AD_AdsiOS
    {
    #if !UNITY_EDITOR
        [DllImport ("__Internal")]
        private static extern void _initAds(string p_adMobID);
        [DllImport ("__Internal")]
        private static extern void _showInterstitialAd();
        [DllImport ("__Internal")]
        private static extern void _showBannerAdTopRight();
        [DllImport ("__Internal")]
        private static extern void _hideBannerAds();
    #endif
     
        public AD_AdsiOS(string p_adMobID)
        {
    #if UNITY_EDITOR
            Debug.Log("AD_AdsiOS: will not work in editor.");
    #else
            _initAds(p_adMobID);
    #endif
        }
     
        public void ShowInterstitialAd()
        {
    #if UNITY_EDITOR
            Debug.Log("AD_AdsiOS: ShowInterstitialAd called in editor.");
    #else
            _showInterstitialAd();
    #endif
        }
     
        public void ShowBannerAdTopRight()
        {
    #if UNITY_EDITOR
            Debug.Log("AD_AdsiOS: ShowBannerAdTopRight called in editor.");
    #else
            _showBannerAdTopRight();
    #endif
        }
     
        public void HideBannerAds()
        {
    #if UNITY_EDITOR
            Debug.Log("AD_AdsiOS: HideBannerAds called in editor.");
    #else
            _hideBannerAds();
    #endif
        }
    }
    #endif

    • 编译为Xcode
    • 第1步完成:)

      1. 使用Xcode管理库(来源:https://developers.google.com/mobile-ads-sdk/docs/
    • 下载AdMob SDK
    • 在项目根文件夹中创建一个名为“GoogleAdMobAdsSdkiOS”
    • 的新文件夹
    • 将下载的SDK中的所有文件(但不是“附加组件”文件夹)复制到新的“GoogleAdMobAdsSdkiOS”中
    • 右键单击您的项目(“Unity-iPhone”)并按“添加文件到Unity iPhone”并在项目根目录中选择新的“GoogleAdMobAdsSdkiOS”文件夹
    • 在Build Phases选项卡下打开Link Binary With Libraries下拉列表。使用变为可见的+按钮从iOS SDK添加框架。将SystemConfiguration,StoreKit,MessageUI,AVFoundation和AdSupport添加到两个目标(检查故障排除步骤1和2)
    • 您现在需要将-ObjC添加到其他链接器标志!THE PROJECT! (不是目标)
    • 第2步完成:D

      1. 添加一些代码(来源:https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals
    • 将以下两个文件添加到项目的“Classes”文件夹中并导入

    MyAdMobAdsImpl.h

    #ifndef __Unity_iPhone__MyAdMobAdsImpl__
    #define __Unity_iPhone__MyAdMobAdsImpl__
    
    
    void InitMyAdMob(UIView* p_rootView, UIViewController* p_rootController);
    void DestroyMyAdMob();
    
    #endif /* defined(__Unity_iPhone__MyAdMobAdsImpl__) */
    

    MyAdMobAdsImpl.mm

    #import "GADBannerView.h"
    #import "GADInterstitial.h"
     
    #include "MyAdMobAdsImpl.h"
     
    NSString* _adMobID;
    UIViewController* _adRootViewController;
    GADBannerView* _adBanner;
    GADInterstitial *_adInterstitial;
    float _adBannerRelWidth;
    float _adBannerRelHeight;
     
    extern "C"
    {
        void _initAds (const char* p_adMobID)
        {
            _adMobID = [NSString stringWithUTF8String:p_adMobID];
     
            _adBanner.adUnitID = _adMobID;
     
            _adInterstitial.adUnitID = _adMobID;
            GADRequest *request = [GADRequest request];
            [_adInterstitial loadRequest:request];
     
        }
     
        void _showInterstitialAd ()
        {
            [_adInterstitial presentFromRootViewController:_adRootViewController];
            // get next add
            _adInterstitial = [[GADInterstitial alloc] init];
            _adInterstitial.adUnitID = _adMobID;
            GADRequest *request = [GADRequest request];
            [_adInterstitial loadRequest:request];
        }
     
        void _showBannerAdTopRight ()
        {
            _adBanner.hidden = NO;
            // force refresh
            GADRequest *request = [GADRequest request];
            [_adBanner loadRequest:request];
        }
     
        void _hideBannerAds ()
        {
            _adBanner.hidden = YES;
         }
     
        float _getAdsRelativeWidth () { return _adBannerRelWidth; }
     
        float _getAdsRelativeHeight () { return _adBannerRelHeight; }
    }
     
    void InitMyAdMob(UIView* p_rootView, UIViewController* p_rootController)
    {
        _adRootViewController = p_rootController;
        // Initialize the banner at the bottom of the screen.
        CGPoint origin = CGPointMake(0.0, 0.0);
        // Use predefined GADAdSize constants to define the GADBannerView.
        _adBanner = [[[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner origin:origin] autorelease];
        [_adBanner setRootViewController:p_rootController];
        [p_rootView addSubview:_adBanner];
        // ios frame size is not rotated -> exchange width-height
        _adBannerRelWidth = kGADAdSizeBanner.size.width / p_rootView.frame.size.height;
        _adBannerRelHeight = kGADAdSizeBanner.size.height / p_rootView.frame.size.width;
        _adBanner.center = CGPointMake(_adBanner.center.x+
                                       MAX(p_rootView.frame.size.height-kGADAdSizeBanner.size.width,
                                           p_rootView.frame.size.width-kGADAdSizeBanner.size.width),
                                       _adBanner.center.y);
     
        // Initialize the interstitial ad
        _adInterstitial = [[GADInterstitial alloc] init];
    }
     
    void DestroyMyAdMob()
    {
        _adBanner.delegate = nil;
        [_adBanner release];
     
        [_adInterstitial release];
    }

    这两个文件实现了AdMob逻辑。使用此代码,可以在右上角显示和隐藏横幅广告,并显示全屏广告。此外,使用getAdsRelative *,您可以在相对屏幕空间中获得横幅的大小(宽度为0.1表示横幅占用0.1 * Screen.width像素)。为简单起见,这些方法不是第一步中的引用,但如果需要,可以使用它们。

    • 现在必须从Unity类调用“InitMyAdMob”和“DestroyMyAdMob”方法
    • 打开“UI / UnityAppController + ViewHandling.mm”文件
    • 在最后一个#include下面添加以下代码: 代码(CSharp):

    //// ******************* ////
    //// *** AdMob START *** ////
    //// ******************* ////
      #import "MyAdMobAdsImpl.h"
    //// ******************* ////
    //// *** AdMob END   *** ////
    //// ******************* ////

    • 在“[_window makeKeyAndVisible];”之前将以下代码添加到“createViewHierarchy”函数中 代码(CSharp):

     //// ******************* ////
        //// *** AdMob START *** ////
        //// ******************* ////
        InitMyAdMob(_rootView, _rootController);
        //// ******************* ////
        //// *** AdMob END   *** ////
        //// ******************* ////

    • 将以下代码添加到“releaseViewHierarchy”函数的开头 代码(CSharp):

    //// ******************* ////
        //// *** AdMob START *** ////
        //// ******************* ////
        DestroyMyAdMob();
        //// ******************* ////
        //// *** AdMob END   *** ////
        //// ******************* ////

    • 第3步完成:p

      1. 故障排除
      1. 如果你找到“未找到-liPhone-lib的库”,请确保将其移至“构建阶段”选项卡中的顶部 - >“链接二进制文件库”下拉列表
      1. 如果你得到“Undefined symbol ... _OBJC_CLASS _ $ _ CTTelephonyNetworkInfo”在Build Phases选项卡中添加CoreTelephony-> Link Binary With Libraries下拉列表
      1. 如果你在NSObjCRuntime.h或类似的NSObjC *文件中得到大约20个错误: 确保为添加到项目中的文件将“文件类型”下拉列表设置为“Objective-C ++ Source”(在选择文件的最右侧列中)。这样它就可以正确处理文件并使用正确的路径和内容。 来源:http://forum.thegamecreators.com/?m=forum_view&t=201075&b=41
      1. 在较新的AdCob版本的XCode版本中,需要添加其他库(EventKit.framework和EventKitUI.framework)。或者你会得到: “_OBJC_CLASS _ $ _ EKEvent”,引自: libGoogleAdMobAds.a中的objc-class-ref(GADOpener.o) “_OBJC_CLASS _ $ _ EKEventEditViewController”,引自: libGoogleAdMobAds.a中的objc-class-ref(GADOpener.o)

    http://stackoverflow.com/questions/25950990/undefined-symbols-for-architecture-when-add-admob