【问题标题】:Why isn't my code increasing the score on reward video ad watched?为什么我的代码没有增加观看的奖励视频广告的分数?
【发布时间】:2020-06-15 07:48:06
【问题描述】:

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

下面是代码。

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;
    }
}

【问题讨论】:

  • 不确定是否相关,但是:您不应该在FixedUpdate 中使用此Totalpointts = PlayerPrefs.GetInt("points" , Totalpointts); ...您是否尝试在debugging 中设置断点并查看调用了哪些方法?
  • 顺便问一下,为什么你的Start[Obsolete] ^^?
  • 我不知道。我从开发人员那里得到了一些帮助。我付钱给他,他给了我这个代码。
  • 正如@Lexicon 所说,尝试将日志放入 x2Thepoints() 方法以了解该方法是否被调用。如果控制台没有出现日志,说明回调方法(HandleOnRewarded)有问题。

标签: c# unity3d admob ads reward


【解决方案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();
  });
}

【讨论】:

  • 不工作..也许我的其他分数有问题?
  • @Ben:我没有从你那里复制,看来你在我最初的回复之后编辑了我的回复——我同意这不是提高分数的好方法——我可以看到编辑历史仅供参考。 Azhar:能否请您在 x2ThePoints() 函数中 saveit=true 后添加 Debug.Log("x2ThePoints called with "+TotalPointts) ,看看控制台是否弹出消息?
  • @Lexicon,我编辑了语法,而不是代码。您首先编辑了您的回答,然后添加了我的代码:P。但没关系,我们是来帮忙的,不是来打架的^^。
【解决方案2】:

不保证在 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 是单线程)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多