Unity + Admob奖励广告:事件未触发

时间:2018-09-05 08:27:43

标签: unity3d admob admob-rewardedvideoad

我受制于受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);

1 个答案:

答案 0 :(得分:0)

首先: 在您的事件中调用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());