C# 为什么我的代码没有增加观看奖励视频广告的分数?
当我点击按钮加载奖励视频广告时,分数应该加倍。取而代之的是,奖赏广告在播放,当我看完后关闭时,什么也没发生。我使用公共静态变量points来获取游戏场景到关卡完成场景的得分值,然后从玩家预置中获取总分。 我不熟悉编码 下面是代码C# 为什么我的代码没有增加观看奖励视频广告的分数?,c#,unity3d,admob,ads,reward,C#,Unity3d,Admob,Ads,Reward,当我点击按钮加载奖励视频广告时,分数应该加倍。取而代之的是,奖赏广告在播放,当我看完后关闭时,什么也没发生。我使用公共静态变量points来获取游戏场景到关卡完成场景的得分值,然后从玩家预置中获取总分。 我不熟悉编码 下面是代码 using GoogleMobileAds.Api; using System; using UnityEngine; using UnityEngine.UI; public class Admob : MonoBehaviour { private Ba
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;
}
}
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 is mono thread中运行回调
在之前设置与ad相关的事件处理程序
adReward.LOADREQUEST,idReward
事件处理程序不保证在Unity主线程中执行。因此,在下一次更新中强制执行它们:
不确定是否相关,但:您不应该使用此TotalPoints=PlayerPrefs.GetIntpoints,TotalPoints;在固定更新中。。。你有没有试过使用断点并查看调用了哪些方法?顺便问一下,为什么你的开始是[过时]^^?我不知道。我从一个开发者那里得到了一些帮助。我给了他钱,他给了我这个代码。正如@Lexicon所说的,试着把log放到points方法中,以了解该方法是否被调用。如果该日志未出现在控制台上,则表示回调方法handleon.not worked有问题。。也许我的其他分数有问题?@Ben:我没有抄袭你的,你似乎在我最初的回答之后编辑了我的回答-我同意这不是增加你分数的好方法-我可以看到编辑历史供参考。Azhar:请在x2ThePoints函数中的saveit=true之后添加Debug.Logx2ThePoints,并使用+totalpoints调用,然后查看控制台中是否弹出消息?@Lexicon,我编辑了语法,而不是代码。你先编辑了你的Answare,然后你添加了我的代码:P。但不管怎样,我们是来帮忙的,不是来打架的。
adReward.OnAdLoaded += this.HandleOnRewardedAdLoaded;
adReward.OnAdRewarded += this.HandleOnRewarded;
adReward.OnAdClosed += this.HandleOnRewardedAdClosed;
adReward.LoadAd(request, idReward);
public void HandleOnRewarded(object sender, EventArgs args)
{
MobileAdsEventExecutor.ExecuteInUpdate(() => {
x2Thepoints();
});
}