如何检查Unity中的广告是否已完成

时间:2016-05-20 18:58:38

标签: android unity3d admob

我正在使用来自admob的奖励视频广告。当玩家去世时调用广告并询问他们是否想要观看要复活的广告。我试图检查广告何时完成,以便奖励玩家。

void Update()
{
    if(isCalled == true){
        string adUnitId = "ca-app-pub-5920324855307233/4458481507";
        RewardBasedVideoAd rewardBasedVideo = RewardBasedVideoAd.Instance;
        AdRequest request = new AdRequest.Builder().Build();
        rewardBasedVideo.LoadAd(request, adUnitId);
        showAd(rewardBasedVideo);
    }
}

public void showAd(RewardBasedVideoAd rewardBasedVideo)
{
    if (rewardBasedVideo.IsLoaded())
    {
        //Subscribe to Ad event
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
        rewardBasedVideo.Show();
    }
}

public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
    string type = args.Type;
    double amount = args.Amount;
    //Reawrd User here
    print("User rewarded with: " + amount.ToString() + " " + type);

    isCalled = false;

    managerScript.revival();
    managerScript.Loading.SetActive(false);
}

第一次在场景中观看广告时,调用方法复活并关闭加载屏幕并且一切正常,之后再次观看广告时它不起作用," HandleRewardBasedVideoRewarded&#34 ;方法没有被调用,一个新的广告只是弹出来反复观看。我如何修复它,因为游戏没有调用" HandleRewardBasedVideoRewarded"广告第二次结束时的方法。因此调用方法复活并且加载屏幕像第一次打开游戏时那样关闭。 注意:即使重新启动场景(而不是整个游戏),问题仍然会发生。

更新: 我已经将脚本编辑到您的解决方案,但问题仍然存在。哇,它不工作?  这是完整的脚本:

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

public class GameAdvertising : MonoBehaviour {

    public bool isCalled;
    public GameObject Manager;
    GameManager managerScript;

    string adUnitId = "ca-app-pub-5920324855307233/4458481507";
    RewardBasedVideoAd rewardBasedVideo = null;

    void Start () {
        managerScript = gameObject.GetComponent<GameManager>();

        isCalled = false;

        //Subscribe to Ad event once
        rewardBasedVideo = RewardBasedVideoAd.Instance;
        //Subscribe to Ad event once
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;

    }

    public void adButton()
    {
        isCalled = true;
    }

    void Update()
    {
        if(isCalled == true){
            AdRequest request = new AdRequest.Builder().Build();
            rewardBasedVideo.LoadAd(request, adUnitId);
            showAd();
        }
    }

    public void showAd()
    {
        if (rewardBasedVideo.IsLoaded())
        {
            rewardBasedVideo.Show();
        }
    }

    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        string type = args.Type;
        double amount = args.Amount;
        //Reawrd User here
        print("User rewarded with: " + amount.ToString() + " " + type);

        isCalled = false;

        managerScript.revival();
        managerScript.Loading.SetActive(false);
    }
}

1 个答案:

答案 0 :(得分:0)

可能是因为您多次拨打#!/usr/bin/python3 import pyglet window = pyglet.window.Window() label = pyglet.text.Label("Hello World!", font_name="Times New Roman", color=(255,255,255,255), font_size=36, x=window.width//2, y=window.height//2, anchor_x="center", anchor_y="center") @window.event def on_draw(): window.clear() label.draw() pyglet.app.run() 时多次注册到活动。您应该从那里删除它并将其放在Start函数中。

rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;