1

我有一个游戏项目,它使用 GoogleMobileAds 和 Unity Ads 来投放横幅、插页式和奖励视频广告(使用统一广告作为中介网络)。一切都设置正确(admob 配置、sdk 和适配器文件等)并且似乎工作正常。所有三种类型的广告都在正常加载和展示。
但是有个小问题。我很难及时响应基于奖励的视频广告OnAdRewarded事件。我尝试了几个选项,包括在事件处理程序中运行奖励命令(不工作)和使用标志并检查其值(也不工作)。据我所知,googlemobileads 事件并未在主线程中引发。所以 flag 应该可以解决问题,但事实并非如此。有没有人遇到过这个问题并找到了解决方案?

我的代码如下:

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

public class RewardBasedAdManager : MonoBehaviour
{
    private RewardBasedVideoAd rewardBasedVideoAd;

    public int rewardAmount = 25;
    public bool isPlayerRewarded = false;

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

        // RewardBasedVideoAd is a singleton, so handlers should only be registered once.
        ..
        rewardBasedVideoAd.OnAdRewarded += this.HandleRewardBasedVideoRewarded;
        ..
        RequestRewardBasedVideo();
    }

    public void RequestRewardBasedVideo(){...}

    ...

    private void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        //Debug.Log("HandleRewardBasedVideoRewarded event received.");
        string type = args.Type;
        double amount = args.Amount;
        //Debug.Log("User rewarded with: " + amount.ToString() + " " + type);

        // Option 1
        // Directly execute reward command (not working)
        // It's a singleton. Could this be the problem?
        CoinTracker.Instance.CoinCount += rewardAmount;

        // Option 2
        // Use flag and check it's value in Update method (not working either)
        isPlayerRewarded = true;
    }

    private void Update()
    {
        if (isPlayerRewarded)
        {
            CoinTracker.Instance.CoinCount += rewardAmount;
            isPlayerRewarded = false;
        }
    }
}
4

0 回答 0