0

当我单击按钮加载奖励视频广告时,分数应该会翻倍。相反,奖励广告会播放,当我看完后关闭时,什么也没有发生。我使用公共静态变量点来获取从游戏场景到关卡完成场景的得分值,然后从玩家预制件中获取总分。我是编码新手。

贝娄是代码。

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

public class Admob : MonoBehaviour
{

    private BannerView adBanner;
    private RewardBasedVideoAd adReward;
    int pointts;
    int Totalpointts;
    private string idApp, idBanner, idReward;
    bool saveit = false;
    float Timer;
    public Text TotalPointsText;
    public GameObject congratulationspop;
    //public WatchVideo x2coinsWatch;

    [SerializeField] Button BtnX2;

    // Start is called before the first frame update
    [Obsolete]
    void Start()
    {
        //get data
        //
        idApp = "ca-app-pub-3940256099942544~3347511713";
        idBanner = "ca-app-pub-3940256099942544/6300978111";
        idReward = "ca-app-pub-3940256099942544/5224354917";

        adReward = RewardBasedVideoAd.Instance;
        MobileAds.Initialize(idApp);

        Debug.LogWarning("Banner Ad Embeded");
        RequestBannerAd();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void GiveRewards()
    {
        BtnX2.interactable = false;
        BtnX2.GetComponentInChildren<Text>().text = "Loading...";
        RequestRewardAd();
    }

    public void RequestBannerAd()
    {
        adBanner = new BannerView(idBanner, AdSize.Banner, AdPosition.Bottom);
        AdRequest request = AdRequestBuild();
        adBanner.LoadAd(request);
    }

    public void DestroyBannerAd()
   {
        if (adBanner != null)
        {
        adBanner.Destroy();
        }
   }

    public void RequestRewardAd()
    {
        AdRequest request = AdRequestBuild();
        adReward.LoadAd(request, idReward);

        adReward.OnAdLoaded += this.HandleOnRewardedAdLoaded;
        adReward.OnAdRewarded += this.HandleOnRewarded;
        adReward.OnAdClosed += this.HandleOnRewardedAdClosed;
    }

    public void ShowRewardAd()
    {
        if (adReward.IsLoaded())
        adReward.Show();
    }

    public void HandleOnRewardedAdLoaded(object sender, EventArgs args) // Ad Loaded
    {
        ShowRewardAd();
    }

    public void HandleOnRewardedAdOpening(object sender, EventArgs args) // Ad Opening
    {

    }

    public void HandleOnRewarded(object sender, EventArgs args) // User Finished Wacthing The Ad
    {
        // Funtion here to double the coins
        x2Thepoints();
    }

    public void HandleOnRewardedAdClosed(object sender, EventArgs args) // ad closed not watching the 
ad
    {
        BtnX2.interactable = false;
        BtnX2.GetComponentInChildren<Text>().text = "X2 Points";
        //testing

        //testing till here
        adReward.OnAdLoaded -= this.HandleOnRewardedAdLoaded;
        adReward.OnAdRewarded -= this.HandleOnRewarded;
        adReward.OnAdClosed -= this.HandleOnRewardedAdClosed;
    }

    AdRequest AdRequestBuild()
    {
        return new AdRequest.Builder().Build();
    }

    void OnDestroy()
    {
         DestroyBannerAd();

        adReward.OnAdLoaded -= this.HandleOnRewardedAdLoaded;
        adReward.OnAdRewarded -= this.HandleOnRewarded;
        adReward.OnAdClosed -= this.HandleOnRewardedAdClosed;
    }

    private void FixedUpdate()
    { 
        Totalpointts = PlayerPrefs.GetInt("points" , Totalpointts);
        TotalPointsText.text = "Total Points : " + Totalpointts;
        pointts = destination.Mypoint;

        Debug.Log(pointts);
        if (saveit == true)
        {
            Timer += Time.deltaTime;
            if (Timer >= 2f)
            {
                Points.inc.OnSavePoint();
                saveit = false;
            }
        }
    }

    public void x2Thepoints()
    {       
        Totalpointts += pointts;
        PlayerPrefs.SetInt("points", Totalpointts);
        PlayerPrefs.Save();
        saveit = true;

        BtnX2.interactable = false;
        congratulationspop.SetActive(true);
        TotalPointsText.text = "Total Points : " + Totalpointts;
    }
}
4

2 回答 2

-1
  1. 在 adReward.LoadAd(request, idReward); 之前设置广告相关的事件处理程序

    adReward.OnAdLoaded += this.HandleOnRewardedAdLoaded;
    adReward.OnAdRewarded += this.HandleOnRewarded;
    adReward.OnAdClosed += this.HandleOnRewardedAdClosed;
    adReward.LoadAd(request, idReward);
    
  2. 事件处理程序不能保证在 Unity 主线程中执行。因此,在下一次更新中强制执行它们:

public void HandleOnRewarded(object sender, EventArgs args)
{
   MobileAdsEventExecutor.ExecuteInUpdate(() => {
    x2Thepoints();
  });
}
于 2020-06-15T10:19:03.667 回答
-1

不保证在 Unity 主线程中调用 Unity Google Ads 回调事件。尝试这样设置你的分数:

public void HandleOnRewarded(object sender, EventArgs args) // User Finished Wacthing The Ad
{ 
    MobileAdsEventExecutor.ExecuteInUpdate(() =>
    {
        // Funtion here to double the coins
        x2Thepoints();
    });
}

该方法MobileAdsEventExecutor.ExecuteInUpdate在 Unity 主线程中运行回调(unity 是单线程)。

于 2020-06-15T09:34:45.227 回答