Admob广告不会显示在编辑器或物理设备中

时间:2019-04-14 16:57:59

标签: android unity3d admob

我正在使用Unity来创建游戏,并且希望使用admob设置广告。 我从here下载了插件,并拥有一个Admob帐户,我在其中添加了我的应用程序,并且确实使用该应用程序的ID设​​置了AndroidManifest.xml文件。 我用来实施广告的C#代码如下:

using GoogleMobileAds.Api;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Assets.Scripts
{
    public class AdManager : MonoBehaviour
    {
        private BannerView bannerView;

        private InterstitialAd interstitial;

        private string appId = null;

#if UNITY_ANDROID
        string testBannerAd = "ca-app-pub-3940256099942544/6300978111";
#elif UNITY_IPHONE
        string adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
        string adUnitId = "unexpected_platform";
#endif

        private void Start()
        {
            string appId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxx";
            MobileAds.Initialize(appId);
            bannerView.OnAdLoaded += OnBannerLoaded;
        }

        public void RequestInterstitial()
        {

            // Initialize an InterstitialAd.
            this.interstitial = new InterstitialAd(testBannerAd);

            // Create an empty ad request.
            AdRequest request = new AdRequest.Builder().Build();
            // Load the interstitial with the request.
            this.interstitial.LoadAd(request);
        }

        public void RequestBanner()
        {
            if (bannerView != null)
                bannerView.Destroy();
            bannerView = new BannerView(testBannerAd, AdSize.SmartBanner, AdPosition.Bottom);
            AdRequest request = new AdRequest.Builder().Build();
            // Load the interstitial with the request.
            this.bannerView.LoadAd(request);
        }

        public void ShowInterstitial()
        {
            if (this.interstitial.IsLoaded())
            {
                this.interstitial.Show();
            }
            else
            {
                Debug.Log("Interstitial ain't loaded yet. ");
            }
        }

        public void HideInterstitial()
        {
            this.interstitial.Destroy();
        }

        public void ShowBanner()
        {
            bannerView.Show();
        }

        public void OnBannerLoaded(object sender, EventArgs args)
        {
            Debug.Log("Banner loaded");
            bannerView.Show();
        }

        public void HideBanner()
        {
            bannerView.Hide();
        }
    }
}

请注意,我使用的是测试横幅广告的ID,而不是注册测试设备。

在编辑器中,我确实收到诸如Dummy ctorDummy CreateBannerViewDummy ShowBannerView之类的消息。生成后,所有内容都不会出现在编辑器中,也不会出现在物理设备中。

修改

已连接到Logcat并获得了此信息:

  

2019-04-14 19:14:59.959 12626-12642 /? E / Unity:AndroidJavaException:   java.lang.ClassNotFoundException:com.google.android.gms.ads.AdSize       java.lang.ClassNotFoundException:com.google.android.gms.ads.AdSize           在java.lang.Class.classForName(本机方法)           在java.lang.Class.forName(Class.java:453)           在java.lang.Class.forName(Class.java:378)           在com.unity3d.player.UnityPlayer.nativeRender(本机方法)           在com.unity3d.player.UnityPlayer.c(未知来源:0)           在com.unity3d.player.UnityPlayer $ e $ 2.queueIdle(未知来源:72)           在android.os.MessageQueue.next(MessageQueue.java:394)           在android.os.Looper.loop(Looper.java:142)           在com.unity3d.player.UnityPlayer $ e.run(未知来源:32)        原因:java.lang.ClassNotFoundException:在路径:DexPathList [[zip文件]中找不到类“ com.google.android.gms.ads.AdSize”   “ /data/app/com.mycompany.mytestapp-i1g6KcCN125pL34LmxAIeQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.mycompany.mytestapp-i1g6KcCN125pL34LmxAIeQ==/lib/x86,   /data/app/com.mycompany.mytestapp-i1g6KcCN125pL34LmxAIeQ==/base.apk!/lib/x86,   / 2019-04-14 19:14:59.995 12626-12642 /? E / Unity:AndroidJavaException:   java.lang.ClassNotFoundException:com.google.android.gms.ads.MobileAds       java.lang.ClassNotFoundException:com.google.android.gms.ads.MobileAds           在java.lang.Class.classForName(本机方法)           在java.lang.Class.forName(Class.java:453)           在java.lang.Class.forName(Class.java:378)           在com.unity3d.player.UnityPlayer.nativeRender(本机方法)           在com.unity3d.player.UnityPlayer.c(未知来源:0)           在com.unity3d.player.UnityPlayer $ e $ 2.queueIdle(未知来源:72)           在android.os.MessageQueue.next(MessageQueue.java:394)           在android.os.Looper.loop(Looper.java:142)           在com.unity3d.player.UnityPlayer $ e.run(未知来源:32)        原因:java.lang.ClassNotFoundException:在路径:DexPathList [[zip文件]中找不到类“ com.google.android.gms.ads.MobileAds”   “ /data/app/com.mycompany.mytestapp-i1g6KcCN125pL34LmxAIeQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.mycompany.mytestapp-i1g6KcCN125pL34LmxAIeQ==/lib/x86,   /data/app/com.mycompany.mytestapp-i1g6KcCN125pL34LmxAIeQ == / base.apk!/ l

因此,我在线上查找了该文件,发现如果我具有所有依赖性,则应该检查我的Assets/Plugins/Android文件夹。 我做到了,无论如何我都强迫解决了,但问题仍然存在。

0 个答案:

没有答案