【问题标题】:Unity and AdMob [Google Ads] demo don't show ADSUnity 和 AdMob [Google Ads] 演示不显示 ADS
【发布时间】:2016-07-02 09:57:58
【问题描述】:

一周以来,我一直在使用 Google Ads 遇到问题。 我从 googleads/googleads-mobile-unity 下载 admob 插件

版本:3.0.4

然后打开我导入包并在下面实现演示脚本:

using System;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;

public class GoogleMobileAdsDemoHandler : IDefaultInAppPurchaseProcessor
{
    private readonly string[] validSkus = { "android.test.purchased" };

    //Will only be sent on a success.
    public void ProcessCompletedInAppPurchase(IInAppPurchaseResult result)
    {
        result.FinishPurchase();
        GoogleMobileAdsDemoScript.OutputMessage = "Purchase Succeeded! Credit user here.";
    }

    //Check SKU against valid SKUs.
    public bool IsValidPurchase(string sku)
    {
        foreach(string validSku in validSkus)
        {
            if (sku == validSku)
            {
                return true;
            }
        }
        return false;
    }

    //Return the app's public key.
    public string AndroidPublicKey
    {
        //In a real app, return public key instead of null.
        get { return null; }
    }
}

// Example script showing how to invoke the Google Mobile Ads Unity plugin.
public class GoogleMobileAdsDemoScript : MonoBehaviour
{

    private BannerView bannerView;
    private InterstitialAd interstitial;
    private RewardBasedVideoAd rewardBasedVideo;
    private float deltaTime = 0.0f;
    private static string outputMessage = "";

    public static string OutputMessage
    {
        set { outputMessage = value; }
    }

    void Start()
    {
        // Get singleton reward based video ad reference.
        rewardBasedVideo = RewardBasedVideoAd.Instance;

        // RewardBasedVideoAd is a singleton, so handlers should only be registered once.
        rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
        rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
        rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
        rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
        rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
        rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
    }

    void Update()
    {
        // Calculate simple moving average for time to render screen. 0.1 factor used as smoothing
        // value.
        deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
    }

    void OnGUI()
    {
        GUIStyle style = new GUIStyle();

        Rect rect = new Rect(0, 0, Screen.width, Screen.height);
        style.alignment = TextAnchor.LowerRight;
        style.fontSize = (int)(Screen.height * 0.06);
        style.normal.textColor = new Color(0.0f, 0.0f, 0.5f, 1.0f);
        float fps = 1.0f / deltaTime;
        string text = string.Format("{0:0.} fps", fps);
        GUI.Label(rect, text, style);

        // Puts some basic buttons onto the screen.
        GUI.skin.button.fontSize = (int)(0.03f * Screen.height);

        Rect requestBannerRect = new Rect(0.1f * Screen.width, 0.05f * Screen.height,
                                     0.8f * Screen.width, 0.1f * Screen.height);
        if (GUI.Button(requestBannerRect, "Request Banner"))
        {
            RequestBanner();
        }

        Rect showBannerRect = new Rect(0.1f * Screen.width, 0.175f * Screen.height,
                                  0.8f * Screen.width, 0.1f * Screen.height);
        if (GUI.Button(showBannerRect, "Show Banner"))
        {
            bannerView.Show();
        }

        Rect destroyBannerRect = new Rect(0.1f * Screen.width, 0.3f * Screen.height,
                                     0.8f * Screen.width, 0.1f * Screen.height);
        if (GUI.Button(destroyBannerRect, "Destroy Banner"))
        {
            bannerView.Destroy();
        }

        Rect requestInterstitialRect = new Rect(0.1f * Screen.width, 0.425f * Screen.height,
                                           0.8f * Screen.width, 0.1f * Screen.height);
        if (GUI.Button(requestInterstitialRect, "Request Interstitial"))
        {
            RequestInterstitial();
        }

        Rect showInterstitialRect = new Rect(0.1f * Screen.width, 0.55f * Screen.height,
                                        0.8f * Screen.width, 0.1f * Screen.height);
        if (GUI.Button(showInterstitialRect, "Show Interstitial"))
        {
            ShowInterstitial();
        }
/*
        Rect requestRewardedRect = new Rect(0.1f * Screen.width, 0.675f * Screen.height,
                                       0.8f * Screen.width, 0.1f * Screen.height);
        if (GUI.Button(requestRewardedRect, "Request Rewarded Video"))
        {
            RequestRewardBasedVideo();
        }

        Rect showRewardedRect = new Rect(0.1f * Screen.width, 0.8f * Screen.height,
                                    0.8f * Screen.width, 0.1f * Screen.height);
        if (GUI.Button(showRewardedRect, "Show Rewarded Video"))
        {
            ShowRewardBasedVideo();
        }
        */
        Rect textOutputRect = new Rect(0.1f * Screen.width, 0.925f * Screen.height,
                                  0.8f * Screen.width, 0.05f * Screen.height);
        GUI.Label(textOutputRect, outputMessage);
    }

