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

标签 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 ();
        }
    }

}

我正在使用:

  • 统一 5.0.1f1
  • Xcode 6.3
  • Google Unity 插件 2.2.1
  • 谷歌广告 SDK 7.2.1

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

最佳答案

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

关于c# - 如何将 admob 广告加载到 unity 5 项目中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30062841/

相关文章:

c# - 当 GameObject 触发时在 Unity 控制台中打印消息? Unity5/2D

c# - 在.net 6中,当服务初始化发生在值的配置绑定(bind)之前时,如何将参数传递给构造函数?

C# 转换问题 - 简洁

ios - 无法使用类型为“(UInt32, UnsafePointer<Int8>, UInt, [CChar], Int, UnsafeMutablePointer<CUnsignedChar>) 的参数列表调用 'CCHmac'

ios - 由远程对等 gcdasyncsocket 关闭的套接字

ios - 使用 Core Data 时如何使用 Watch Connectivity 共享数据

unity3d - 在Unity中设置Google Play游戏时,我不断收到无效的类名:发现歧义匹配错误

c# - 使用 Base64 编码将字节数组转换为 SecureString 的安全方法

c# - 解析前过滤LinqToExcel中的空白行(转换/映射)

android - 无法使用 Unity 插件中的 android jar 中的 context.getClassLoader.getResaurceAsStream 访问图像