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

How to load admob ads into a unity 5 project?(如何将 admob 广告加载到 unity 5 项目中?)
本文介绍了如何将 admob 广告加载到 unity 5 项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我正在尝试将横幅广告加载到我的 Unity 5 项目中并导出到 iOS.

Basically I'm trying to load a banner ad into my Unity 5 project and export to iOS.

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

Here is the code i'm calling inside of unity which is attached to a game object:

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

有人用这个来投放广告吗?注意:我确实将 xxxxx 替换为正确的广告单元 ID.

Has anyone got this to serve ads? Note: I did replace the xxxxx with the correct Ad Unit ID.

推荐答案

GitHub上有一个项目:

There is an project on GitHub:

Unity Admob 插件

它易于使用,我在这方面取得了成功.

it easy to use,and I have success with this.

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);

这篇关于如何将 admob 广告加载到 unity 5 项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)