简体   繁体   English

unity 3D: Admob 奖励广告事件回调仅触发一次

[英]Unity 3D: Admob rewarded Ads event callback fires only once

I have Admob rewarded ads in my game and I have an continue button as soon as player die, so the problem is that when I click on continue the ads shows and the player revives(callback fired) but say if player died again (same run) and click to continue the ad would show but no callback fires and for the callback to fire I have to reset the scene.我的游戏中有 Admob 奖励广告,一旦玩家死亡,我就有一个继续按钮,所以问题是当我点击继续时,广告显示并且玩家复活(回调触发)但是如果玩家再次死亡(同样的运行) ) 并单击以继续广告会显示,但不会触发回调,并且要触发回调,我必须重置场景。 The ads shows all the time but callback fires only once per scene.广告一直显示,但每个场景只触发一次回调。

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

public class AdsManager:MonoBehaviour公共 class AdsManager:MonoBehaviour

{
    public GameObject PlayerScript;
public static AdsManager AdsInstance;



string Rewarded_AD = "ca-app-pub-3940256099942544/5224354917"; // these all 3 ids are test ids, swap them later with real ids, delete this line when done
string Inter_AD = "ca-app-pub-3940256099942544/1033173712";

private RewardedAd rewardedAD;
// private RewardBasedVideoAd rewardedAD;
private InterstitialAd InterAd;

private void Awake()
{
    if (AdsInstance == null)
    {
        AdsInstance = this;
    }
    else if (AdsInstance != this)
    {
        Destroy(gameObject);
    }




}

private void Start() {
    MobileAds.Initialize(initStatus => {

        
        Debug.Log("Initialize done");
       


    });

    PlayerScript = GameObject.FindGameObjectWithTag("players");



    this.RequestRewardedAd();
    this.RequestInterAd();


    this.rewardedAD.OnAdClosed += HandleRewardedAdCloseed;
    this.rewardedAD.OnUserEarnedReward += HandleUserEarnedRewards;

   
}
private void Update()
{
    PlayerScript = GameObject.FindGameObjectWithTag("players");

   
}


public void RequestRewardedAd()
{
    //  this.rewardedAD =  RewardBasedVideoAd.Instance;   //RewardBasedVideoAds(Deprecated ones)

    this.rewardedAD = new RewardedAd(Rewarded_AD);

    AdRequest request = new AdRequest.Builder().Build();
  //  this.rewardedAD.LoadAd(request, Rewarded_AD);     //RewardBasedVideoAds(Deprecated ones)
    this.rewardedAD.LoadAd(request);
}


public void ShowRewardBasedVideoAds()
{
    if (rewardedAD.IsLoaded())
    {
        rewardedAD.Show();
        
    }
}


public void RequestInterAd()
{
    this.InterAd = new InterstitialAd(Inter_AD);


    AdRequest request = new AdRequest.Builder().Build();

    this.InterAd.LoadAd(request);

}
public void ShowInterstitialAd()
{
    if (InterAd.IsLoaded())
    {

        InterAd.Show();
        ScoreManagerScript.ScoreInstance.AddCoinsOnWatchingAd();




        RequestInterAd();
    }

}


public void HandleRewardedAdCloseed(object sender, EventArgs args)
{
   
    MonoBehaviour.print("HandleRewardedAdClosed event received");
    this.RequestRewardedAd();
    PlayerScript.GetComponent<Player>().rb.isKinematic = true;
    PlayerScript.GetComponent<Player>().StartEnemySpikeTimer();
    // ScoreManagerScript.ScoreInstance.ReAddTheCoinsIfAdIsClosed();

}


public void HandleUserEarnedRewards(object sender, Reward args)
{

  
    MonoBehaviour.print("HandleRewardedAd event received ");

    PlayerScript.GetComponent<Player>().ReSpawnPlayer();

    ScoreManagerScript.ScoreInstance.ReduceCoinsOnContinue();

    this.RequestRewardedAd();
    
}

Also I have tried swapping the ads subscription into the RequestRewardedAd() method so yeah it doesn't work either, actually when I do that then only one ad is shown per scene and only one callback.此外,我尝试将广告订阅交换为 RequestRewardedAd() 方法,所以它也不起作用,实际上当我这样做时,每个场景只显示一个广告并且只有一个回调。

Please help thanks请帮忙谢谢

I think I discovered what might be wrong, please give it a test and tell me if it works, I can't test it right now.我想我发现了可能有问题的地方,请给它一个测试并告诉我它是否有效,我现在无法测试它。

Try adding the following function:尝试添加以下 function:

void InitializeRewardedAd()
{
    //Creating a new instance of rewarded ads
    this.rewardedAD = new RewardedAd(Rewarded_AD);

    //loading the ads
    AdRequest request = new AdRequest.Builder().Build();

    this.rewardedAD.LoadAd(request);

    //subscribing the events to that new instance
    this.rewardedAD.OnAdClosed += HandleRewardedAdCloseed;
    this.rewardedAD.OnUserEarnedReward += HandleUserEarnedRewards;
}

and call InitializeRewardedAds() when you close the rewarded ads HandleRewardedAdCloseed (you only need to call it once when you close it, you are currently calling it both when you close it and when it fires a reward)并在您关闭奖励广告HandleRewardedAdCloseed时调用InitializeRewardedAds() (您只需要在关闭它时调用它一次,您当前在关闭它和触发奖励时都调用它)

I think the problem can be that the events are not being subscribed to the new instance of the ad, only at the start, so only when you restart the scene it gets subscribed again.我认为问题可能是事件没有被订阅到广告的新实例,只是在开始时,所以只有当你重新启动场景时,它才会再次被订阅。

PS: Just one other thing, do you really need to search for "PlayerScript" every frame? PS:还有一件事,你真的需要每帧搜索“PlayerScript”吗? It can affect your performance.它会影响你的表现。 Maybe it would be nicer to do it once at Start() .也许在Start()做一次会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM