奖励广告回调未触发 [英] Reward ad callback is not firing

查看:148
本文介绍了奖励广告回调未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个游戏,当玩家丧生时,我希望他们能够再次观看视频。

i have a game and when the player loses their lives, i want them to be able to watch a video for a second chance.

我正在使用统一版本2018.1.1f1人和我已经下载了admob unity插件版本3.13.1

I am using unity version 2018.1.1f1 person and i have downloaded the admob unity plugin version 3.13.1

因此,如果玩家同意观看广告,则该广告将播放,然后继续游戏而不会触发奖励玩家的回调。这是我的代码:

So if the player agrees to watch an ad, the ad will play and then resume the game without firing the callback that rewards the player. This is my code:

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

public class RewardAd : MonoBehaviour {
    private BannerView bannerView;
    private InterstitialAd interstitial;
    private RewardBasedVideoAd rewardBasedVideo;
    private float deltaTime = 0.0f;
    private static string outputMessage = string.Empty;

    public AudioSource musicPlayer;
    public Player player;

    public Text UIText;

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

    public void Start()
    {

        #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

        MobileAds.SetiOSAppPauseOnBackground(true);

        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);

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

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

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

    // 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 = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
        string adUnitId = "ca-app-pub-7624023175090985/4535603801";
#else
        string adUnitId = "unexpected_platform";
#endif

        this.rewardBasedVideo.LoadAd(this.CreateAdRequest(), adUnitId);
    }

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

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

    #region RewardBasedVideo callback handlers

    public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
    {
        this.rewardBasedVideo.Show();
        UIText.text = "Watch a short video\nfor an extra life ?";
    }

    public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        UIText.text = "Watch a short video\nfor an extra life ?";
    }

    public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
    {
        musicPlayer.Pause();
        player.disableMovment = true;
    }

    public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
    {
        musicPlayer.Pause();
        player.disableMovment = true;
    }

    public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    {
        UIText.text = "Watch a short video\nfor an extra life ?";
    }

    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        player.rewardAdUI.SetActive(false);
        UIText.text = "Watch a short video\nfor an extra life ?";
        player.disableMovment = false;

        if (PlayerPrefs.GetInt("music") == 1)
        {
            musicPlayer.Play();
        }

        Lives.addLives(1);
        player.RespawnPlayer();
    }

    public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
    {
        UIText.text = "Watch a short video\nfor an extra life ?";
    }

    public void secondChance()
    {
        UIText.text = "Loading video...\nplease wait.";
        this.RequestRewardBasedVideo();
    }

    public void GameOver()
    {
        player.disableMovment = false;

        if (PlayerPrefs.GetInt("music") == 1)
        {
            musicPlayer.Play();
        }

        player.gameOver();
    }

    #endregion
}

倒数第二个函数secondChance()就是播放广告的过程。在我的android设备上,它将播放经过测试的奖励视频。

So the 2nd to last function called secondChance() is what runs to play the ad. On my android device, this plays the test rewarded video.

广告播放之后,应调用HandleRewardBasedVideoRewarded函数,但不会。

After the ad plays, the HandleRewardBasedVideoRewarded function should be called but it doesnt. Any help at all is appriciated

推荐答案

我在Android Studio中进行调试后发现了一个静默错误,这是由于Unity中的线程问题所致。

I found a Silent Error after debugging in Android Studio, its due to threading issues in Unity.

解决方案:在主线程中调用处理事件(在Update方法中)

Solution : Call your handling event in main thread (in Update Method)

bool isAdClosed = false;
bool isRewarded = false;
void Update()
{
    if (isAdClosed)
    {
        if (isRewarded)
        {
            // do all the actions
            // reward the player
            isRewarded = false;
        }
        else
        {
            // Ad closed but user skipped ads, so no reward 
           // Ad your action here 
        }
        isAdClosed = false;  // to make sure this action will happen only once.
    }
}

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

    RequestRewardBasedVideo(); // to load Next Videoad
    isAdClosed = true;

}

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

这篇关于奖励广告回调未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