    private void RequestBanner()
    {
        #if UNITY_EDITOR
            string adUnitId = "unused";
#elif UNITY_ANDROID
            string adUnitId = "ca-app-pub-7875398596727009/9705603172";
#elif (UNITY_5 && UNITY_IOS) || UNITY_IPHONE
            string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
            string adUnitId = "unexpected_platform";
#endif

        // Create a 320x50 banner at the top of the screen.
        bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
        // Register for ad events.
        bannerView.OnAdLoaded += HandleAdLoaded;
        bannerView.OnAdFailedToLoad += HandleAdFailedToLoad;
        bannerView.OnAdLoaded += HandleAdOpened;
        bannerView.OnAdClosed += HandleAdClosed;
        bannerView.OnAdLeavingApplication += HandleAdLeftApplication;
        // Load a banner ad.
        bannerView.LoadAd(createAdRequest());
    }

    private void RequestInterstitial()
    {
        #if UNITY_EDITOR
            string adUnitId = "unused";
#elif UNITY_ANDROID
            string adUnitId = "ca-app-pub-7875398596727009/1543207972";
#elif (UNITY_5 && UNITY_IOS) || UNITY_IPHONE
            string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
#else
            string adUnitId = "unexpected_platform";
#endif

        // Create an interstitial.
        interstitial = new InterstitialAd(adUnitId);
        // Register for ad events.
        interstitial.OnAdLoaded += HandleInterstitialLoaded;
        interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
        interstitial.OnAdOpening += HandleInterstitialOpened;
        interstitial.OnAdClosed += HandleInterstitialClosed;
        interstitial.OnAdLeavingApplication += HandleInterstitialLeftApplication;
        // Load an interstitial ad.
        interstitial.LoadAd(createAdRequest());
    }

    // Returns an ad request with custom ad targeting.
    private AdRequest createAdRequest()
    {
        return new AdRequest.Builder()
                .AddTestDevice(AdRequest.TestDeviceSimulator)
                //.AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
                //.AddKeyword("game")
                //.SetGender(Gender.Male)
                //.SetBirthday(new DateTime(1985, 1, 1))
                //.TagForChildDirectedTreatment(false)
                //.AddExtra("color_bg", "9B30FF")
                .Build();
    }

    private void RequestRewardBasedVideo()
    {
        #if UNITY_EDITOR
            string adUnitId = "unused";
        #elif UNITY_ANDROID
            string adUnitId = "INSERT_ANDROID_REWARD_BASED_VIDEO_AD_UNIT_ID_HERE";
        #elif (UNITY_5 && UNITY_IOS) || UNITY_IPHONE
            string adUnitId = "INSERT_IOS_REWARD_BASED_VIDEO_AD_UNIT_ID_HERE";
        #else
            string adUnitId = "unexpected_platform";
        #endif

        rewardBasedVideo.LoadAd(createAdRequest(), adUnitId);
    }

    private void ShowInterstitial()
    {
        if (interstitial.IsLoaded())
        {
            interstitial.Show();
        }
        else
        {
            print("Interstitial is not ready yet.");
        }
    }

