当我点击按钮加载奖励视频广告时,分数应该翻倍。相反,奖励广告在播放,当我看完后关闭时,什么也没发生。我使用公共静态变量points来获取从游戏场景到关卡完成场景的分数值,然后我从玩家预设中获取总分。我是编码新手。
下面是代码。
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;
}
}
在adReward.LoadAd(request, idReward)之前设置广告相关事件处理程序。
adReward.OnAdLoaded += this.HandleOnRewardedAdLoaded; adReward.OnAdRewarded += this.HandleOnRewarded; adReward.OnAdClosed += this.HandleOnRewardedAdClosed; adReward.LoadAd(request, idReward);
事件处理程序不能保证在Unity主线程中执行。所以,在下次更新时强制执行它们。
public void HandleOnRewarded(object sender, EventArgs args) { MobileAdsEventExecutor.ExecuteInUpdate(() => { x2Thepoints(); }); }
Unity Google Ads回调事件不保证在Unity主线程中被调用。尽量这样设置你的分数。
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是单线程)。