繁体   English   中英

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

[英]How to load admob ads into a unity 5 project?

基本上我正在尝试将横幅广告加载到我的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。

GitHub上有一个项目:

Unity Admob插件

它易于使用,我对此有所成功。

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. 在Unity项目中添加一些代码
    • 将此c#类添加到Unity项目中
    • 从项目中的某个地方调用此类的函数
  2. 尽早调用构造函数“AD_AdsiOS”并传递您的AdMob广告ID
  3. 调用“ShowInterstitialAd”以显示全屏广告
  4. 调用“ShowBannerAdTopRight”以在右上方显示横幅广告
  5. 调用“HideBannerAds”隐藏横幅广告

 #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:Code(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的库”,请确保将它移到Build Phases选项卡的顶部 - > Link Binary With Libraries下拉列表
    1. 如果你得到“Undefined symbol ... _OBJC_CLASS _ $ _ CTTelephonyNetworkInfo”在Build Phases选项卡中添加CoreTelephony-> Link Binary With Libraries下拉列表
    1. 如果你在NSObjCRuntime.h或类似的NSObjC *文件中得到大约20个错误:确保'File Type'下拉列表设置为'Objective-C ++ Source'(当你选择文件时在最右边的列中)您添加到项目的文件。 这样它就可以正确处理文件并使用正确的路径和内容。 来源: http//forum.thegamecreators.com/?m = forum_view&t = 201075&b = 41
    1. 在较新的AdMob版本的较新的XCode版本中,需要添加额外的库(EventKit.framework和EventKitUI.framework)。 或者你会得到:“_ OBJC_CLASS _ $ _ EKEvent”,引用自:libGoogleAdMobAds.a(GADOpener.o)中的objc-class-ref“_OBJC_CLASS _ $ _ EKEventEditViewController”,引用自:libGoogleAdMobAds.a中的objc-class-ref(GADOpener.o )

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM