Admob横幅广告在Unity3D中无法正常运行

时间:2018-07-22 09:23:19

标签: c# android unity3d admob

我将 Ad-mob横幅广告集成到我的Unity游戏项目 android 构建

但是有时候广告会展示,但是大多数时候却没有展示

显示百分比仅为100%中的 4%

我不知道为什么会这样

我从developers.google获得了以下代码,并且能够展示广告,但是坦率地说,它不会在每个初创企业上展示横幅应用程序,也就是说,它仅在构建后的第一个或第二个初创企业中展示广告,而不再显示。 / p>

我试图找到解决方案,但看起来所有解决方案都是相同的。

所以,我被困在这里

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

public class BannerAd : MonoBehaviour
{

    private BannerView bannerView;

    // Use this for initialization
    void Start ()
    {
        string appID = "ca-app-pub-id"; // this is appId

        MobileAds.Initialize (appID);

        string adUnitId = "ca-app-pub-adUnitId"; // adUnitID

        this.showBannerAd (adUnitId);
    }

    private void showBannerAd (string adUnitId)
    {



        //Create a custom ad size at the bottom of the screen

        AdSize adSize = new AdSize (250, 50);
        bannerView = new BannerView (adUnitId, adSize, AdPosition.Bottom);

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder ().Build ();

        // Load the banner with the request.
        bannerView.LoadAd (request);

        StartCoroutine(bannerAdTime());
    }

    IEnumerator bannerAdTime ()
    {
        yield return new WaitForSeconds (300f);

        RequestNewAd();
    }

    private void RequestNewAd ()
    {
        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder ().Build ();

        // Load the banner with the request.
        bannerView.LoadAd (request);

        StartCoroutine(bannerAdTime());
    }

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

    }
}

2 个答案:

答案 0 :(得分:2)

1。检查您是否已将插件安装为文档https://github.com/unity-plugins/Unity-Admob

2。编辑AndroidManifest.xml并配置Admob APP ID 此配置由admob从17.0版开始要求,如果未配置,APP将崩溃。在应用中添加元数据标记并将值设置为admob appid

<meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="your admob app id"/>

示例代码

    <application  android:theme="@style/UnityThemeSelector" 
   android:icon="@drawable/app_icon" 
   android:label="@string/app_name" >
           <activity
        android:name="com.unity3d.player.UnityPlayerActivity"
        android:label="@string/app_name" >
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>
        <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-3940256099942544~3347511713"/>
  </application>

3.Init Admob Unity插件 创建一个C#脚本,将脚本拖到场景中的对象上,在脚本文件中添加以下代码

using admob;
Admob.Instance().initSDK("admob appid", new AdProperties());//admob id with format ca-app-pub-3940256099942544~3347511713
//Admob.Instance().initAdmob("ca-app-pub-3940256099942544/2934735716", new AdProperties());

4。在Unity App中添加Admob横幅

Admob.Instance().showBannerRelative("your admob banner unit id",AdSize.BANNER, AdPosition.BOTTOM_CENTER, 0);

答案 1 :(得分:1)

我找到了解决方法

问题出在我的仿真器

使用 Real Device 可以正常运行

并且不需要使用协同例程

以下代码足以显​​示BannderAds

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

public class BannerAd : MonoBehaviour
{

    private BannerView bannerView;

    // Use this for initialization
    void Start ()
    {
        string appID = "ca-app-pub-id"; // this is appId

        MobileAds.Initialize (appID);

        string adUnitId = "ca-app-pub-adUnitId"; // adUnitID

        this.showBannerAd (adUnitId);
    }

    private void showBannerAd (string adUnitId)
    {

        //Create a custom ad size at the bottom of the screen

        AdSize adSize = new AdSize (250, 50);
        bannerView = new BannerView (adUnitId, adSize, AdPosition.Bottom);

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder ().Build ();

        // Load the banner with the request.
        bannerView.LoadAd (request);

    }


}