    private void ShowRewardBasedVideo()
    {
        if (rewardBasedVideo.IsLoaded())
        {
            rewardBasedVideo.Show();
        } else
        {
            print("Reward based video ad is not ready yet.");
        }
    }

    #region Banner callback handlers

    public void HandleAdLoaded(object sender, EventArgs args)
    {
        print("HandleAdLoaded event received.");
    }

    public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        print("HandleFailedToReceiveAd event received with message: " + args.Message);
    }

    public void HandleAdOpened(object sender, EventArgs args)
    {
        print("HandleAdOpened event received");
    }

    void HandleAdClosing(object sender, EventArgs args)
    {
        print("HandleAdClosing event received");
    }

    public void HandleAdClosed(object sender, EventArgs args)
    {
        print("HandleAdClosed event received");
    }

    public void HandleAdLeftApplication(object sender, EventArgs args)
    {
        print("HandleAdLeftApplication event received");
    }

    #endregion

    #region Interstitial callback handlers

    public void HandleInterstitialLoaded(object sender, EventArgs args)
    {
        print("HandleInterstitialLoaded event received.");
    }

    public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
    }

    public void HandleInterstitialOpened(object sender, EventArgs args)
    {
        print("HandleInterstitialOpened event received");
    }

    void HandleInterstitialClosing(object sender, EventArgs args)
    {
        print("HandleInterstitialClosing event received");
    }

    public void HandleInterstitialClosed(object sender, EventArgs args)
    {
        print("HandleInterstitialClosed event received");
    }

    public void HandleInterstitialLeftApplication(object sender, EventArgs args)
    {
        print("HandleInterstitialLeftApplication event received");
    }

    #endregion

    #region RewardBasedVideo callback handlers

    public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
    {
        print("HandleRewardBasedVideoLoaded event received.");
    }

    public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        print("HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
    }

    public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
    {
        print("HandleRewardBasedVideoOpened event received");
    }

    public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
    {
        print("HandleRewardBasedVideoStarted event received");
    }

    public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    {
        print("HandleRewardBasedVideoClosed event received");
    }

    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        string type = args.Type;
        double amount = args.Amount;
        print("HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " +
                type);
    }

    public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
    {
        print("HandleRewardBasedVideoLeftApplication event received");
    }

    #endregion
}

请在下面找到日志形式 logcat:

横幅:I/Ads (16645):开始广告请求。

I/Ads (16645):使用 AdRequest.Builder.addTestDevice("17F2B8177B4B56D0B45Z78C2D204D7BC") 到 在此设备上获取测试广告。

W/Ads (16645):获得广告响应时出现问题。错误代码: 0

W/Ads (16645):未能加载广告:0

I/Unity (16645):HandleFailedToReceiveAd 事件收到消息: 内部错误

I/Unity (16645): UnityEngine.DebugLogHandler:Internal_Log(LogType, 字符串,对象)

I/Unity (16645): UnityEngine.DebugLogHandler:LogFormat(LogType, 对象、字符串、对象[])

I/Unity (16645):UnityEngine.Logger:Log(LogType, Object)

I/Unity (16645):UnityEngine.Debug:Log(Object)

I/Unity (16645): UnityEngine.MonoBehaviour:print(Object) (at /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineMonoBehaviourBindings.gen.cs:106)

我/团结 (16645): GoogleMobileAdsDemoScript:HandleAdFailedToLoad(Object, AdFailedToLoadEventArgs) (在 G:\Admob\Assets\samples\HelloWorld\Assets\GoogleMobileAdsDemoScript.cs:253)

I/Unity (16645):GoogleMobileAds.Api.BannerView:m__8(Object, AdFailedToLoadEventArgs) (在 G:\Admob\Assets\GoogleMobileAds\Api\BannerView.cs:45)

我/团结 (16645): GoogleMobileAds.Android.BannerClient:onAdFailedToLoad(String) (在 G:\Admob\Assets\GoogleMobileAds\Platforms\Android\BannerClient.cs:86)

