简体   繁体   English

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

[英]Admob Banner Ads are not working properly in Unity3D

I integrate Ad-mob banner ads in my Unity game project android build 我在我的Unity游戏项目android build中集成了Ad-mob横幅广告

But Sometimes the ad is showing but most of time it's not 但是有时候广告在展示,但是大多数时候却没有

showing percentage will be just just 4% out of 100% 显示百分比仅占100%中的4%

I don't know why this happening 我不知道为什么会这样

I got this following code from developers.google and i am able to show ads but it does not show the banner app on every startup frankly speaking it only shows ads in first or 2nd startup after build and not anymore. 我从developers.google获得了以下代码,并且能够显示广告,但坦率地说,它不会在每个初创企业上都显示横幅应用程序,它只在构建后的第一个或第二个初创企业中显示广告,而不再显示。

I try to find solutions but looks like all solutions are same. 我尝试找到解决方案,但看起来所有解决方案都是相同的。

So, I am Stuck in here 所以,我被困在这里

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 ()
    {

    }
}

1.Check you have install plugin as document https://github.com/unity-plugins/Unity-Admob 1.检查您是否已将安装插件作为文档https://github.com/unity-plugins/Unity-Admob

2.Edit AndroidManifest.xml and config Admob APP ID This configuration is reqired by admob from version 17.0,APP will crash if not config .Add a meta-data tag in application and set value to your admob appid 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"/>

Sample code 样例代码

    <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 Plugin Create AC# script ,drag the script to a object on scene , add the follow code in the script file 3.Init Admob Unity Plugin创建AC#脚本,将脚本拖到场景中的对象上,在脚本文件中添加以下代码

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.Add Admob Banner in Unity App 4,在Unity App中添加Admob Banner

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

I found the solution 我找到了解决方案

The problem was with my Emulator 问题出在我的模拟器上

With The Real Device it work fine 使用Real Device可以正常工作

and there's no need to use the co-routine 无需使用协同例程

the following codes will be enough to show the BannderAds 以下代码足以显​​示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);

    }


}

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

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