Unity + Admob奖励广告:活动没有解雇

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity + Admob奖励广告:活动没有解雇相关的知识,希望对你有一定的参考价值。

我坚持使用admob奖励广告,我无法想象如何使活动工作。问题是我的测验游戏会在每个问题上重新加载场景,即使我阻止广告被破坏,事件也根本不会发生。广告完美呈现。我尝试了很多东西,但我必须在某处犯错......任何人都有想法?

非常感谢你!

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


public class RewardedScriptRow : MonoBehaviour
{
    private RewardBasedVideoAd rewardBasedVideo;
    public AudioClip GiftSound;

    // Use this for initialization
    void Start()
    {    
        RequestInterstitial();
        Debug.Log("Load at start");
    }

    public void LaunchAd() //Called from another script
    {
        StartCoroutine("Load");
    }

    private void RequestInterstitial()
    {

        string adUnitId = "";
#if UNITY_android
        adUnitId = "ca-app-pub-00000/00000000";
#elif UNITY_ios
         adUnitId = "ca-app-pub-0000000000000";
#else
         adUnitId = "unexpected_platform";
#endif

        // Get singleton reward based video ad reference.
        this.rewardBasedVideo = RewardBasedVideoAd.Instance;

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

        rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoClosed;
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
    }


    IEnumerator Load()
    {
        while (!rewardBasedVideo.IsLoaded())
            yield return new WaitForEndOfFrame();

        yield return new WaitForSeconds(0.0f);
        rewardBasedVideo.Show();  
        yield break;
    }



    //EVENT
    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
        RequestInterstitial();
    }

    public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    {
        GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
        RequestInterstitial();
    }
}

编辑1:

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


public class RewardedScriptRow : MonoBehaviour
{
    private RewardBasedVideoAd rewardBasedVideo;
    public AudioClip GiftSound;
    public static RewardedScriptRow Instance;

    // Use this for initialization
    void Start()
    {
         Instance = this;
         DontDestroyOnLoad(this);
         RequestRewardBasedVideo();

        rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoClosed;
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;

     }

    //Called after 10 questions
    public void LaunchAd() 
    {
        StartCoroutine("Load");
    }

    private void RequestRewardBasedVideo()
    {

        string adUnitId = "";
#if UNITY_ANDROID
        adUnitId = "ca-app-pub-0000000/0000000000";
#elif UNITY_IOS
         adUnitId = "ca-app-pub-00000/00000000";
#else
         adUnitId = "unexpected_platform";
#endif

        // Get singleton reward based video ad reference.
        this.rewardBasedVideo = RewardBasedVideoAd.Instance;
        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the interstitial with the request.
        this.rewardBasedVideo.LoadAd(request, adUnitId);

    }

    //EVENT
    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F); 
        RequestRewardBasedVideo();

    }
    public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    {
        GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
        RequestRewardBasedVideo();
    }

    IEnumerator Load()
    {
        while (!rewardBasedVideo.IsLoaded())
            yield return new WaitForEndOfFrame();

        yield return new WaitForSeconds(0.0f);
        rewardBasedVideo.Show();
        yield break;
    }
}

这就是游戏如何与场景一起工作:

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
答案

首先:在你的活动中调用RequestInterstial方法似乎是非常不明智/不合逻辑的。因为通过这样做,您正在为您已订阅的相同事件创建多个订阅!这可能导致非常不希望的/不想要的行为以及导致Stackoverflow exceptions

我不清楚为什么你甚至会在事件发生时打电话给RequestInterstial。在我看来,你想要在显示第一个视频后加载新视频。重构您的方法,以便您不添加订阅事件。

将订阅事件和初始化代码移动到Start或Awake方法。

你也不是要求一个有趣的,而是一个基于奖励的视频。我建议重命名以保持代码合乎逻辑。

Public static RewardedScriptRow Instance;

void Start()
{    
    Instance = this;
    DontDestroyOnLoad(this);
    RequestRewardBasedVideo();

    // Get singleton reward based video ad reference.
    this.rewardBasedVideo = RewardBasedVideoAd.Instance;

    rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoClosed;
    rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}

private void RequestRewardBasedVideo()
    {
        #if UNITY_ANDROID
            string appId = "ca-app-pub-3940256099942544~3347511713";
        #elif UNITY_IPHONE
            string appId = "ca-app-pub-3940256099942544~1458002511";
        #else
            string appId = "unexpected_platform";
        #endif

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the rewarded video ad with the request.
        this.rewardBasedVideo.LoadAd(request, adUnitId);
    }

public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
    GetComponent<AudioSource>().PlayOneShot(GiftSound, 1.0F);
    RequestRewardBasedVideo();
}

除此之外,它应该工作。如果您仍未获得理想的结果,请尝试在调试时设置断点和/或在订阅的方法中使用Debug.Log()以查看发生的情况。

编辑:此外,如果由于重新加载场景而发生,您可以尝试添加DontDestroyOnLoad(this);以防止“AdObject”被破坏。我建议在你的第一个场景中创建这个脚本并将其从其他场景中删除(以防止重复)。

然后,您甚至可以应用singleton pattern,以便您可以从其他类中轻松访问脚本。

例:

StartCoroutine(RewardedScriptRow.Instance.LaunchAd());

以上是关于Unity + Admob奖励广告:活动没有解雇的主要内容,如果未能解决你的问题,请参考以下文章

基于 iOS ADMOB 奖励的视频广告加载失败

如何限制奖励视频广告长度(admob)?

Admob奖励视频广告

我们可以使用Admob奖励广告而不向用户提供任何奖励吗?

如何在奖励广告中添加 onAdClick 回调? - AdMob - 安卓

Admob - 如何暂停插页式广告