I/Unity (16645): System.Reflection.MonoMethod:InternalInvoke(Object, 对象[], 异常&)

I/Unity (16645): System.Reflection.MonoMethod:Invoke(Object, BindingFlags、Binder、Object[

E/Ads (12275):连接失败 https://googleads.g.doubleclick.net/pagead/drt/m。未检索到 DSID。

插页式广告:I/Ads (16645):启动广告请求。

I/Ads (16645):使用 AdRequest.Builder.addTestDevice("17F2B8177B4B56D0B45Z78C2D204D7BC") 到 在此设备上获取测试广告。

W/Ads (16645):获得广告响应时出现问题。错误代码: 0

W/Ads (16645):未能加载广告:0

I/Unity (16645):收到 HandleInterstitialFailedToLoad 事件 消息:内部错误

I/Unity (16645): UnityEngine.DebugLogHandler:Internal_Log(LogType, 字符串,对象)

I/Unity (16645): UnityEngine.DebugLogHandler:LogFormat(LogType, 对象、字符串、对象[])

I/Unity (16645):UnityEngine.Logger:Log(LogType, Object)

I/Unity (16645):UnityEngine.Debug:Log(Object)

I/Unity (16645): UnityEngine.MonoBehaviour:print(Object) (at /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineMonoBehaviourBindings.gen.cs:106)

我/团结 (16645): GoogleMobileAdsDemoScript:HandleInterstitialFailedToLoad(对象, AdFailedToLoadEventArgs) (在 G:\Admob\Assets\samples\HelloWorld\Assets\GoogleMobileAdsDemoScript.cs:287)

I/Unity (16645):GoogleMobileAds.Api.InterstitialAd:m__12(Object, AdFailedToLoadEventArgs) (在 G:\Admob\Assets\GoogleMobileAds\Api\InterstitialAd.cs:44)

我/团结 (16645): GoogleMobileAds.Android.InterstitialClient:onAdFailedToLoad(String) (在 G:\Admob\Assets\GoogleMobileAds\Platforms\Android\InterstitialClient.cs:99)

I/Unity (16645): System.Reflection.MonoMethod:InternalInvoke(Object, 对象[], 异常&)

I/Unity (16645):System.Reflection.MonoMethod:In

在编辑器日志中建议脚本正常工作,显示日志虚拟加载广告等。但是当我构建并安装到我的设备时,没有广告?!

你知道哪里出了问题吗?

我在我的设备小米 Mi4c 上试用它只是因为我还没有访问其他 Android 设备的权限。

【问题讨论】:

    标签: unity3d admob ads


    【解决方案1】:

    首先尝试使用测试 ID。真实的广告 ID 并不总是匹配。

    参考:https://developers.google.com/admob/android/test-ads

    【讨论】:

      【解决方案2】:

      从 Admob Unity3d Plugin Project Home https://github.com/unity-plugins/Unity-Admob 下载这些文件

      安装 Admob Unity

      在 Unity 编辑器中打开您的项目。 导航到资产 -> 导入包 -> 自定义包。 选择 AdmobUnityPlugin.unitypackage 文件。 通过选择导入来导入插件的所有文件。确保检查与文件的任何冲突。 Unity 插件 Wiki 和文档

      1.Init Admob Unity 插件 创建一个C#脚本,将脚本拖到场景中的一个物体上,在脚本文件中添加如下代码

          using admob;
          Admob.Instance().initAdmob("admob banner id", "admob interstitial id");//admob id with format ca-app-pub-279xxxxxxxx/xxxxxxxx
      

      2.在Unity App中添加Admob Banner 这是显示 admob 横幅所需的最少代码。

      Admob.Instance().showBannerRelative(AdSize.Banner, AdPosition.BOTTOM_CENTER, 0);
      

      AdPosition 类指定放置横幅的位置。 AdSize 指定要显示的女巫大小横幅

      3.删除横幅 默认情况下,横幅是可见的。要暂时隐藏横幅,请调用:

      Admob.Instance().removeBanner();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-23
        相关资源
        最近更新 更多